Leedcode算法专题训练(数组与矩阵)

1. 把数组中的 0 移到末尾

283. Move Zeroes (Easy)

Leetcode / 力扣

class Solution {
    public void moveZeroes(int[] nums) {
        int id=0;
        for(int num:nums){
            if(num!=0)nums[id++]=num;
        }
        while(id<nums.length){
            nums[id++]=0;
        }
    }
}

跨计算节点

2. 改变矩阵维度

566. Reshape the Matrix (Easy)

Leetcode / 力扣

class Solution {
    public int[][] matrixReshape(int[][] nums, int r, int c) {
        int m=nums.length;
        int n=nums[0].length;
        if(m*n!=r*c)return nums;
        int[][] res=new int[r][c];
        int idx=0;
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                res[i][j]=nums[idx/n][idx%n];
                idx++;
            }
        }
        return res;
    }
}

3. 找出数组中最长的连续 1

485. Max Consecutive Ones (Easy)

Leetcode / 力扣

class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        int res=0;
        int count=0;
        for(int i=0;i<nums.length;i++){
            if(nums[i]==1){
                count++;
                res=Math.max(res,count); 
            }
            else count=0;
        }
        return res;
    }
}

4. 有序矩阵查找

240. Search a 2D Matrix II (Medium)

Leetcode / 力扣

[
   [ 1,  5,  9],
   [10, 11, 13],
   [12, 13, 15]
]
class Solution {
    public int[][] matrixReshape(int[][] nums, int r, int c) {
        int m=nums.length;
        int n=nums[0].length;
        if(m*n!=r*c)return nums;
        int[][] res=new int[r][c];
        int idx=0;
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                res[i][j]=nums[idx/n][idx%n];
                idx++;
            }
        }
        return res;
    }
}

5. 有序矩阵的 Kth Element

378. Kth Smallest Element in a Sorted Matrix ((Medium))

Leetcode / 力扣

class Solution {
    public int kthSmallest(int[][] matrix, int k) {
        int m=matrix.length,n=matrix[0].length;
        int lo=matrix[0][0],hi=matrix[m-1][n-1];
        while(lo<=hi){
            int mid=lo+(hi-lo)/2;
            int cnt=0;
            for(int i=0;i<m;i++){
                for(int j=0;j<n && matrix[i][j]<=mid;j++){
                    cnt++;
                }
            }
            if(cnt<k)lo=mid+1;
            else hi=mid-1;
        }
        return lo;
    }
}

6. 一个数组元素在 [1, n] 之间,其中一个数被替换为另一个数,找出重复的数和丢失的数

645. Set Mismatch (Easy)

Leetcode / 力扣

class Solution {
    public int[] findErrorNums(int[] nums) {
        HashMap<Integer,Integer>map=new HashMap<>();
        int[] arr=new int[2];
        for(int n:nums){
            map.put(n,map.getOrDefault(n,0)+1);
        }
        for(int i=1;i<=nums.length;i++){
            if(map.containsKey(i)){
                if(map.get(i)==2)arr[0]=i;
            }
            else arr[1]=i;
        }
        return arr;
    }
}

7. 找出数组中重复的数,数组值在 [1, n] 之间

287. Find the Duplicate Number (Medium)

Leetcode / 力扣

要求不能修改数组,也不能使用额外的空间。

二分查找解法:

class Solution {
    public int findDuplicate(int[] nums) {
        int l=1,h=nums.length-1;
        while(l<=h){
            int mid=l+(h-l)/2;
            int cnt=0;
            for(int i=0;i<nums.length;i++){
                if(nums[i]<=mid)cnt++;
            }
            if(cnt>mid)h=mid-1;
            else l=mid+1;
        }
        return l;
    }
}

8. 数组相邻差值的个数

667. Beautiful Arrangement II (Medium)

Leetcode / 力扣

class Solution {
    public int[] constructArray(int n, int k) {
        int[] res=new int[n];
        int numk=k+1,numt=1;
        //下标段[0,k]中,偶数填充[1、2、3...]
        for(int i=0;i<=k;i+=2){
            res[i]=numt++;
        }
        //下标段[0,k]中,奇数下标填充[k+1,k,k-1...]
        for(int i=1;i<=k;i+=2){
            res[i]=numk--;
        }
        //其余的部分顺序填充
        for(int i=k+1;i<n;i++){
            res[i]=i+1;
        }
        return res;
    }
}

9. 数组的度

697. Degree of an Array (Easy)

Leetcode / 力扣

Input: [1,2,2,3,1,4,2]
Output: 6
class Solution {
    public int findShortestSubArray(int[] nums) {
        Map<Integer,Integer> left=new HashMap<>(),right=new HashMap<>(),count=new HashMap<>();
        //一个map记录左边的节点,一个map记录右边的节点,利用count进行计数
        for(int i=0;i<nums.length;i++){
            int x=nums[i];
            if(left.get(x)==null)left.put(x,i);
            right.put(x,i);
            count.put(x,count.getOrDefault(x,0)+1);
        }

        int ans=nums.length;
        int degree=Collections.max(count.values());
        for(int x: count.keySet()){
            if(count.get(x)==degree){
                ans=Math.min(ans,right.get(x)-left.get(x)+1);
            }
        }
        return ans;
    }
}

10. 对角元素相等的矩阵

766. Toeplitz Matrix (Easy)

Leetcode / 力扣

1234
5123
9512
class Solution {
    public boolean isToeplitzMatrix(int[][] matrix) {
        for(int i=0;i<matrix.length-1;i++){
            for(int j=0;j<matrix[0].length-1;j++){
                if(matrix[i][j]!=matrix[i+1][j+1]){
                    return false;
                }
            }
        }
        return true;
    }
}
  1. 如果矩阵存储在磁盘上,并且磁盘内存是有限的,因此一次最多只能将一行矩阵加载到内存中,该怎么办?
  2. 如果矩阵太大以至于只能一次将部分行加载到内存中,该怎么办?

11. 嵌套数组

565. Array Nesting (Medium)

Leetcode / 力扣

class Solution {
    public int arrayNesting(int[] nums) {
        int max=0;
        for(int i=0;i<nums.length;i++){
            int cnt=0;
            for(int j=i;nums[j]!=-1;){
                cnt++;
                int t=nums[j];
                nums[j]=-1; //标记该位置已经被访问
                j=t;
            }
            max=Math.max(max,cnt);
        }
        return max;
    }
}

12. 分隔数组

769. Max Chunks To Make Sorted (Medium)

Leetcode / 力扣

class Solution {
    public int maxChunksToSorted(int[] arr) {
        if(arr==null)return 0;
        int ret=0;
        int right=arr[0];
        for(int i=0;i<arr.length;i++){
            right=Math.max(right,arr[i]);
            if(right==i)ret++;
        }
        return ret;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值