最长递增字串

public class Zuichangdizengzichuang {

    public static void main(String args[]) {

        Integer[] a = new Integer[]{4,6,8,10,34,65,2,43,54,76,86,54,33,23,55};
        MaxIncreSubSequence<Integer> mss = new MaxIncreSubSequence<Integer>(a);
        System.out.println(mss.getNumOfMaxIncreSubSequence());

        MaxIncreCloseSubSequence<Integer> mcss = new MaxIncreCloseSubSequence<Integer>(a);
        System.out.println(mcss.getNumOfMaxIncreCloseSubSequence());
    }

    //连续最长递增子串
    static class MaxIncreCloseSubSequence<T extends Comparable<? super T>>{

        T[] t ;

        public MaxIncreCloseSubSequence(T[] t) {

            this.t = t;
        }

        public int getNumOfMaxIncreCloseSubSequence() {

            int res = 0, find = 1;
            for(int i = 0; i < t.length; i++) {

                if(i + 1 < t.length && t[i].compareTo(t[i+1]) < 0) {

                    find++;
                } else {

                    res = Math.max(find, res);
                    find = 0;
                }
            }
            return res;
        }
    }

    //最长递增字串,不一定连续。用一个数组tmp保存计算过的数据,tmp[i]表示到i+1位置时的递增字串长度
    static class MaxIncreSubSequence<T extends Comparable<? super T>>{

        T[] t ;

        public MaxIncreSubSequence(T[] t) {

            this.t = t;
        }

        public int getNumOfMaxIncreSubSequence() {

            int[] tmp = new int[t.length];
            int res = 0;
            for(int i = 0; i < t.length ; i++) {

                tmp[i] = 1;
                for(int j = 0; j < i; j++ ) {

                    if(t[j].compareTo(t[i]) < 0) 
                        tmp[i] = Math.max(tmp[i], tmp[j] + 1);
                }
                res = Math.max(res, tmp[i]);
            }
            return res;
        }

    }

}

结果是
9
6

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值