【LeetCode】 Registro personal de Sama_49

Inserte la descripción de la imagen aquí

>>> 不要一个个斜行去判断,而是一个个横行去判断
class Solution {
    
    
    public boolean isToeplitzMatrix(int[][] matrix) {
    
    
        for(int i = 1; i < matrix.length; i++) {
    
    
            for(int j = 1; j < matrix[0].length; j++) {
    
    
                if(matrix[i][j] != matrix[i - 1][j - 1]) {
    
    
                    return false;
                }
            }
        }
        return true;
    }
}

 
 
 

Inserte la descripción de la imagen aquí

>>> 对于一个字符串,要求子串的所有字母不能少于k次,那么原先少于k次的字母就能做"分隔符"
>>> 对于分割后得到的若干子串,原先大于等于k次的字母可能会变得不符合,因此对于每个子串,需重新统计
>>> 这显然是一个"分治/递归"的思想
class Solution {
    
    
    public int longestSubstring(String s, int k) {
    
    
    	// 统计字母频次
        Map<Character, Integer> map = new HashMap<>();
        for(Character c : s.toCharArray()) {
    
    
            map.put(c, map.getOrDefault(c, 0) + 1);
        }
        // 分割并递归
        for(Character c : map.keySet()) {
    
    
            if(map.get(c) < k) {
    
    
                int res = 0;
                for(String t : s.split(c.toString())) {
    
    
                    res = Math.max(res, longestSubstring(t, k));
                }
                return res;
            }
        }
        return s.length();
    }
}

 
 
 

Inserte la descripción de la imagen aquí

>>> 二维前缀和
class NumMatrix {
    
    

    private int[][] preSum;

    public NumMatrix(int[][] matrix) {
    
    
        int m = matrix.length;
        int n = matrix[0].length;
        preSum = new int[m + 1][n + 1];
        for(int i = 0; i < m; i++) {
    
    
            for(int j = 0; j < n; j++) {
    
    
                preSum[i + 1][j + 1] = matrix[i][j] + preSum[i + 1][j] + preSum[i][j + 1] - preSum[i][j];
            }
        }
    }

    public int sumRegion(int row1, int col1, int row2, int col2) {
    
    
        return preSum[row2 + 1][col2 + 1] - preSum[row1][col2 + 1] - preSum[row2 + 1][col1] + preSum[row1][col1];
    }
}

 
 
 
 

 
 
 
 

 
 
 
 

FIN FIN E N D

Supongo que te gusta

Origin blog.csdn.net/m0_46202073/article/details/113930070
Recomendado
Clasificación