704.二分查找
注意临界点即可
time: O(logn) space: O(1)
When we analyze the space complexity of an algorithm, we typically focus on the additional space used by the algorithm itself, not the space used by the input data.
Binary search is typically implemented as an in-place algorithm, meaning it operates directly on the input array without requiring additional space for data structures like auxiliary arrays or hash tables. That's why space is O(1).
方法一:左闭右闭 方法二:左闭右开
[left, right], while(left<=right), left = middle + 1, right = middle-1
[left, right), while(left<right), left = middle + 1, right = middle
积累写法:
int middle = left + ((right - left) >> 1);
nums.length
27.移除元素
brute solution: two for loop, one for iterate through array, the other one for update the array
time: O(n^2) space:O(1)
fast and slow index:
time: O(n) space:O(1)
fast index: look for the element which is not equal to val
slow index: update the array
积累写法:
nums[slowIndex++] = nums[fastIndex];