397 - 连续上升子序列

    xiaoxiao2021-04-19  80

    4.14

    同号的问题可以用乘法解决

    public class Solution { /** * @param A an array of Integer * @return an integer */ public static int longestIncreasingContinuousSubsequence(int[] A) { if(A.length <=1){// Write your code here return A.length; } int max = 0; int count = 1; for(int i = 1;i<A.length -1;i++){ if(A[i] != A[i-1]){ count++; } while(i<A.length-1 && (A[i] - A[i+1])*(A[i-1]-A[i])>0){ i++; count ++; } if(count > max){ max = count; } count = 1; }  return max; } }

    后来用动态规划的思想做了一下,用时减少一半

    public class Solution { /** * @param A an array of Integer * @return an integer */ public static int longestIncreasingContinuousSubsequence(int[] A) { if(A.length <=1){// Write your code here return A.length; } int max = 0; int count = 1; for(int i = 1;i<A.length -1;i++){ if(A[i] != A[i-1]){ count++; } while(i<A.length-1 && (A[i] - A[i+1])*(A[i-1]-A[i])>0){ i++; count ++; } if(count > max){ max = count; } count = 1; } return max; } }

    转载请注明原文地址: https://ju.6miu.com/read-675739.html

    最新回复(0)