代码随想录-035期-算法训练营【博客笔记汇总表】-CSDN博客
第一章 数组part01
今日任务
数组理论基础,704. 二分查找,27. 移除元素
详细布置
数组理论基础
文章链接: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
视频讲解:https://www.bilibili.com/video/BV12A4y1Z7LP
目录
0704_二分查找
训练营二刷,参考答案,还是不熟练。
- 注意边界值:while (left <= right)
- right = mid - 1;
- left = mid + 1;
- mid = left + (right - left) / 2; // 修正mid的计算方式,防止溢出
System.out.println(Arrays.binarySearch(nums, target));
import java.util.Arrays;
public class _0704_二分查找 {
public static void main(String[] args) {
int nums[] = {-1, 0, 3, 5, 9, 12}, target = 0;
System.out.println(Arrays.binarySearch(nums, target));
}
}
class Solution {
public int search(int[] nums, int target) {
//避免当 target 小于nums[0],nums[nums.length - 1]时,多次循环运算
if (target < nums[0] || target > nums[nums.length - 1]) {
return -1;
}
int left = 0, right = nums.length - 1, mid = nums.length / 2;
while (left <= right) {
if (nums[mid] == target) {
return mid;
} else if (nums[mid] > target) {
right = mid - 1;
mid = (left + right) / 2;
} else if (nums[mid] < target) {
left = mid + 1;
mid = (left + right) / 2;
}
}
return -1;
}
}
0034_在排序数组中查找元素的第一个和最后一个位置
class Solution {
public int[] searchRange(int[] nums, int target) {
int end = -1, len = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == target) {
end = i;
len++;
}
}
if (end == -1) {
return new int[]{-1, -1};
}
return new int[]{end - len + 1, end};
}
}
0035_搜索插入位置
class Solution {
public int searchInsert(int[] nums, int target) {
int n = nums.length;
int left = 0, right = n - 1, ans = n;
while (left <= right) {
int mid = ((right - left) >> 1) + left;
if (target <= nums[mid]) {
ans = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
return ans;
}
}
0027_移除元素
1、暴力解法
for (int j = i; j < nums.length - count; j++) {//
j < nums.length - 1 - count,错误!
内层循环的范围应该是从当前找到的
val
的位置开始,一直到数组末尾,但你的代码中内层循环的结束条件是nums.length - 1 - count
,这个条件是错误的。在移动元素时,你的代码应该将当前位置的元素替换为下一个位置的元素,而不是将下一个位置的元素移到当前位置。这样可以避免丢失数组中的一些元素。
class Solution {
public int removeElement(int[] nums, int val) {
int count = 0;
for (int i = 0; i < nums.length - count; i++) {
if (nums[i] == val) {
count++;
for (int j = i; j < nums.length - count; j++) {//j < nums.length - 1 - count,错误!
nums[j] = nums[j + 1];
}
i--;
}
}
return nums.length - count;
}
public int removeElement(int[] nums, int val) {
int size = nums.length;
for (int i = 0; i < size; i++) {
if (nums[i] == val) {//发现需要移除的元素,就将数组集体向前移动一位
for (int j = i + 1; j < size; j++) {
nums[j - 1] = nums[j];
}
i--;//因为下标i以后的数值都向前移动了一位,所以i也向前移动一位
size--;//此时数组的大小-1
}
}
return size;
}
}
2、双指针法【重点掌握】
class Solution {
public int removeElement(int[] nums, int val) {
int j = 0;//指向新数组的下标
for (int i = 0; i < nums.length; i++) {
if (nums[i] != val) {
nums[j] = nums[i];//将不等于val的元素移到新数组的位置
j++;//更新新数组的下标
}
}
return j;//返回新数组的长度
}
public int removeElement(int[] nums, int val) {
//快慢指针
int slowIndex = 0;
for (int fastIndex = 0; fastIndex < nums.length; fastIndex++) {
if (nums[fastIndex] != val) {
nums[slowIndex] = nums[fastIndex];
slowIndex++;
}
}
return slowIndex;
}
}
ヾ(◍°∇°◍)ノ゙加油~