Leetcode-backtracking题目总结

桃扇骨 2023-07-09 07:26 100阅读 0赞

Leetcode-78. Subsets (全组合问题)

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Input: nums = [1,2,3] Output:[[3],[1],[2],[1,2,3],[1,3],[2,3],[1,2],[]]

  1. public List<List<Integer>> subsets(int[] nums) {
  2. List<List<Integer>> list = new ArrayList<>();
  3. Arrays.sort(nums);
  4. backtrack(list, new ArrayList<>(), nums, 0);
  5. return list;
  6. }
  7. private void backtrack(List<List<Integer>> list , List<Integer> tempList, int [] nums, int start){
  8. list.add(new ArrayList<>(tempList));
  9. for(int i = start; i < nums.length; i++){
  10. tempList.add(nums[i]);
  11. backtrack(list, tempList, nums, i + 1);
  12. tempList.remove(tempList.size() - 1);
  13. }
  14. }

如果是全排列问题:

  1. public List<List<Integer>> fullsubsets(int[] nums) {
  2. List<List<Integer>> list = new ArrayList<>();
  3. Arrays.sort(nums);
  4. backtrack(list, new ArrayList<>(), nums);
  5. return list;
  6. }
  7. private void backtrack(List<List<Integer>> list , List<Integer> tempList, int [] nums){
  8. if(tempList.size() == nums.length){
  9. list.add(new ArrayList<>(tempList));
  10. return;
  11. }
  12. for(int i = 0; i < nums.length; i++){
  13. if(!tempList.contains(nums[i])){
  14. tempList.add(nums[i]);
  15. backtrack(list, tempList, nums);
  16. tempList.remove(tempList.size() - 1);
  17. }
  18. }
  19. }

Leecode-79. Word Search

Given a 2D board and a word, find if the word exists in the grid.

The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.

Example:

  1. board =
  2. [
  3. ['A','B','C','E'],
  4. ['S','F','C','S'],
  5. ['A','D','E','E']
  6. ]
  7. Given word = "ABCCED", return true.
  8. Given word = "SEE", return true.
  9. Given word = "ABCB", return false

不利用额外空间的方法:

  1. public boolean exist(char[][] board, String word) {
  2. char[] w = word.toCharArray();
  3. for (int y=0; y<board.length; y++) {
  4. for (int x=0; x<board[y].length; x++) {
  5. if (exist(board, y, x, w, 0)) return true;
  6. }
  7. }
  8. return false;
  9. }
  10. private boolean exist(char[][] board, int y, int x, char[] word, int i) {
  11. if (i == word.length) return true;
  12. if (y<0 || x<0 || y == board.length || x == board[y].length) return false;
  13. if (board[y][x] != word[i]) return false;
  14. board[y][x] ^= 256;
  15. boolean exist = exist(board, y, x+1, word, i+1)
  16. || exist(board, y, x-1, word, i+1)
  17. || exist(board, y+1, x, word, i+1)
  18. || exist(board, y-1, x, word, i+1);
  19. board[y][x] ^= 256;
  20. return exist;
  21. }

使用了额外空间的方法:

  1. boolean backtrack(char[][]board, boolean[][]visited, String word, int index, int x, int y){
  2. if(index == word.length()) return true;
  3. if(x < 0 || x >= board.length || y < 0 || y >= board[0].length || visited[x][y]) return false;
  4. if(word.charAt(index) != board[x][y]) return false;
  5. visited[x][y] = true;
  6. boolean res = backtrack (board, visited, word, index + 1, x, y + 1)
  7. || backtrack(board, visited, word, index + 1, x, y - 1)
  8. || backtrack(board, visited, word, index + 1, x + 1, y)
  9. || backtrack(board, visited, word, index + 1, x - 1, y);
  10. visited[x][y] = false;
  11. return res;
  12. }
  13. public boolean exist(char[][] board, String word) {
  14. boolean[][] visited = new boolean[board.length][board[0].length];
  15. for(int i = 0; i < board.length; ++i) {
  16. for (int j = 0; j < board[0].length; ++j) {
  17. if (backtrace(board,visited, word ,0, i, j))
  18. return true;
  19. }
  20. }
  21. return false;
  22. }

发表评论

表情:
评论列表 (有 0 条评论,100人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java基础题目总结

    有些基础题目由于工作中用的比较少但却又是不可少的,这样回答起来就会反应慢,不确定,不准确,特此开了文章记录遇到的不确定或者回答比较拗口的问题。 1.servlet是单例的吗,

    相关 面试题目总结

    马上又到了金九银十是招聘的旺季:博主这里收集了一些相应的面试题目,提供给大家参考学习!!!!!!!!当然这里也仅仅是参考,更多的知识还是自己不断的去积累学习掌握!!! St

    相关 MySQL题目总结

    1.数据库三范式是什么? 1. 第一范式(1NF):字段具有原子性,不可再分。(所有关系型数据库系统都满足第一范式数据库表中的字段都是单一属性的,不可再分) 2. 第二范