数组
1、二分查找
--035 搜索插入位置

public class _035 { /** * 搜索插入位置 * @param args */ public static void main(String[] args) { int[] nums=new int[]{1,3,5,6}; int target=5; _035 test = new _035(); System.out.println(test.searchInsert(nums,5)); } public int searchInsert(int[] nums, int target) { int low=0,high=nums.length-1; while (low<=high){//使用闭区间[low,high] int mid=low+(high-low)/2; if(nums[mid]<target) low=mid+1; else if(nums[mid]>target) high=mid-1; else return mid;//1、target与某一个元素相等时,返回下标索引 } return high+1;//2、target比所有元素小 3、target比所有元素大 4、target需要插入到某一元素之后 //2、target比所有元素小,执行最后一次while循环时low==high==0,往下执行满足nums[mid]>target,所以high=0-1==-1,而-1+1=0,刚好是0索引,正确 //3、target比所有元素大, 执行最后一次循环时, low=high==num.length-1, 往下执行满足nums[mid]<target, 所以low=num.length-1+1==num.length, 而high仍==num.length-1, +1后就是尾插到最后你一个元素之后 //4、target需要插入到某一元素之后, 执行最后一次循环时, low=high, 且此时nums[high]元素比target大, 此时high的位置即是要插入的位置, 往下执行满足nums[mid]>target, 所以high=mid-1, 再+1才是实际要插入的位置 } }
--069 x 的平方根

public class _069 { public static void main(String[] args) { _069 test = new _069(); System.out.println(test.mySqrt(1)); } public int mySqrt(int x) { int low=0,high=x; int ans=-1; while(low<=high){ int mid=low+(high-low)/2; if((long)mid*mid<=x){ ans=mid; low=mid+1; } else{ high=mid-1; } } return ans; } }
--367 有效的完全平方数

import java.time.chrono.IsoChronology; public class _367 { public static void main(String[] args) { _367 v = new _367(); System.out.println(v.isPerfectSquare(16)); } public boolean isPerfectSquare(int num) { int low=0,high=num; while (low<=high){ int mid=low+(high-low)/2; if((long)mid*mid<num){ low=mid+1; } else if((long)mid*mid>num) { high=mid-1; } else { return true; } } return false; } }
--704 二分查找

import java.lang.annotation.Target; public class _704 { public static void main(String[] args) { _704 test=new _704(); int[] nums = new int[]{-1,0,3,5,9,12}; int target = 2; System.out.println(test.search(nums, target)); } public int search(int[] nums,int target){ if(target<nums[0] || target>nums[nums.length-1]) return -1; int low=0,high=nums.length-1; while (low<=high){ int middle=low+((high-low)>>1); if(target>nums[middle]) low=middle+1; else if(target<nums[middle]) high=middle-1; else return middle; } return -1; } }
2、移除元素
--026双指针法删除重复元素

package RemoveElement; public class _026 { public static void main(String[] args) { _026 v = new _026(); int[] arr = new int[]{0,0,1,1,1,2,2,3,3,4}; System.out.println(v.removeDuplicates(arr)); } /** * 双指针法删除重复元素 * @param nums * @return */ public int removeDuplicates(int[] nums) { int slowIndex=1; for(int fastIndex=1;fastIndex<nums.length;fastIndex++){ if(nums[fastIndex]!=nums[fastIndex-1]){ nums[slowIndex]=nums[fastIndex]; slowIndex++; } } return slowIndex; } }
--027双指针法原地删除目标元素

package RemoveElement; public class _027 { public static void main(String[] args) { int[] arr=new int[]{3,2,2,3}; _027 v = new _027(); System.out.println(v.removeElement(arr, 3)); } /** * 双指针法原地删除目标元素 * @param nums * @param val * @return */ 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; } }
--283将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序

package RemoveElement; public class _283 { public static void main(String[] args) { _283 v = new _283(); int[] arr = new int[]{0,1,0,3,12}; v.moveZeroes(arr); for (int i : arr) { System.out.println(i); } } /** * 移动零 * 将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序 * @param nums */ public void moveZeroes(int[] nums) { int slowIndex=0; for(int fastIndex=0; fastIndex< nums.length; fastIndex++){ if(nums[fastIndex]!=0){ swap(nums, slowIndex, fastIndex); slowIndex++; } } } public void swap(int[] nums, int left, int right){ int temp=nums[left]; nums[left]=nums[right]; nums[right]=temp; } }
--977有序数组的平方

package RemoveElement; import java.lang.reflect.Array; import java.util.Arrays; public class _977 { public static void main(String[] args) { int[] arr = {-4,-1,0,3,10}; _977 v = new _977(); int[] ans=v.sortedSquares(arr); for (int i : ans) { System.out.println(i); } System.out.println(); ans=v.sortedSquares1(arr); for (int an : ans) { System.out.println(an); } } /** * 有序数组的平方 * 给你一个按 非递减顺序 排序的整数数组 nums,返回 每个数字的平方 组成的新数组,要求也按 非递减顺序 排序。 *暴力解法 * @param nums * @return */ public int[] sortedSquares(int[] nums) { int[] arr = new int[nums.length]; for(int i=0;i< nums.length;i++){ arr[i]=nums[i]*nums[i]; } Arrays.sort(arr); return arr; } /** * 有序数组的平方 * 数组原本是有序的,只不过负数平方之后可能成为最大数了 * 那么数组平方的最大值就在数组的两端,不是最左边就是最右边,不可能是中间 * 用双指针指向数组的两端,必定能找到平方后的最大值,放到新数组末尾,之后不断向中间移动 * @param nums * @return */ public int[] sortedSquares1(int[] nums) { int[] ans = new int[nums.length]; //从两端开始遍历,找到平方最大元素放到新数组末尾,之后不断向中间移动 for(int i=0,j= nums.length-1,pos= nums.length-1;i<=j;){ if(nums[i]*nums[i]>nums[j]*nums[j]){ ans[pos]=nums[i]*nums[i]; i++; } else { ans[pos]=nums[j]*nums[j]; j--; } pos--; } return ans; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)