题目链接
    image.png

    1. class Solution {
    2. public int findLengthOfLCIS(int[] nums) {
    3. // return findLengthOfLCIS1(nums);
    4. return findLengthOfLCIS2(nums);
    5. }
    6. // 1.贪心算法。速度2ms
    7. public int findLengthOfLCIS1(int[] nums) {
    8. int start = 0;
    9. int max = 1;
    10. for(int i = 1; i < nums.length; i++) {
    11. if(nums[i] <= nums[i-1]) {
    12. start = i;
    13. }
    14. max = Math.max(max, i-start+1); // 两边植树取中间
    15. }
    16. return max;
    17. }
    18. // 2.栈。速度 8ms或7ms
    19. public int findLengthOfLCIS2(int[] nums) {
    20. Stack<Integer> stack = new Stack<>();
    21. int max = 0;
    22. for(int i = 0; i < nums.length; i++) {
    23. while(!stack.isEmpty() && stack.peek() >= nums[i]) {
    24. stack = new Stack<>();
    25. }
    26. stack.push(nums[i]);
    27. max = Math.max(max, stack.size());
    28. }
    29. return max;
    30. }
    31. }