while (lo < hi) choose mi or mi + 1
n + 1 or calculating mi will overflow
search the first rank that is no smaller than the given target
lo++
orhi--
may overflow
using normal swap operation, there will be gcd(n, k)
circles
int gcd(int m, int n){
return n == 0 ? m : gcd(n, m % n);
}
Trick: equals to rotate(nums.rbegin(), nums.rbegin() + k % nums.size(), nums.rend());
Fast-slow bipointers. When the loop ends, those behind slow pointers are all zeroes.
Small-big bipointers. Or using binary-search in #35.
Just reverse.
A pointer to detect space.
Binary search for the first no smaller than the target and the first bigger than it.
e < nums[mi] return lo - 1 // the last no bigger
e <=nums[mi] return hi // the first no smaller
Fast-slow bipointers. Fast goes next of next.
Fast-slow bipointers. Fast ahead by n pointers. Remind of deleting the head.
Recursion Ver.
class Solution {
public:
int cur = 0;
ListNode* removeNthFromEnd(ListNode* head, int n) {
if(!head) return nullptr;
head->next = removeNthFromEnd(head->next, n);
cur++;
if(n == cur) return head->next;
return head;
}
};
PS: Dummyhead can be useful.
Slide-window. Take a vector or map to restore the last position where the end
character appears. Remember to check the window size when the loop ends.
Slide-window.
PS: Update the number of the matched characters.
BFS.
PS: Remenber to change the color when it is pushed in queue.
Check every position to conduct a BFS.
Recursive merge.
When coming to the next layer, the next pointers of the last layer are ready. Use those pointers to traverse the last layer.
Sorted array is the solution.
Set all the zero into the queue.
Same.
Recursion.
Loop: pre, cur, nxt tri-pointers.
Recursion: write an independent function reverse(pre, cur)
.
Same.
Same.
PS: Use 2-digit encoding to traverse.
Fibnacci.
dp[i] = max(dp[i - 2] + nums[i], dp[i - 1])
dp[i] = min(dp[i], dp[i - 1]) + triangle[layer][i]
PS: Bottom-up will be simple.
Best solution:
return n > 0 && (n & (n - 1)) == 0;
Same.
Trick in java's methods Integer.reverse
and Interger.bitCount
. Using the way that count every 2/4/8/16/32 bits.
Xor.
PS: accumulate(vec.begin(), vec.end(), bit_op)
.