每一步向前都是向自己的梦想更近一步,坚持不懈,勇往直前!
第一题:51. N 皇后 - 力扣(LeetCode)
class Solution {public List<List<String>> solveNQueens(int n) {List<List<String>> res = new ArrayList<>();char[][] matrix = new char[n][n];//全部赋值为'.'for(char[] row : matrix){Arrays.fill(row, '.');}traversal(0, n, matrix, res);return res;}private static void traversal(int row, int n, char[][] matrix, List<List<String>> res){//行数到了,可以放入结果了if(row == n){res.add(array2list(matrix));return;}for(int col = 0; col < n; col++){if(isValid(row, col, n, matrix)){matrix[row][col] = 'Q';traversal(row + 1, n, matrix, res);matrix[row][col] = '.';}}}private static List array2list(char[][] matrix){//结果要字符串List<String> path = new ArrayList<>();for(char[] c : matrix){path.add(String.valueOf(c));}return path;}private static boolean isValid(int row, int col, int n, char[][] matrix){//从三个纬度来验证是否符合for(int i = 0; i < row; i++){if(matrix[i][col] == 'Q'){return false;}}for(int i = row - 1, j = col - 1; i >=0 && j >= 0; i--, j--){if(matrix[i][j] == 'Q'){return false;}}for(int i = row - 1, j = col + 1; i >= 0 && j <= n - 1; i--, j++){if(matrix[i][j] == 'Q'){return false;}}return true;}
}
第二题:52. N 皇后 II - 力扣(LeetCode)
class Solution {public int totalNQueens(int n) {//注意使用数组来存储int[] res = new int[1];res[0] = 0;int[][] matrix = new int[n][n];for(int[] c : matrix){Arrays.fill(c, 0);}traversal(0, n, matrix, res);return res[0];}private static void traversal(int row, int n, int[][] matrix, int[] res){if(row == n){res[0]++;return;}for(int col = 0; col < n; col++){if(isValid(row, col, n, matrix)){matrix[row][col] = 1;traversal(row + 1, n, matrix, res);matrix[row][col] = 0;}}}private static boolean isValid(int row, int col, int n, int[][] matrix){//和上一题一样也是三个纬度for(int i = 0; i < n; i++){if(matrix[i][col] == 1){return false;}}for(int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--){if(matrix[i][j] == 1){return false;}}for(int i = row - 1, j = col + 1; i >= 0 && j <= n - 1; i--, j++){if(matrix[i][j] == 1){return false;}}return true;}
}
第三题:53. 最大子数组和 - 力扣(LeetCode)
class Solution {public int maxSubArray(int[] nums) {int n = nums.length;if(n == 1){return nums[0];}//直接使用dp来完成int[] dp = new int[n];dp[0] = nums[0];int maxsum = dp[0];for(int i = 1; i < n; i++){//注意这个状态转移方程dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]);maxsum = Math.max(maxsum, dp[i]);}return maxsum;}
}
第四题:54. 螺旋矩阵 - 力扣(LeetCode)
class Solution {public List<Integer> spiralOrder(int[][] matrix) {//老题目了 四个方向换着来呗int m = matrix.length, n = matrix[0].length;if(m == 0 || n == 0){return new ArrayList<>();}List<Integer> res = new ArrayList<>();int l = 0, r = n - 1, t = 0, b = m - 1, index = 0;while(index < m * n){for(int i = l; i <= r && index < m * n; i++){res.add(matrix[t][i]);index++;}t++;for(int i = t; i <= b && index < m * n; i++){res.add(matrix[i][r]);index++;}r--;for(int i = r; i >= l && index < m * n; i--){res.add(matrix[b][i]);index++;}b--;for(int i = b; i >= t && index < m * n; i--){res.add(matrix[i][l]);index++;}l++;}return res;}
}
第五题:55. 跳跃游戏 - 力扣(LeetCode)
class Solution {public boolean canJump(int[] nums) {//直接一个贪心过去int max_len = nums[0];for(int i = 0; i < nums.length; i++){if(i <= max_len){max_len = Math.max(max_len, i + nums[i]);}else{break;}}return max_len >= nums.length - 1 ? true : false;}
}