
LeetCode 17. 九键字母 3:30
LeetCode 79. 二维数组查找单词 17:27
LeetCode 49. 全排列 32:50
LeetCode 50. 全排列2 45:0
LeetCode 78. 子集 59:41
LeetCode 90. 子集2 68:03
LeetCode 216. 三个数的和 75:41
LeetCode 52.N皇后问题2 87:52
LeetCode 37.数独 103:46
17. 电话号码的字母组合
难度中等1114
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
示例 1:
输入:digits = “23”
输出:[“ad”,”ae”,”af”,”bd”,”be”,”bf”,”cd”,”ce”,”cf”]
示例 2:
输入:digits = “”
输出:[]
示例 3:
输入:digits = “2”
输出:[“a”,”b”,”c”]
解法1:
class Solution {public List<String> letterCombinations(String digits) {if(digits.isEmpty()) return new ArrayList<>();String[] map ={"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};List<String> states = new ArrayList<String>();states.add("");for(char u:digits.toCharArray()){List<String> now = new ArrayList<String>();for(char c:map[u-'2'].toCharArray())for(String s:states)now.add(s+c+"");states=now;}return states;}}
79. 单词搜索
给定一个二维网格和一个单词,找出该单词是否存在于网格中。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
示例:
board =
[
[‘A’,’B’,’C’,’E’],
[‘S’,’F’,’C’,’S’],
[‘A’,’D’,’E’,’E’]
]
给定 word = “ABCCED“, 返回 true
给定 word = “SEE“, 返回 true
给定 word = “ABCB“, 返回 false
class Solution {int n,m;int[] dx={-1,0,1,0},dy={0,1,0,-1};public boolean exist(char[][] board, String word) {if(board.length==0||board[0].length==0) return false;n=board.length;m=board[0].length;for(int i=0;i<n;i++)for(int j=0;j<m;j++)if(dfs(board,i,j,word,0))return true;return false;}public boolean dfs(char[][] board,int x,int y,String word,int u){if(board[x][y]!=word.charAt(u)) return false;if(u==word.length()-1) return true;board[x][y]='.';for(int i=0;i<4;i++){int a=x+dx[i],b=y+dy[i];if(a>=0&&a<n&&b>=0&&b<m)if(dfs(board,a,b,word,u+1))return true;}board[x][y]=word.charAt(u);return false;}}
46. 全排列
给定一个 没有重复 数字的序列,返回其所有可能的全排列。
示例:
输入: [1,2,3]
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
class Solution {int n;List<Integer> path=new ArrayList<>();List<List<Integer>> ans=new ArrayList<>();boolean[] st;public List<List<Integer>> permute(int[] nums) {n=nums.length;st=new boolean[n];dfs(nums,0);return ans;}void dfs(int[] nums,int u){if(u==n){ans.add(new ArrayList<>(path));return;}for(int i=0;i<n;i++){if(!st[i]){st[i]=true;path.add(nums[i]);dfs(nums,u+1);path.remove(path.size()-1);st[i]=false;}}}}
78. 子集
给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。
解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。
示例 1:
输入:nums = [1,2,3]
输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
示例 2:
输入:nums = [0]
输出:[[],[0]]
class Solution {public List<List<Integer>> subsets(int[] nums) {List<List<Integer>> res=new ArrayList<>();for(int i=0;i<1<<nums.length;i++){List<Integer> now=new ArrayList<>();for(int j=0;j<nums.length;j++){if((i>>j&1)==1)now.add(nums[j]);}res.add(now);}return res;}}
