代码拉取完成,页面将自动刷新
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;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。