Java | Leetcode Java题解之第491题非递减子序列

题目:

题解:

java 复制代码
class Solution {
    List<Integer> temp = new ArrayList<Integer>();
    List<List<Integer>> ans = new ArrayList<List<Integer>>();

    public List<List<Integer>> findSubsequences(int[] nums) {
        dfs(0, Integer.MIN_VALUE, nums);
        return ans;
    }

    public void dfs(int cur, int last, int[] nums) {
        if (cur == nums.length) {
            if (temp.size() >= 2) {
                ans.add(new ArrayList<Integer>(temp));
            }
            return;
        }
        if (nums[cur] >= last) {
            temp.add(nums[cur]);
            dfs(cur + 1, nums[cur], nums);
            temp.remove(temp.size() - 1);
        }
        if (nums[cur] != last) {
            dfs(cur + 1, last, nums);
        }
    }
}
相关推荐
文丑颜不良啊4 小时前
LeetCode2821~2840题目解析
java·leetcode
apcipot_rain4 小时前
C++转Java日记(10)
java·开发语言
简 洁 冬冬5 小时前
泛型方法写法全面辨析:从「能编译」到「能跑」到「能用」
java·泛型
LGL6030A5 小时前
Java学习历程31——初识Spring Boot
java·spring boot·学习
Lansonli7 小时前
大数据Spark(八十八):Spark任务资源之Standalone集群测试
java·服务器·spark
budingxiaomoli7 小时前
Spring AOP
java·spring·java-ee
特立独行的猫A7 小时前
Ionic + 仓颉语言的 cj-tauri 开发框架实战:开发精美移动端 APP
java·后端·python
Pniubi8 小时前
力扣24. 两两交换链表中的节点(递归)
算法·leetcode·链表
nice_lcj5208 小时前
LRU 缓存入门:它是什么、怎么用、面试怎么手写
java·数据结构·缓存·面试·lru
文丑颜不良啊9 小时前
LeetCode2741~2760题目解析
java·leetcode