关于数组常用的算法总结

33 篇文章 1 订阅
29 篇文章 3 订阅

数组

数组是存放在连续内存空间上的相同类型数据的集合。

  • 数组下标都是从0开始的。
  • 数组内存空间的地址是连续的

二分法

704. 二分查找
class Solution {
    public int search(int[] nums, int target) {
        if(target <nums[0] || target > nums[nums.length-1]) return -1;
        int left = 0;
        int right = nums.length-1;
        while(left <= right){
            int mobile = (right-left)/2+left;
            if(nums[mobile]  == target) return mobile;
            else if(nums[mobile] > target) right = mobile-1;
            else if(nums[mobile] < target) left = mobile+1;
        }
        return -1;
    }
}

双指针法

27. 移除元素 - 力扣(LeetCode) (leetcode-cn.com)

class Solution {
    public int removeElement(int[] nums, int val) {
        int slow = 0;
        
        for(int fast = 0; fast<nums.length;fast++){
            if(nums[fast] != val){
                nums[slow] = nums[fast];
                slow++;
            }
        }
        return slow;
    }
}

在数组和链表中是否常见

滑动窗口

209. 长度最小的子数组 - 力扣(LeetCode) (leetcode-cn.com)

class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int right = 0;
        int left = 0;
        int sum = 0;
        int sublentgh = Integer.MAX_VALUE;
        for(right = 0; right<nums.length;right++){
            sum += nums[right];
            while(sum>=target){
                sublentgh = Math.min(sublentgh,right-left+1);
                sum -= nums[left];
                left++;
            }
            
        }
        return sublentgh ==Integer.MAX_VALUE?0:sublentgh;
    }
}

模拟法

59. 螺旋矩阵 II - 力扣(LeetCode) (leetcode-cn.com)

class Solution {
    public int[][] generateMatrix(int n) {
        //生成一个数组
        int[][] nums = new int[n][n];
        //定义循环次数
        int loop = n/2;
        //定义开始的位置
        int startx = 0;
        int starty = 0;
        //定义偏移量
        int offest = 1;
        //定义填充的数字
        int count = 1;
        //定义中间的位置
        int mid = n/2;
        while(loop > 0){
            int i = startx;
            int j = starty;
            //模拟从左到右
            for(;j<starty+n-offest;++j){
                nums[i][j] = count;
                count++;
            }
            //从上到下
            for(;i<startx+n-offest;++i){
                nums[i][j] = count;
                count++;
            }
            //从右到左
            for(;j>starty;j--){
                nums[i][j] = count;
                count ++;
            }
            //从下到上
            for(;i>starty;i--){
                nums[i][j] = count;
                count ++;
            }
            loop --;
            startx = startx+1;
            starty=starty+1;
            offest  += 2;
            //如果是奇数需要单独赋值

        }
         if (n % 2 == 1) {
            nums[mid][mid] = count;
        }
        return nums;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小七rrrrr

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值