Given two strings s and t, determine if they are isomorphic.
Two strings s and t are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.
Example 1:
1 2
Input: s = "egg", t = "add" Output: true
Example 2:
1 2
Input: s = "foo", t = "bar" Output: false
Example 3:
1 2
Input: s = "paper", t = "title" Output: true
哈希表:
时间复杂度:O(n)
空间复杂度:O(m),m为字符串字符集数目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { public: boolisIsomorphic(string s, string t){ unordered_map<char, char>s_t; unordered_map<char, char>t_s; int len = s.length(); for (int i = 0; i < len; i++) { char x = s[i]; char y = t[i]; if ((s_t.count(x) && s_t[x] != y) || (t_s.count(y) && t_s[y] != x))returnfalse; s_t[x] = y; t_s[y] = x; } returntrue; } };
classSolution { public: intget_next(int value){ int sum = 0; while (value) { sum += (value % 10) * (value % 10); value /= 10; } return sum; } boolisHappy(int n){ int slow = n; int fast = get_next(n); while (fast != 1 && fast != slow) { slow = get_next(slow); fast = get_next(get_next(fast)); } return fast == 1; } };
Write a function that takes an unsigned integer and returns the number of ‘1’ bits it has (also known as the Hamming weight.
Note:
Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer’s internal binary representation is the same, whether it is signed or unsigned.
In Java, the compiler represents the signed integers using 2’s complement notation. Therefore, in Example 3, the input represents the signed integer. -3.
Example 1:
1 2 3
Input: n = 00000000000000000000000000001011 Output: 3 Explanation: The input binary string 00000000000000000000000000001011 has a total of three '1' bits.
Example 2:
1 2 3
Input: n = 00000000000000000000000010000000 Output: 1 Explanation: The input binary string 00000000000000000000000010000000 has a total of one '1' bit.
Example 3:
1 2 3
Input: n = 11111111111111111111111111111101 Output: 31 Explanation: The input binary string 11111111111111111111111111111101 has a total of thirty one '1' bits.
位操作:
时间复杂度:O(logn)
空间复杂度:O(1)
1 2 3 4 5 6 7 8 9 10 11
classSolution { public: inthammingWeight(uint32_t n){ int sum = 0; while (n!=0) { if ((n & 1) == 1)sum += 1; n >>= 1; } return sum; } };
优化:
1 2 3 4 5 6 7 8 9 10 11
classSolution { public: inthammingWeight(uint32_t n){ int ret = 0; while (n) { n &= n - 1; ret++; } return ret; } };
Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer’s internal binary representation is the same, whether it is signed or unsigned.
In Java, the compiler represents the signed integers using 2’s complement notation. Therefore, in Example 2 above, the input represents the signed integer -3 and the output represents the signed integer -1073741825.
Example 1:
1 2 3
Input: n = 00000010100101000001111010011100 Output: 964176192 (00111001011110000010100101000000) Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000.
Example 2:
1 2 3
Input: n = 11111111111111111111111111111101 Output: 3221225471 (10111111111111111111111111111111) Explanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10111111111111111111111111111111.
位操作:
时间复杂度:O(logn)
空间复杂度:O(1)
1 2 3 4 5 6 7 8 9 10 11
classSolution { public: uint32_treverseBits(uint32_t n){ uint32_t r ans = 0; for (int i = 0; i < 32 && n > 0; ++i) { ans |= (n & 1) << (31 - i); n >>= 1; } return ans; } };
Given a string columnTitle that represents the column title as appears in an Excel sheet, return its corresponding column number.
For example:
1 2 3 4 5 6 7 8
A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ...
Example 1:
1 2
Input: columnTitle = "A" Output: 1
Example 2:
1 2
Input: columnTitle = "AB" Output: 28
Example 3:
1 2
Input: columnTitle = "ZY" Output: 701
时间复杂度:O(n)
空间复杂度:O(1)
1 2 3 4 5 6 7 8 9 10 11 12
classSolution { public: inttitleToNumber(string columnTitle){ long n = pow(26, (columnTitle.size() - 1)); int ans = 0; for (char c : columnTitle) { ans += (c - 'A' + 1) * n; n /= 26; } return ans; } };