学习目标:
学习SpringBoot
求解leetcode51
学习内容:
学习如何建立一个SpringBoot项目,并且完成了第一个接口的开发,了解了多种配置URL的方式,熟悉了配置文件的两种写法。
leetcode51:
public class L51 { public List<List<String>> solveNQueens(int n) { //1.n x n char[][] board = new char[n][n]; for (int i = 0; i < n; i++) { for(int j=0;j<n;j++){ board[i][j]='.'; } } List<List<String>> result = new ArrayList<>(); backtrack(result,0,n,board); return result; } public void backtrack(List<List<String>> result, int row, int n, char[][] board) { //if满足结束条件 if (row == n) { result.add(arrayToList(board)); return; } for (int col= 0; col < n; col++) { if (!isValid(row, col, board)) { continue; } board[row][col] = 'Q'; backtrack(result, row + 1, n, board); board[row][col] = '.'; } } //把数组转换成列表满足题目要求 public List<String> arrayToList(char[][] board) { List<String> list = new ArrayList<>(); for (int i = 0; i < board.length; i++) { StringBuilder builder = new StringBuilder(); for (int j = 0; j < board.length; j++) { builder.append(board[i][j]); } list.add(builder.toString()); } return list; } //判断当前位置(row,col)的皇后是否合法 public boolean isValid(int row, int col, char[][] board) { //行 for (int temCol = 0; temCol < board.length; temCol++) { if (board[row][temCol] == 'Q') { return false; } } //列 for (int tmpRow = 0; tmpRow < board.length; tmpRow++) { if (board[tmpRow][col] == 'Q') { return false; } } //45度角 for (int i = row - 1, j = col + 1; j <= board.length-1&& i >= 0; i--, j++) { if (board[i][j] == 'Q') { return false; } } //135度角 for (int i = row - 1, j = col - 1; j >= 0 && i >= 0; i--, j--) { if (board[i][j] == 'Q') { return false; } } return true; } public static void main(String[] args) { int n=7; System.out.println(new L51().solveNQueens(n)); } }
leetcode349:
import java.util.ArrayList; import java.util.TreeSet; public class L349 { public int[] intersection(int[] nums1, int[] nums2) { TreeSet<Integer> set = new TreeSet<>(); for (int num : nums1) { set.add(num); } ArrayList<Integer> list = new ArrayList<>(); for (int num : nums2) { if (set.contains(num)) { list.add(num); set.remove(num); } } int[] res = new int[list.size()]; for (int i = 0; i < list.size(); i++) { res[i] = list.get(i); } return res; } }
leetcode350:
import java.util.ArrayList; import java.util.TreeMap; public class L350 { public int[] intersect(int[] nums1, int[] nums2) { TreeMap<Integer, Integer> map = new TreeMap<>(); for (int num : nums1) { if (!map.containsKey(num)) map.put(num, 1); else map.put(num, map.get(num) + 1); } ArrayList<Integer> list = new ArrayList<>(); for (int num : nums2) { if (map.containsKey(num)) { list.add(num); map.put(num, map.get(num) - 1); if (map.get(num) == 0) { map.remove(num); } } } int[] res = new int[list.size()]; for (int i = 0; i < list.size(); i++) { res[i] = list.get(i); } return res; } }
学习时间:
08:00-10:00 11:30-12:00 18:00-19:00
学习产出:
掌握了SpringBoot的创建与配置。