网站进入百度观察期,临沂做网站的在哪里,有没有一个网站做黄油视频,邯郸专业做网站报价给定一个整型数组, 你的任务是找到所有该数组的递增子序列#xff0c;递增子序列的长度至少是2。
示例:
输入: [4, 6, 7, 7]
输出: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]]说明:
给定数组的长度不会超过15。
数组中的整数范围是 [-…给定一个整型数组, 你的任务是找到所有该数组的递增子序列递增子序列的长度至少是2。
示例:
输入: [4, 6, 7, 7]
输出: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]]说明:
给定数组的长度不会超过15。
数组中的整数范围是 [-100,100]。
给定数组中可能包含重复数字相等的数字应该被视为递增的一种情况。和一般的去重不一致的是由于不能对数组排序,所以去重数组不能使用,同样是对同一树层的元素进行去重,而不是对树枝中元素去重 public class NonDecreasingSubsequences {public static void main(String[] args) {Solution solution new NonDecreasingSubsequences().new Solution();}//leetcode submit region begin(Prohibit modification and deletion)class Solution {ListListInteger ans new ArrayList();ListInteger path new ArrayList();public ListListInteger findSubsequences(int[] nums) {fun(nums, 0);return ans;}public void fun(int[] nums, int startIndex) {//和一般的回溯算法不一样这里不需要return ,特殊的不同if (path.size() 1) {ans.add(new ArrayList(path));}HashSetInteger hashSet new HashSet();for (int i startIndex; i nums.length; i) {// 两个条件的 或判断 条件1 非空递增 条件2同一树层的要排重 前面的计算过后面的同值元素 就略过 if((!path.isEmpty() path.get(path.size() -1 ) nums[i] )|| hashSet.contains(nums[i])){continue;}//hashSet 是记录本层元素是否重复使用新的一层uset都会重新定义清空所以要知道hashSet 只负责本层hashSet.add(nums[i]);path.add(nums[i]);fun(nums, i 1);path.remove(path.size() - 1);}}}}