代码随想录算法训练营第一天 | 704.二分查找、27.移除元素

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];
  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值