数组

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才是实际要插入的位置
    }
}
View Code
复制代码

 --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;
    }
}
View Code
复制代码

--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;
    }
}
View Code
复制代码

--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;
    }
}
View Code
复制代码

 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;
    }
}
View Code
复制代码

--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;
    }
}
View Code
复制代码

--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;
    }
}
View Code
复制代码

--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;
    }
}
View Code
复制代码

 

posted @   船长华莱士  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示