其他分享
首页 > 其他分享> > 力扣刷题——数组4

力扣刷题——数组4

作者:互联网

目录

杨辉三角

class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        for (int i = 0; i < numRows; ++i) {
            List<Integer> list1 = new ArrayList<Integer>();
            for (int j = 0; j <= i; ++j) {
                if (j == 0 || j == i) {
                    list1.add(1);
                } else {
                    list1.add(list.get(i - 1).get(j - 1) + list.get(i - 1).get(j));
                }
            }
            list.add(list1);
        }
        return list;
    }
}

杨辉三角II

class Solution {
    public List<Integer> getRow(int rowIndex) {
       List<Integer> list  = new ArrayList<>();
       for(int i = 0; i <= rowIndex; i++){
           List<Integer> list1 = new ArrayList<>();
           for(int j = 0; j <= i; j++){
               if(j == 0 || j == i){
                   list1.add(1);
               }else{
                   list1.add(list.get(j)+list.get(j-1));
               }
           }
           list = list1;
       }
       return list;
    }
}

图片平滑器

class Solution {
    public int[][] imageSmoother(int[][] img) {
        int row = img.length;
        int col = img[0].length;
        int[][] sum = new int[row][col];
        int[][] newImg = new int[row+2][col+2];
        for(int i = 1; i < row+1;i++){
            for(int j = 1; j < col + 1; j++){
                newImg[i][j] = img[i-1][j-1];
            }
        }
        for(int i = 1; i < row + 1; i++){
            for(int j = 1; j < col + 1; j++){
                sum[i-1][j-1] = (newImg[i-1][j-1]+newImg[i-1][j]+newImg[i-1][j+1]+
                        newImg[i][j-1]+newImg[i][j]+newImg[i][j+1]+
                        newImg[i+1][j-1]+newImg[i+1][j]+newImg[i+1][j+1]);
                int k = 0;
                if(i==1&&j==1||(i==1&&j==col)||(i==row&&j==1)||(i==row&&j==col)){
                    if(row == 1 && col == 1){
                        k = 1;
                    }else if(row == 1||col == 1){
                        k = 2;
                    }else{
                        k = 4;
                    }
                }else if(i==1||i==row||j==1||j==col){
                    if(row == 1|| col == 1){
                        k = 3;
                    }else{
                        k = 6;
                    }
                }else{
                    k = 9;
                }
                sum[i-1][j-1] /= k;
            }
        }
        return sum;
    }
}

范围求和

class Solution {
    public int maxCount(int m, int n, int[][] ops) {
      int res = m * n;
        if(ops.length == 0){
            return res;
        }
        int rmin = ops[0][0];
        int cmin = ops[0][1];
        for(int i = 0; i < ops.length;i++){
            if(rmin > ops[i][0]){
                rmin = ops[i][0];
            }
            if(cmin > ops[i][1]){
                cmin = ops[i][1];
            }
        }
        return rmin * cmin;
    }
}

总结

本次总结的题目主要关于的是二维数组以及滚动数组,在解决二维数组的问题时,我们优先考虑的是对二维数组规模的估量以及对二维数组边界以及一些特殊条件的优先处理;滚动数组实质上体现的是一种递归的思想,根据前面的递归出后面的,层层相连。当然,题目本身还是很简单的,哈哈,,呜呜。。希望对大家有所收获!

标签:int,newImg,list,力扣,数组,杨辉三角,刷题,col,row
来源: https://blog.csdn.net/weixin_52477733/article/details/122440726