- LeetCode
- 9. Palindrome Number
- 125. Valid Palindrome
- 680. Valid Palindrome II
- 60. Permutation Sequence
- 31. Next Permutation
- 1793. Maximum Score of a Good Subarray
- 4. Median of Two Sorted Arrays
- 6. Zigzag Conversion
- 11. Container With Most Water
- 12. Integer to Roman
- 13. Roman to Integer
- 14. Longest Common Prefix
- 551. Student Attendance Record I
- 15. 3Sum
- 16. 3Sum Closest
- 17. Letter Combinations of a Phone Number
- 401. Binary Watch
- 18. 4Sum
- 454. 4Sum II
- 560. Subarray Sum Equals K
- 523. Continuous Subarray Sum
- 88. Merge Sorted Array
- 977. Squares of a Sorted Array
- 26. Remove Duplicates from Sorted Array
- 80. Remove Duplicates from Sorted Array II
- 27. Remove Element
- 83. Remove Duplicates from Sorted List
- 1023. Camelcase Matching
- 66. Plus One
- 283. Move Zeroes
# LeetCode
# 9. Palindrome Number
According to the examples, a negative number is not palindromic. And the reverse of the number counting 0.
We can use two pointers to check if it’s a palindrome.
1 | |
# 125. Valid Palindrome
Similar to 5. But first remove all non-alphanumeric characters.
1 | |
# 680. Valid Palindrome II
Similar to 5. But there is only one chance of unmatching. There are two possible situation, therefore we must validate each substring.
1 | |
# 60. Permutation Sequence
A straight forward idea is divide k-1 by (n-1)!, because the n! are divided into (n-1)! interval, therefore we could find the interval number which is exactly the first number. For the rest of number, it’s quite similar. Remove the first number out of the sequence and get the interval and the next corresponding number.
1 | |
# 31. Next Permutation
This is quite different from 60. This is for a general situation.
Wikipedia
According to Wikipedia, a man named Narayana Pandita presented the following simple algorithm to solve this problem in the 14th century.
- Find the largest index k such that nums[k] < nums[k + 1]. If no such index exists, just reverse nums and done.
- Find the largest index l > k such that nums[k] < nums[l].
- Swap nums[k] and nums[l].
- Reverse the sub-array nums[k + 1:].
1 | |
# 1793. Maximum Score of a Good Subarray
Two pointer
We start with two pointers, left = right = k and the area = nums[k]
When increment the size of window, we want to reduce the min(nums[i]…nums[j]) slowly.
To do this, we can check the values on both sides of the window.
If A[i - 1] < A[j + 1], we do j = j + 1
If A[i - 1] >= A[j + 1], we do i = i - 1
1 | |
# 4. Median of Two Sorted Arrays
Brute Force
Time Complexity O(M+N), Space Complexity O(M+N)
1 | |
Binary Search
A common solution for get kth element from two array:
- get median elements k/2 from each array
- if a > b, get (k - k/2)th element from a, b[k/2:]
generalized get kth element from 3 array is the same.
1 | |
1 | |
# 6. Zigzag Conversion
Simple Simulation
1 | |
# 11. Container With Most Water
Try to find a higher line than the the first two lines.
How could you solve with 42.
1 | |
# 12. Integer to Roman
1 | |
# 13. Roman to Integer
1 | |
# 14. Longest Common Prefix
1 | |
# 551. Student Attendance Record I
1 | |
# 15. 3Sum
1 | |
# 16. 3Sum Closest
1 | |
# 17. Letter Combinations of a Phone Number
1 | |
# 401. Binary Watch
1 | |
# 18. 4Sum
TC O(N^3)
SC O(1)
1 | |
# 454. 4Sum II
1 | |
# 560. Subarray Sum Equals K
1 | |
# 523. Continuous Subarray Sum
(a+(n*x))%x is same as (a%x)
For e.g. in case of the array [23,2,6,4,7] the running sum is [23,25,31,35,42] and the remainders are [5,1,1,5,0]. We got remainder 5 at index 0 and at index 3. That means, in between these two indexes we must have added a number which is multiple of the k. Hope this clarifies your doubt 😃
1 | |
# 88. Merge Sorted Array
1 | |
# 977. Squares of a Sorted Array
1 | |
# 26. Remove Duplicates from Sorted Array
1 | |
# 80. Remove Duplicates from Sorted Array II
1 | |
#
# 27. Remove Element
1 | |
# 83. Remove Duplicates from Sorted List
1 | |
# 1023. Camelcase Matching
1 | |
# 66. Plus One
1 | |
# 283. Move Zeroes
1 | |