zoukankan      html  css  js  c++  java
  • 491. Increasing Subsequences 491.增加子序列

    Given an integer array, your task is to find all the different possible increasing subsequences of the given array, and the length of an increasing subsequence should be at least 2.

     

    Example:

    Input: [4, 6, 7, 7]
    Output: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]]

    和划分字符串一样的思路,就是把isPalindrome换成isIncreasing就行了
    如果出现了空串、长度为1的串,需要限制一下temp的尺寸大于等于2

    class Solution {
        public List<List<Integer>> findSubsequences(int[] nums) {
            //cc
            List<Integer> temp = new ArrayList<>();
            List<List<Integer>> result = new ArrayList<>();
            
            if (nums == null) return null;
            if (nums.length == 0) return result;
            
            dfs(nums, 0, temp, result);
            
            return result;
        }
        
        public void dfs(int[] nums, int start, List<Integer> temp, List<List<Integer>> result) {
            //控制一下size
            if (start == nums.length && temp.size() >= 2) {
                result.add(new ArrayList(temp));
            }else {
                for (int i = start; i < nums.length; i++) {
                    if (isIncreasing(nums, start, i)) {
                        //temp.add(Arrays.copyOfRange(nums, start, i));
                        for (int j = start; j < i; j++) {
                            temp.add(nums[j]);
                        }
                        dfs(nums, i + 1, temp, result);
                        
                        if (temp.size() >= 1)
                        temp.remove(temp.size() - 1);
                    }
                }
            }
        }
        
        //isIncreasing
        public boolean isIncreasing(int[] nums, int start, int end) {
            for (int i = start; i < end - 1; i++) {
                if (nums[i + 1] < nums[i])
                    return false;
            }
            return true;
        }
    }
    View Code
    
    
    
     
  • 相关阅读:
    微信小程序实战,与后台交互
    微信小程序开发笔记
    微信小程序 之页面跳转
    小程序(四):模板
    (1)Appium基础操作API
    ADB命令
    移动端测试知识概览
    计算机网络常见面试题
    pip命令
    随笔-记录一些简单内容
  • 原文地址:https://www.cnblogs.com/immiao0319/p/13228149.html
Copyright © 2011-2022 走看看