代码随想录算法训练营第1天 | ● 704. 二分查找 ● 27. 移除元素 - 第1章 数组part01
今天开始第一天,其实之前也刷过题,也写过博客,可是没有坚持下去;主要是没有动力吧,我又是一个严重的拖延症患者,还好遇到刷到Carl哥的视频,记得是在bilibili分享的二分法视频,感觉讲的挺好的,就加了微信;然后发现有刷题训练营,太适合我这种人了,果断加入,哈哈,废话不多说,开始刷题。
第一章 数组part01
今日任务
数组理论基础,704. 二分查找,27. 移除元素
[ ) 一般对于区间的表示,使用左闭右开表示
1 class Solution { 2 public int search(int[] nums, int target) { 3 int left = 0, right = nums.length; 4 while(left < right){ 5 int mid = left + ((right - left)/2); 6 if(nums[mid] == target) 7 return mid; 8 else if (nums[mid] < target) 9 left = mid + 1; 10 else if (nums[mid] > target) 11 right = mid; 12 } 13 return -1; 14 } 15 }
1 class Solution { 2 public int removeElement(int[] nums, int val) { 3 int fast = 0, slow = 0; 4 while (fast < nums.length) { 5 if (nums[fast] != val) { 6 nums[slow] = nums[fast]; 7 slow++; 8 } 9 fast++; 10 } 11 return slow; 12 } 13 }
数组理论基础
文章链接:https://programmercarl.com/%E6%95%B0%E7%BB%84%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html
题目建议: 了解一下数组基础,以及数组的内存空间地址,数组也没那么简单。
704. 二分查找
题目建议: 大家能把 704 掌握就可以,35.搜索插入位置 和 34. 在排序数组中查找元素的第一个和最后一个位置 ,如果有时间就去看一下,没时间可以先不看,二刷的时候在看。
先把 704写熟练,要熟悉 根据 左闭右开,左闭右闭 两种区间规则 写出来的二分法。
题目链接:https://leetcode.cn/problems/binary-search/
文章讲解:https://programmercarl.com/0704.%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE.html
视频讲解:https://www.bilibili.com/video/BV1fA4y1o715
27. 移除元素
题目建议: 暴力的解法,可以锻炼一下我们的代码实现能力,建议先把暴力写法写一遍。 双指针法 是本题的精髓,今日需要掌握,至于拓展题目可以先不看。
题目链接:https://leetcode.cn/problems/remove-element/
文章讲解:https://programmercarl.com/0027.%E7%A7%BB%E9%99%A4%E5%85%83%E7%B4%A0.html