1 Star 0 Fork 0

Neo_1001/LeetCode刷题记录

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
LC_491_wrong.java 1.28 KB
一键复制 编辑 原始数据 按行查看 历史
Neo_1001 提交于 2024-04-10 22:16 . first commit
package LeetCode;
import java.util.ArrayList;
import java.util.List;
public class LC_491_wrong {
static List<List<Integer>> res = new ArrayList<>();
static ArrayList<Integer> path = new ArrayList<>();
public static void main(String[] args) {
int[] nums = {1,2,3,4,5,6,7,8,9,10,1,1,1,1,1};
findSubsequences(nums);
}
public static List<List<Integer>> findSubsequences(int[] nums) {
backtracking(nums, 0);
System.out.println(res);
return res;
}
public static void backtracking(int[] nums, int startIdx) {
if (startIdx > nums.length) return;
if (isValid(path))
res.add(new ArrayList<>(path));
for (int i = startIdx; i < nums.length; i++) {
if (i > startIdx && nums[i - 1] == nums[i]) continue;
else {
path.add(nums[i]);
backtracking(nums, i + 1);
path.remove(path.size() - 1);
}
}
}
public static boolean isValid(ArrayList<Integer> path) {
if (path.size() < 2) return false;
int[] pathNum = path.stream().mapToInt(Integer::intValue).toArray();
for (int i = 0; i < pathNum.length-1; i++) {
if(pathNum[i]>pathNum[i+1]) return false;
}
return true;
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/neo1001/leet-code-practice-record.git
git@gitee.com:neo1001/leet-code-practice-record.git
neo1001
leet-code-practice-record
LeetCode刷题记录
master

搜索帮助