当前位置:网站首页>LeetCode_ 491_ Longest increasing subsequence
LeetCode_ 491_ Longest increasing subsequence
2022-07-23 13:50:00 【Fitz1318】
Topic link
Title Description
Give you an array of integers nums , Find and return all different increasing subsequences in the array , In increasing subsequence There are at least two elements . You can press In any order Return to the answer .
The array may contain duplicate elements , If two integers are equal , It can also be regarded as a special case of increasing sequence .
Example 1:
Input :nums = [4,6,7,7]
Output :[[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
Example 2:
Input :nums = [4,4,3,2,1]
Output :[[4,4]]
Tips :
1 <= nums.length <= 15-100 <= nums[i] <= 100
Their thinking
backtracking , By way of example 1 As an example

The retrospective trilogy
- Parameters of recursive functions
startIndex:for Loop traversalpath: The results of the record meet the conditions ofans: Set of recorded results
- Termination condition of recursive function
path.size >= 2
- Single layer logic
- Here we need to pay attention to weight removal , That is, as shown in the figure , The elements used on the same layer under the same parent node can no longer be used
AC Code
class Solution {
public List<List<Integer>> findSubsequences(int[] nums) {
List<Integer> path = new ArrayList<>();
List<List<Integer>> ans = new ArrayList<>();
if (nums.length == 0) {
return ans;
}
backTracing(nums, 0, path, ans);
return ans;
}
private static void backTracing(int[] nums, int startIndex, List<Integer> path, List<List<Integer>> ans){
if (path.size() >= 2) {
ans.add(new ArrayList<>(path));
}
int[] flag = new int[201];
for (int i = startIndex; i < nums.length; i++) {
if (path.size() != 0 && nums[i] < path.get(path.size() - 1) || flag[nums[i] + 100] == 1) {
continue;
}
flag[nums[i] + 100] = 1;
path.add(nums[i]);
backTracing(nums, i + 1, path, ans);
path.remove(path.size() - 1);
}
}
}
边栏推荐
猜你喜欢

Method of entering mathematical formula into mark down document

Elephant Swap的LaaS方案优势分析,致eToken表现强势

Research on hardware architecture of Ti single chip millimeter wave radar xwr1642

docker redis

建立STM32F103C8T6工程模板和STM32 ST-LINK Utilit烧录hex文件

Introduction to radar part vii 4 SAR system design

Learn to use canvas to build line chart, bar chart and pie chart

浅谈Anroid设备的CPU类型以及so文件的放置目录

了解canvas

Client does not support authentication protocol requested by server; consider upgrading MySQL client
随机推荐
LeetCode_2341_数组能形成多少数对
[Muduo] poller abstract class
Hardware system architecture of 4D millimeter wave radar
Common mouse events and keyboard events
Special topic of MIMO Radar (0) - General Chapter
Color recognition of regions of interest in pictures and videos based on OpenCV
KingbaseES 格式化函数
docker mysql
Warcraft map editor trigger notes
Remote editing and debugging with vscode
Vs2019:constexpr function "qcountleadingzerobits" cannot generate constant expressions
欧洲“气荒”对中国有哪些影响?
Interface test - simple interface automation test demo
面试官:有了解过ReentrantLock的底层实现吗?说说看
Introduction to radar part vii 4 SAR system design
ROS中引用和输出消息类型
十大券商开户风险性大吗,安全吗?
【Ardunio】2种方法控制舵机
同花顺开户风险性大吗,安全吗?
解决MySQL向表中增加数据插入中文乱码问题