Java算法练习5
- 1.8 [268. 丢失的数字](https://leetcode.cn/problems/missing-number/)
- 1.9 [383. 赎金信](https://leetcode.cn/problems/ransom-note/)
- 1.10 [2133. 检查是否每一行每一列都包含全部整数](https://leetcode.cn/problems/check-if-every-row-and-column-contains-all-numbers/)
- 1.11 [599. 两个列表的最小索引总和](https://leetcode.cn/problems/minimum-index-sum-of-two-lists/)
- 1.12 [36. 有效的数独](https://leetcode.cn/problems/valid-sudoku/)
- 1.13 [17. 电话号码的字母组合](https://leetcode.cn/problems/letter-combinations-of-a-phone-number/)
- 1.14 [37. 解数独](https://leetcode.cn/problems/sudoku-solver/)
1.8 268. 丢失的数字
给定一个包含
[0, n]
中n
个数的数组nums
,找出[0, n]
这个范围内没有出现在数组中的那个数。示例 1:
输入:nums = [3,0,1] 输出:2 解释:n = 3,因为有 3 个数字,所以所有的数字都在范围 [0,3] 内。2 是丢失的数字,因为它没有出现在 nums 中。
示例 2:
输入:nums = [0,1] 输出:2 解释:n = 2,因为有 2 个数字,所以所有的数字都在范围 [0,2] 内。2 是丢失的数字,因为它没有出现在 nums 中。
示例 3:
输入:nums = [9,6,4,2,3,5,7,0,1] 输出:8 解释:n = 9,因为有 9 个数字,所以所有的数字都在范围 [0,9] 内。8 是丢失的数字,因为它没有出现在 nums 中。
示例 4:
输入:nums = [0] 输出:1 解释:n = 1,因为有 1 个数字,所以所有的数字都在范围 [0,1] 内。1 是丢失的数字,因为它没有出现在 nums 中。
class Solution {public int missingNumber(int[] nums) {int n = nums.length;Set<Integer> set = new HashSet<Integer>();for(int i = 0;i< n;i++){set.add(nums[i]);}int missNum = -1;for(int j = 0;j <= n; j++){if(!set.contains(j)){missNum = j;break;}}return missNum;}
}
1.9 383. 赎金信
给你两个字符串:
ransomNote
和magazine
,判断ransomNote
能不能由magazine
里面的字符构成。如果可以,返回
true
;否则返回false
。
magazine
中的每个字符只能在ransomNote
中使用一次。示例 1:
输入:ransomNote = "a", magazine = "b" 输出:false
示例 2:
输入:ransomNote = "aa", magazine = "ab" 输出:false
示例 3:
输入:ransomNote = "aa", magazine = "aab" 输出:true
class Solution {public boolean canConstruct(String ransomNote, String magazine) {int[] s = new int[26];for(char c : magazine.toCharArray()){s[c-'a']++;}for(char c : ransomNote.toCharArray()){s[c-'a']--;if(s[c-'a'] < 0){return false;}}return true;}
}
1.10 2133. 检查是否每一行每一列都包含全部整数
对一个大小为
n x n
的矩阵而言,如果其每一行和每一列都包含从1
到n
的 全部 整数(含1
和n
),则认为该矩阵是一个 有效 矩阵。给你一个大小为
n x n
的整数矩阵matrix
,请你判断矩阵是否为一个有效矩阵:如果是,返回true
;否则,返回false
。示例 1:
输入:matrix = [[1,2,3],[3,1,2],[2,3,1]] 输出:true 解释:在此例中,n = 3 ,每一行和每一列都包含数字 1、2、3 。 因此,返回 true 。
示例 2:
输入:matrix = [[1,1,1],[1,2,3],[1,2,3]] 输出:false 解释:在此例中,n = 3 ,但第一行和第一列不包含数字 2 和 3 。 因此,返回 false 。
class Solution {public boolean checkValid(int[][] matrix) {int n = matrix.length;for(int i = 0;i < n ; i++){Set<Integer> s1 = new HashSet();Set<Integer> s2 = new HashSet();for(int j = 0;j < n ; j++){s1.add( matrix[j][i]);s2.add( matrix[i][j]);}if(s1.size() != n || s2.size() != n){return false;}}return true;}
}
1.11 599. 两个列表的最小索引总和
假设 Andy 和 Doris 想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示。
你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅。 如果答案不止一个,则输出所有答案并且不考虑顺序。 你可以假设答案总是存在。
示例 1:
输入: list1 = ["Shogun", "Tapioca Express", "Burger King", "KFC"],list2 = ["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"] 输出: ["Shogun"] 解释: 他们唯一共同喜爱的餐厅是“Shogun”。
示例 2:
输入:list1 = ["Shogun", "Tapioca Express", "Burger King", "KFC"],list2 = ["KFC", "Shogun", "Burger King"] 输出: ["Shogun"] 解释: 他们共同喜爱且具有最小索引和的餐厅是“Shogun”,它有最小的索引和1(0+1)。
class Solution {public String[] findRestaurant(String[] list1, String[] list2) {Map<String,Integer> map = new HashMap();for(int i = 0;i < list1.length;i++){map.put(list1[i],i);}List<String> list = new ArrayList();int minIndex = 2000;for(int i = 0;i < list2.length;i++){if(map.containsKey(list2[i])){int index = map.get(list2[i]) + i;if(index < minIndex){list.clear();minIndex = index;list.add(list2[i]);}else if(index == minIndex){list.add(list2[i]);}}}return list.toArray(new String[list.size()]);}
}
1.12 36. 有效的数独
请你判断一个
9 x 9
的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效即可。
- 数字
1-9
在每一行只能出现一次。- 数字
1-9
在每一列只能出现一次。- 数字
1-9
在每一个以粗实线分隔的3x3
宫内只能出现一次。(请参考示例图)注意:
- 一个有效的数独(部分已被填充)不一定是可解的。
- 只需要根据以上规则,验证已经填入的数字是否有效即可。
- 空白格用
'.'
表示。示例 1:
输入:board = [["5","3",".",".","7",".",".",".","."] ,["6",".",".","1","9","5",".",".","."] ,[".","9","8",".",".",".",".","6","."] ,["8",".",".",".","6",".",".",".","3"] ,["4",".",".","8",".","3",".",".","1"] ,["7",".",".",".","2",".",".",".","6"] ,[".","6",".",".",".",".","2","8","."] ,[".",".",".","4","1","9",".",".","5"] ,[".",".",".",".","8",".",".","7","9"]] 输出:true
示例 2:
输入:board = [["8","3",".",".","7",".",".",".","."] ,["6",".",".","1","9","5",".",".","."] ,[".","9","8",".",".",".",".","6","."] ,["8",".",".",".","6",".",".",".","3"] ,["4",".",".","8",".","3",".",".","1"] ,["7",".",".",".","2",".",".",".","6"] ,[".","6",".",".",".",".","2","8","."] ,[".",".",".","4","1","9",".",".","5"] ,[".",".",".",".","8",".",".","7","9"]] 输出:false 解释:除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。
class Solution {public boolean isValidSudoku(char[][] board) {int[][] rows = new int[9][9];int[][] columns = new int[9][9];int[][][] subboxes = new int[3][3][9];for(int i = 0;i < 9 ;i++){for(int j = 0;j < 9 ;j++){char c = board[i][j];if(c!='.'){int index = c - '1';rows[i][index]++;columns[j][index]++;subboxes[i / 3][j / 3 ][index]++;if(rows[i][index] >1 || columns[j][index] >1 ||subboxes[i / 3][j / 3 ][index]>1){return false;}}}}return true;}
}
1.13 17. 电话号码的字母组合
给定一个仅包含数字
2-9
的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
示例 1:
输入:digits = "23" 输出:["ad","ae","af","bd","be","bf","cd","ce","cf"]
示例 2:
输入:digits = "" 输出:[]
示例 3:
输入:digits = "2" 输出:["a","b","c"]
class Solution {String[] letterMap = {" "," ","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};List<String> list = new ArrayList();public List<String> letterCombinations(String digits) {if(digits.equals(""))return list;findCombination(digits, 0, new StringBuilder());return list;}private void findCombination(String digits, int index, StringBuilder s){if(index == digits.length()){list.add(s.toString());return;}char c = digits.charAt(index);String letters = letterMap[c - '0'];for(int i = 0 ; i < letters.length() ; i++){findCombination(digits, index+1, s.append(letters.charAt(i)));s.deleteCharAt(s.length()-1);}}}
1.14 37. 解数独
编写一个程序,通过填充空格来解决数独问题。
数独的解法需 遵循如下规则:
- 数字
1-9
在每一行只能出现一次。- 数字
1-9
在每一列只能出现一次。- 数字
1-9
在每一个以粗实线分隔的3x3
宫内只能出现一次。(请参考示例图)数独部分空格内已填入了数字,空白格用
'.'
表示。示例 1:
输入:board = [["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]] 输出:[["5","3","4","6","7","8","9","1","2"],["6","7","2","1","9","5","3","4","8"],["1","9","8","3","4","2","5","6","7"],["8","5","9","7","6","1","4","2","3"],["4","2","6","8","5","3","7","9","1"],["7","1","3","9","2","4","8","5","6"],["9","6","1","5","3","7","2","8","4"],["2","8","7","4","1","9","6","3","5"],["3","4","5","2","8","6","1","7","9"]] 解释:输入的数独如上图所示,唯一有效的解决方案如下所示:
class Solution {public void solveSudoku(char[][] board) {solve(board);}public boolean solve(char[][] board){for(int i = 0;i < 9;i++){for(int j = 0;j < 9;j++){if(board[i][j] != '.'){continue;}for(char k ='1' ;k <= '9';k++){if(isVailSudoku(i,j,k,board)){board[i][j] = k;if(solve(board)){return true;}else{board[i][j] = '.';}}}return false;}}return true;}public boolean isVailSudoku(int row, int col,char val,char[][] board){for(int i = 0;i < 9;i++){if(board[row][i] == val) return false;}for(int i = 0;i < 9;i++){if(board[i][col] == val) return false;}int startRow = (row / 3) * 3; int startCol = (col / 3) * 3;for (int i = startRow; i < startRow + 3; i++){for (int j = startCol; j < startCol + 3; j++){if (board[i][j] == val){return false;}}}return true;}
}