【LeetCode】Sama的个人记录_52

在这里插入图片描述

	 	 Left				Right
		 -----------------------
 Top	|	|	|	|	|	|	|
		 -----------------------
		|	|	|	|	|	|	|
		 -----------------------
Bottom	|	|	|	|	|	|	|
		 -----------------------
class Solution {
    
    
    public List<Integer> spiralOrder(int[][] matrix) {
    
    
        List<Integer> res = new ArrayList<>();
        if (matrix == null || matrix.length == 0) {
    
    
            return res;
        }

        int L = 0;
        int R = matrix[0].length - 1;
        int T = 0;
        int B = matrix.length- 1;

        int n = matrix.length * matrix[0].length;
        while (n > 0) {
    
    
            for (int i = L; i <= R && n > 0; i++) {
    
    
                res.add(matrix[T][i]);
                n--;
            }
            T++;
            for (int i = T; i <= B && n > 0; i++) {
    
    
                res.add(matrix[i][R]);
                n--;
            }
            R--;
            for (int i = R; i >= L && n > 0; i--) {
    
    
                res.add(matrix[B][i]);
                n--;
            }
            B--;
            for (int i = B; i >= T && n > 0; i--) {
    
    
                res.add(matrix[i][L]);
                n--;
            }
            L++;
        }
        return res;
    }
}

 
 
 
在这里插入图片描述

>>> 经典的"字符串匹配"问题,用"动态规划"解决
>>> 通常dp[i][j]的含义: 的是一个串从0-i的部分,能否与另一个串0-j的部分匹配(或者是匹配的个数)
b a b g b a g
b 1 1 2 3 3 3 3
a 0 1 1 1 1 4 4
g 0 0 0 1 1 1 5
class Solution {
    
    
    public int numDistinct(String s, String t) {
    
    
        int m = s.length();
        int n = t.length();
        if (m < n) {
    
    
            return 0;
        }
        // 初始化
        int[][] dp = new int[n][m];
        dp[0][0] = s.charAt(0) == t.charAt(0) ? 1 : 0;
        for (int i = 1; i < m; i++) {
    
    
            dp[0][i] = dp[0][i - 1] + (s.charAt(i) == t.charAt(0) ? 1 : 0);
        }
        // 动态规划
        for (int i = 1; i < n; i++) {
    
    
            for (int j = 1; j < m; j++) {
    
    
                if (s.charAt(j) == t.charAt(i)) {
    
    
                    dp[i][j] =  dp[i - 1][j - 1] + dp[i][j - 1];
                } else {
    
    
                    dp[i][j] = dp[i][j - 1];
                }
            }
        }
        return dp[n - 1][m - 1];
    }
}

 
 
 

在这里插入图片描述

>>> 不断取最后一位并右移
public class Solution {
    
    
    public int hammingWeight(int n) {
    
    
        int res = 0;
        while (n != 0) {
    
    
            res += (n & 1);
            n >>>= 1;
            // 注意>>>无符号右移
        }
        return res;
    }
}
>>> 各语言中bigCount源码的实现(这篇博客解析的比较清晰明了:https://blog.csdn.net/cor_twi/article/details/53720640)
class Solution {
    
    
public:
	int hammingWeight(uint32_t n) {
    
    
		n = (n & 0x55555555) + ((n >> 1) & 0x55555555);
		n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
		n = (n & 0x0f0f0f0f) + ((n >> 4) & 0x0f0f0f0f);
		n = (n & 0x00ff00ff) + ((n >> 8) & 0x00ff00ff);
		n = (n & 0x0000ffff) + ((n >> 16) & 0x0000ffff);
		return n;
	}
};

猜你喜欢

转载自blog.csdn.net/m0_46202073/article/details/114868777