Merge branch 'master' of https://gitee.com/huangge1199_admin/leet-code
This commit is contained in:
commit
18f6aad6fc
@ -0,0 +1,60 @@
|
|||||||
|
//给你两个整数 left 和 right ,表示区间 [left, right] ,返回此区间内所有数字 按位与 的结果(包含 left 、right 端点)
|
||||||
|
//。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:left = 5, right = 7
|
||||||
|
//输出:4
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:left = 0, right = 0
|
||||||
|
//输出:0
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:left = 1, right = 2147483647
|
||||||
|
//输出:0
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 0 <= left <= right <= 2³¹ - 1
|
||||||
|
//
|
||||||
|
// Related Topics 位运算 👍 309 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
//201:数字范围按位与
|
||||||
|
class BitwiseAndOfNumbersRange {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new BitwiseAndOfNumbersRange().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int rangeBitwiseAnd(int left, int right) {
|
||||||
|
int mov = 0;
|
||||||
|
while (left < right) {
|
||||||
|
left >>= 1;
|
||||||
|
right >>= 1;
|
||||||
|
mov++;
|
||||||
|
}
|
||||||
|
return left << mov;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
86
src/main/java/leetcode/editor/cn/CombinationSumIi.java
Normal file
86
src/main/java/leetcode/editor/cn/CombinationSumIi.java
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
//给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
|
||||||
|
//
|
||||||
|
// candidates 中的每个数字在每个组合中只能使用一次。
|
||||||
|
//
|
||||||
|
// 注意:解集不能包含重复的组合。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入: candidates = [10,1,2,7,6,1,5], target = 8,
|
||||||
|
//输出:
|
||||||
|
//[
|
||||||
|
//[1,1,6],
|
||||||
|
//[1,2,5],
|
||||||
|
//[1,7],
|
||||||
|
//[2,6]
|
||||||
|
//]
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入: candidates = [2,5,2,1,2], target = 5,
|
||||||
|
//输出:
|
||||||
|
//[
|
||||||
|
//[1,2,2],
|
||||||
|
//[5]
|
||||||
|
//]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 1 <= candidates.length <= 100
|
||||||
|
// 1 <= candidates[i] <= 50
|
||||||
|
// 1 <= target <= 30
|
||||||
|
//
|
||||||
|
// Related Topics 数组 回溯 👍 668 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
//40:组合总和 II
|
||||||
|
class CombinationSumIi {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new CombinationSumIi().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
|
||||||
|
Arrays.sort(candidates);
|
||||||
|
List<List<Integer>> result = new ArrayList<>();
|
||||||
|
dfs(candidates, target, 0, result, new ArrayList<>());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void dfs(int[] candidates, int target, int index, List<List<Integer>> result,
|
||||||
|
List<Integer> list) {
|
||||||
|
if (target == 0) {
|
||||||
|
result.add(new ArrayList<>(list));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (index >= candidates.length || target < 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (int i = index; i < candidates.length; i++) {
|
||||||
|
if (i - 1 < candidates.length && i > 0 && i > index && candidates[i] == candidates[i - 1]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
list.add(candidates[i]);
|
||||||
|
dfs(candidates, target - candidates[i], i + 1, result, list);
|
||||||
|
list.remove(list.size() - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,61 @@
|
|||||||
|
//给定两个单词 word1 和 word2,找到使得 word1 和 word2 相同所需的最小步数,每步可以删除任意一个字符串中的一个字符。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例:
|
||||||
|
//
|
||||||
|
// 输入: "sea", "eat"
|
||||||
|
//输出: 2
|
||||||
|
//解释: 第一步将"sea"变为"ea",第二步将"eat"变为"ea"
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 给定单词的长度不超过500。
|
||||||
|
// 给定单词中的字符只含有小写字母。
|
||||||
|
//
|
||||||
|
// Related Topics 字符串 动态规划 👍 215 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
//583:两个字符串的删除操作
|
||||||
|
class DeleteOperationForTwoStrings {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new DeleteOperationForTwoStrings().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
// public int minDistance(String word1, String word2) {
|
||||||
|
// return word1.length() + word2.length() - 2 * maxString(word1, word2, word1.length(), word2.length());
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// private int maxString(String word1, String word2, int index1, int index2) {
|
||||||
|
// if (index1 == 0 || index2 == 0) {
|
||||||
|
// return 0;
|
||||||
|
// } else if (word1.charAt(index1 - 1) == word2.charAt(index2 - 1)) {
|
||||||
|
// return 1 + maxString(word1, word2, index1 - 1, index2 - 1);
|
||||||
|
// } else {
|
||||||
|
// return Math.max(maxString(word1, word2, index1 - 1, index2), maxString(word1, word2, index1, index2 - 1));
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
public int minDistance(String word1, String s2) {
|
||||||
|
int[][] dp = new int[word1.length() + 1][s2.length() + 1];
|
||||||
|
for (int i = 0; i <= word1.length(); i++) {
|
||||||
|
for (int j = 0; j <= s2.length(); j++) {
|
||||||
|
if (i == 0 || j == 0) continue;
|
||||||
|
if (word1.charAt(i - 1) == s2.charAt(j - 1)) dp[i][j] = 1 + dp[i - 1][j - 1];
|
||||||
|
else dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return word1.length() + s2.length() - 2 * dp[word1.length()][s2.length()];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
65
src/main/java/leetcode/editor/cn/GenerateParentheses.java
Normal file
65
src/main/java/leetcode/editor/cn/GenerateParentheses.java
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
//数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。
|
||||||
|
//
|
||||||
|
// 有效括号组合需满足:左括号必须以正确的顺序闭合。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:n = 3
|
||||||
|
//输出:["((()))","(()())","(())()","()(())","()()()"]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:n = 1
|
||||||
|
//输出:["()"]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 1 <= n <= 8
|
||||||
|
//
|
||||||
|
// Related Topics 字符串 动态规划 回溯 👍 1970 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
//22:括号生成
|
||||||
|
class GenerateParentheses {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new GenerateParentheses().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public List<String> generateParenthesis(int n) {
|
||||||
|
List<String> result = new ArrayList<>();
|
||||||
|
dfs(n,0,"",result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void dfs(int left, int right, String str, List<String> result) {
|
||||||
|
if (left == 0 && right == 0) {
|
||||||
|
result.add(str);
|
||||||
|
}
|
||||||
|
if (left > 0) {
|
||||||
|
dfs(left - 1, right + 1, str + "(", result);
|
||||||
|
}
|
||||||
|
if (right > 0) {
|
||||||
|
dfs(left, right - 1, str + ")", result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
52
src/main/java/leetcode/editor/cn/IntegerBreak.java
Normal file
52
src/main/java/leetcode/editor/cn/IntegerBreak.java
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
//给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化。 返回你可以获得的最大乘积。
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
// 输入: 2
|
||||||
|
//输出: 1
|
||||||
|
//解释: 2 = 1 + 1, 1 × 1 = 1。
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
// 输入: 10
|
||||||
|
//输出: 36
|
||||||
|
//解释: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36。
|
||||||
|
//
|
||||||
|
// 说明: 你可以假设 n 不小于 2 且不大于 58。
|
||||||
|
// Related Topics 数学 动态规划 👍 572 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
//343:整数拆分
|
||||||
|
class IntegerBreak {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new IntegerBreak().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int integerBreak(int n) {
|
||||||
|
if (n == 2) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (n == 3) {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
int ans = 1;
|
||||||
|
int x = n / 3;
|
||||||
|
int y = n % 3;
|
||||||
|
if (y == 0) {
|
||||||
|
ans *= (int) Math.pow(3, x);
|
||||||
|
} else if (y == 1) {
|
||||||
|
ans *= (int) Math.pow(3, x - 1) * 4;
|
||||||
|
} else {
|
||||||
|
ans *= (int) Math.pow(3, x) * 2;
|
||||||
|
}
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
72
src/main/java/leetcode/editor/cn/JumpGame.java
Normal file
72
src/main/java/leetcode/editor/cn/JumpGame.java
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
//给定一个非负整数数组 nums ,你最初位于数组的 第一个下标 。
|
||||||
|
//
|
||||||
|
// 数组中的每个元素代表你在该位置可以跳跃的最大长度。
|
||||||
|
//
|
||||||
|
// 判断你是否能够到达最后一个下标。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [2,3,1,1,4]
|
||||||
|
//输出:true
|
||||||
|
//解释:可以先跳 1 步,从下标 0 到达下标 1, 然后再从下标 1 跳 3 步到达最后一个下标。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [3,2,1,0,4]
|
||||||
|
//输出:false
|
||||||
|
//解释:无论怎样,总会到达下标为 3 的位置。但该下标的最大跳跃长度是 0 , 所以永远不可能到达最后一个下标。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 1 <= nums.length <= 3 * 10⁴
|
||||||
|
// 0 <= nums[i] <= 10⁵
|
||||||
|
//
|
||||||
|
// Related Topics 贪心 数组 动态规划 👍 1315 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
|
//55:跳跃游戏
|
||||||
|
class JumpGame {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new JumpGame().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public boolean canJump(int[] nums) {
|
||||||
|
boolean[] use = new boolean[nums.length];
|
||||||
|
Queue<Integer> queue = new LinkedList<>();
|
||||||
|
queue.add(0);
|
||||||
|
use[0] = true;
|
||||||
|
while (!queue.isEmpty()) {
|
||||||
|
int index = queue.poll();
|
||||||
|
if (index + nums[index] >= nums.length - 1) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
for (int i = index + 1; i <= index + nums[index]; i++) {
|
||||||
|
if (!use[i]) {
|
||||||
|
queue.add(i);
|
||||||
|
use[i] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,93 @@
|
|||||||
|
//给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
|
||||||
|
//
|
||||||
|
// 给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:digits = "23"
|
||||||
|
//输出:["ad","ae","af","bd","be","bf","cd","ce","cf"]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:digits = ""
|
||||||
|
//输出:[]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:digits = "2"
|
||||||
|
//输出:["a","b","c"]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 0 <= digits.length <= 4
|
||||||
|
// digits[i] 是范围 ['2', '9'] 的一个数字。
|
||||||
|
//
|
||||||
|
// Related Topics 哈希表 字符串 回溯 👍 1453 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
//17:电话号码的字母组合
|
||||||
|
class LetterCombinationsOfAPhoneNumber {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new LetterCombinationsOfAPhoneNumber().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public List<String> letterCombinations(String digits) {
|
||||||
|
List<String> combinations = new ArrayList<>();
|
||||||
|
if (digits.length() == 0) {
|
||||||
|
return combinations;
|
||||||
|
}
|
||||||
|
Map<Character, String> phoneMap = new HashMap<Character, String>() {{
|
||||||
|
put('2', "abc");
|
||||||
|
put('3', "def");
|
||||||
|
put('4', "ghi");
|
||||||
|
put('5', "jkl");
|
||||||
|
put('6', "mno");
|
||||||
|
put('7', "pqrs");
|
||||||
|
put('8', "tuv");
|
||||||
|
put('9', "wxyz");
|
||||||
|
}};
|
||||||
|
backtrack(combinations, phoneMap, digits, 0, new StringBuffer());
|
||||||
|
return combinations;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void backtrack(List<String> combinations, Map<Character, String> phoneMap, String digits, int index, StringBuffer combination) {
|
||||||
|
if (index == digits.length()) {
|
||||||
|
combinations.add(combination.toString());
|
||||||
|
} else {
|
||||||
|
char digit = digits.charAt(index);
|
||||||
|
String letters = phoneMap.get(digit);
|
||||||
|
int lettersCount = letters.length();
|
||||||
|
for (int i = 0; i < lettersCount; i++) {
|
||||||
|
combination.append(letters.charAt(i));
|
||||||
|
backtrack(combinations, phoneMap, digits, index + 1, combination);
|
||||||
|
combination.deleteCharAt(index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,69 @@
|
|||||||
|
//给定一个未排序的整数数组,找到最长递增子序列的个数。
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入: [1,3,5,4,7]
|
||||||
|
//输出: 2
|
||||||
|
//解释: 有两个最长递增子序列,分别是 [1, 3, 4, 7] 和[1, 3, 5, 7]。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入: [2,2,2,2,2]
|
||||||
|
//输出: 5
|
||||||
|
//解释: 最长递增子序列的长度是1,并且存在5个子序列的长度为1,因此输出5。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 注意: 给定的数组长度不超过 2000 并且结果一定是32位有符号整数。
|
||||||
|
// Related Topics 树状数组 线段树 数组 动态规划 👍 352 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
//673:最长递增子序列的个数
|
||||||
|
class NumberOfLongestIncreasingSubsequence {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new NumberOfLongestIncreasingSubsequence().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
|
||||||
|
public int findNumberOfLIS(int[] nums) {
|
||||||
|
int N = nums.length;
|
||||||
|
if (N <= 1) return N;
|
||||||
|
int[] lengths = new int[N];
|
||||||
|
int[] counts = new int[N];
|
||||||
|
Arrays.fill(counts, 1);
|
||||||
|
for (int j = 0; j < N; ++j) {
|
||||||
|
for (int i = 0; i < j; ++i) {
|
||||||
|
if (nums[i] < nums[j]) {
|
||||||
|
if (lengths[i] >= lengths[j]) {
|
||||||
|
lengths[j] = lengths[i] + 1;
|
||||||
|
counts[j] = counts[i];
|
||||||
|
} else if (lengths[i] + 1 == lengths[j]) {
|
||||||
|
counts[j] += counts[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int longest = 0, ans = 0;
|
||||||
|
for (int length : lengths) {
|
||||||
|
longest = Math.max(longest, length);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < N; ++i) {
|
||||||
|
if (lengths[i] == longest) {
|
||||||
|
ans += counts[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
103
src/main/java/leetcode/editor/cn/ShuffleAnArray.java
Normal file
103
src/main/java/leetcode/editor/cn/ShuffleAnArray.java
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
//给你一个整数数组 nums ,设计算法来打乱一个没有重复元素的数组。
|
||||||
|
//
|
||||||
|
// 实现 Solution class:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Solution(int[] nums) 使用整数数组 nums 初始化对象
|
||||||
|
// int[] reset() 重设数组到它的初始状态并返回
|
||||||
|
// int[] shuffle() 返回数组随机打乱后的结果
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入
|
||||||
|
//["Solution", "shuffle", "reset", "shuffle"]
|
||||||
|
//[[[1, 2, 3]], [], [], []]
|
||||||
|
//输出
|
||||||
|
//[null, [3, 1, 2], [1, 2, 3], [1, 3, 2]]
|
||||||
|
//
|
||||||
|
//解释
|
||||||
|
//Solution solution = new Solution([1, 2, 3]);
|
||||||
|
//solution.shuffle(); // 打乱数组 [1,2,3] 并返回结果。任何 [1,2,3]的排列返回的概率应该相同。例如,返回 [3,
|
||||||
|
//1, 2]
|
||||||
|
//solution.reset(); // 重设数组到它的初始状态 [1, 2, 3] 。返回 [1, 2, 3]
|
||||||
|
//solution.shuffle(); // 随机返回数组 [1, 2, 3] 打乱后的结果。例如,返回 [1, 3, 2]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 1 <= nums.length <= 200
|
||||||
|
// -10⁶ <= nums[i] <= 10⁶
|
||||||
|
// nums 中的所有元素都是 唯一的
|
||||||
|
// 最多可以调用 5 * 10⁴ 次 reset 和 shuffle
|
||||||
|
//
|
||||||
|
// Related Topics 数组 数学 随机化 👍 152 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
//384:打乱数组
|
||||||
|
class ShuffleAnArray {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
// Solution solution = new ShuffleAnArray().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
private int[] array;
|
||||||
|
private int[] original;
|
||||||
|
|
||||||
|
private Random rand = new Random();
|
||||||
|
|
||||||
|
private List<Integer> getArrayCopy() {
|
||||||
|
List<Integer> asList = new ArrayList<Integer>();
|
||||||
|
for (int i = 0; i < array.length; i++) {
|
||||||
|
asList.add(array[i]);
|
||||||
|
}
|
||||||
|
return asList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Solution(int[] nums) {
|
||||||
|
array = nums;
|
||||||
|
original = nums.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[] reset() {
|
||||||
|
array = original;
|
||||||
|
original = original.clone();
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[] shuffle() {
|
||||||
|
List<Integer> aux = getArrayCopy();
|
||||||
|
|
||||||
|
for (int i = 0; i < array.length; i++) {
|
||||||
|
int removeIdx = rand.nextInt(aux.size());
|
||||||
|
array[i] = aux.get(removeIdx);
|
||||||
|
aux.remove(removeIdx);
|
||||||
|
}
|
||||||
|
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Your Solution object will be instantiated and called as such:
|
||||||
|
* Solution obj = new Solution(nums);
|
||||||
|
* int[] param_1 = obj.reset();
|
||||||
|
* int[] param_2 = obj.shuffle();
|
||||||
|
*/
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
66
src/main/java/leetcode/editor/cn/Subsets.java
Normal file
66
src/main/java/leetcode/editor/cn/Subsets.java
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
//给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。
|
||||||
|
//
|
||||||
|
// 解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [1,2,3]
|
||||||
|
//输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [0]
|
||||||
|
//输出:[[],[0]]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 1 <= nums.length <= 10
|
||||||
|
// -10 <= nums[i] <= 10
|
||||||
|
// nums 中的所有元素 互不相同
|
||||||
|
//
|
||||||
|
// Related Topics 位运算 数组 回溯 👍 1284 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
//78:子集
|
||||||
|
class Subsets{
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new Subsets().new Solution();
|
||||||
|
solution.subsets(new int[]{1,2,3});
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public List<List<Integer>> subsets(int[] nums) {
|
||||||
|
List<List<Integer>> result = new ArrayList<>();
|
||||||
|
for (int i = 0; i < nums.length; i++) {
|
||||||
|
int size = result.size();
|
||||||
|
for (int j = 0; j < size; j++) {
|
||||||
|
List<Integer> list = result.get(j);
|
||||||
|
result.add(new ArrayList<>(list));
|
||||||
|
list.add(nums[i]);
|
||||||
|
}
|
||||||
|
result.add(new ArrayList<>(Collections.singletonList(nums[i])));
|
||||||
|
}
|
||||||
|
result.add(new ArrayList<>());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
76
src/main/java/leetcode/editor/cn/SubsetsIi.java
Normal file
76
src/main/java/leetcode/editor/cn/SubsetsIi.java
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
//给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。
|
||||||
|
//
|
||||||
|
// 解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [1,2,2]
|
||||||
|
//输出:[[],[1],[1,2],[1,2,2],[2],[2,2]]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [0]
|
||||||
|
//输出:[[],[0]]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 1 <= nums.length <= 10
|
||||||
|
// -10 <= nums[i] <= 10
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Related Topics 位运算 数组 回溯 👍 630 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
//90:子集 II
|
||||||
|
class SubsetsIi {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new SubsetsIi().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public List<List<Integer>> subsetsWithDup(int[] nums) {
|
||||||
|
Arrays.sort(nums);
|
||||||
|
List<List<Integer>> result = new ArrayList<>();
|
||||||
|
dfs(nums,0,new ArrayList<>(),result,new ArrayList<>());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void dfs(int[] nums, int index, List<Integer> list, List<List<Integer>> result, List<String> use) {
|
||||||
|
if (index == nums.length) {
|
||||||
|
List<Integer> temp = new ArrayList<>(list);
|
||||||
|
String key = Arrays.toString(temp.toArray());
|
||||||
|
if (!use.contains(key)) {
|
||||||
|
result.add(temp);
|
||||||
|
use.add(key);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
list.add(nums[index]);
|
||||||
|
dfs(nums, index + 1, list, result, use);
|
||||||
|
list.remove(list.size() - 1);
|
||||||
|
dfs(nums, index + 1, list, result, use);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
65
src/main/java/leetcode/editor/cn/WordBreak.java
Normal file
65
src/main/java/leetcode/editor/cn/WordBreak.java
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
//给定一个非空字符串 s 和一个包含非空单词的列表 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词。
|
||||||
|
//
|
||||||
|
// 说明:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 拆分时可以重复使用字典中的单词。
|
||||||
|
// 你可以假设字典中没有重复的单词。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
// 输入: s = "leetcode", wordDict = ["leet", "code"]
|
||||||
|
//输出: true
|
||||||
|
//解释: 返回 true 因为 "leetcode" 可以被拆分成 "leet code"。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
// 输入: s = "applepenapple", wordDict = ["apple", "pen"]
|
||||||
|
//输出: true
|
||||||
|
//解释: 返回 true 因为 "applepenapple" 可以被拆分成 "apple pen apple"。
|
||||||
|
// 注意你可以重复使用字典中的单词。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
// 输入: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
|
||||||
|
//输出: false
|
||||||
|
//
|
||||||
|
// Related Topics 字典树 记忆化搜索 哈希表 字符串 动态规划 👍 1123 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
//139:单词拆分
|
||||||
|
class WordBreak {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new WordBreak().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public boolean wordBreak(String s, List<String> wordDict) {
|
||||||
|
Set<String> wordDictSet = new HashSet(wordDict);
|
||||||
|
boolean[] dp = new boolean[s.length() + 1];
|
||||||
|
dp[0] = true;
|
||||||
|
for (int i = 1; i <= s.length(); i++) {
|
||||||
|
for (int j = 0; j < i; j++) {
|
||||||
|
if (dp[j] && wordDictSet.contains(s.substring(j, i))) {
|
||||||
|
dp[i] = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dp[s.length()];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user