美文网首页
最大有序数组长度

最大有序数组长度

作者: 全都是泡沫啦 | 来源:发表于2020-03-21 14:35 被阅读0次
    import java.util.Arrays;
    
    /**
     * 输入一个数组,1, 5, 6, 2, 3, 4, 5, 6, 1, 2
     * 1,5,6有序长度为3,  2,3,4,5,6有序长度为5, 1,2有序长度为2
     * 输出他们最常的有序长度为5
     * Created by hjt on 2020/3/14.
     */
    public class LongestSortedLengthDemo {
        public static void main(String[] args) {
     //       int[] b = {};
    //        int[] b = {1};
    //        int[] b = {1, 2};
    //        int[] b = {2,1};
    //        int[] b = {2,1,2,3,123,1,2};
            int[] b = {1, 5, 6, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 8};
            //int[] b = {1, 5, 6, 2, 3, 4, 5, 6, 1, 2};
            int longestOrderedStringLength = longestOrderedStringLength(b);
            System.out.println("数组" + Arrays.toString(b) + "最长有序长度:" + longestOrderedStringLength);
        }
    
        public static int longestOrderedStringLength(int[] data) {
            if (data == null || data.length == 0) {
                return 0;
            }
            if (data.length == 1) {
                return 1;
            }
            int dataLen = data.length;
            int lastInt = data[0];
            int maxOrderedLength = 1;
            int lastOrderedLength = 1;
            for (int i = 1; i < dataLen; i++) {
                int nowInt = data[i];
                if (nowInt >= lastInt) {
                    lastOrderedLength++;
                    if (lastOrderedLength > maxOrderedLength) {
                        maxOrderedLength = lastOrderedLength;
                    }
                }else {
                    if (lastOrderedLength > maxOrderedLength) {
                        maxOrderedLength = lastOrderedLength;
                    }
                    lastOrderedLength = 1;
                }
                lastInt = nowInt;
            }
            return maxOrderedLength;
        }
    }
    
    

    相关文章

      网友评论

          本文标题:最大有序数组长度

          本文链接:https://www.haomeiwen.com/subject/wsbyyhtx.html