Inspired by Xi Chen
-
Time Complexity: O(n), Space Complexity: O(n).
The main idea is to use a map to store every element we seen, and for every
nums[i]we check wether there is a corresponding numbertarget - nums[i]stored in the map. If so, get the valuejby the keytarget - nums[i], which is the index of the corresponding number, then return{i + 1, j + 1}. -
Time Complexity: O(n), Space Complexity: O(n);
This problem is rather easy. It focuses more on operations on linked lists. Just scan over the two linked list and add each other together, remember to keep a carry bit and check the boundary conditions.
-
Longest Substring Without Repeating Characters
Time Complexity: O(n), Space Complexity: O(1);
The idea is to use a hash map to track the index of the character we recently met. When we scan the string, we can use the hash map to check whether we have met this character. If so, we get its index and update our
startindex. -
Time Complexity: O(log(n) + log(m)), Space Complexity: O(1);
By the reason that the median of the two arrays should be located between the median of
nums1and the median ofnums2, a good approach is to compare the median ofnums1andnums2, if the median ofnums1is greater than the median ofnums2, then we can just drop the first part ofnums2and go on recursively. -
Space Complexity: O(1);
Scan over the input string. For every character, consider it as the middle character of a palindromic string(Note: it can be an odd length string as well as an even length string, so it should be considered separately), then find the longest palindromic string in that position, and compare the length of it with the longest palindromic substring we have found.
-
Time Complexity: O(n), Space Complexity: O(n);
The key to solve this problem is to calculate the position of every element in the ZigZag pattern(Note that the index starts from 0):
0 2*row - 2 1 2*row - 3 2*row 2 2*row - 4 2*row + 1 ... ... ... row - 2 row + 1 3*row - 2 row - 1 3*row - 3Then the problem becomes easy, we can just scan over the rows. And for every row, we scan the elements in this row using the result of the pattern above.
-
The tricky part of this problem is to handle overflow cases. Before adding the last digit to the return value(
ret * 10 + x), we have to check whether the answer ofret * 10 + xwill overflow. We achieved it by this way:- If the answer is a positive number:
- If
ret > Integer.MAX_VALUE / 10, it would cause overflow. - If
ret == Integer.MAX_VALUE / 10, butx > Integer.MAX_VALUE % 10, the overflow happens too.
- If
- If the answer is a negative number:
- If
ret < Integer.MIN_VALUE / 10, it would cause overflow. - If
ret == Integer.MIN_VALUE / 10, butx < Integer.MIN_VALUE % 10, the overflow happens too.
- If
- If the answer is a positive number:
-
Time Complexity: O(n), Space Complexity: O(1);
We reverse this integer but skip the highest digit to prevent overflow.
For example, if we have
54345, then we reverse it and get5434(Skip the highest digit), then we compare54345 / 10with5434and compare the last remaining digitx = 5(which we skipped) with the lowest digit of the original integer54345 % 10. If they all match, then we can say it is a Palindrome Number. -
Time Complexity: O(n*m), Space Complexity: O(n);
This problem is a little bit difficult. We use dynamic programming to solve it. We use an array of boolean
match. Every element ofmatchis corresponding to an element in the input string to be matched. For example:match[5] == truestands for that every element after and includings[5]matches the regular expression. -
Time Complexity: O(n), Space Complexity: O(1);
In this problem, the smart scan way is to set two pointers initialized at both ends of the array. Every time move the smaller value pointer to inner array. Then after the two pointers meet, all possible max cases have been scanned and the max situation is 100% reached somewhere in the scan.