幸运子序列

https://www.nowcoder.com/practice/872919272a33406a9c5ddc8b2f7532f4?tpId=90&tqId=30804&tPage=2&rp=2&ru=%2Fta%2F2018test&qru=%2Fta%2F2018test%2Fquestion-ranking

把当前位置当做次大,分别计算它左边和右边第一个比它大的值作为最大值

public class Main1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            Cal(sc);
        }
    }
    public static void Cal(Scanner sc) {
        int n = sc.nextInt();
        int[] V = new int[n];
        ArrayList<Integer> arr = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            V[i] = sc.nextInt();
        }
        for (int i = 0; i < n; i++) {
            int l=-1, r=-1;
            //计算左边第一个比它大的
            for (int x = i - 1; x >= 0; x--) {
                if (V[x] > V[i]) {
                    l = x;
                    break;
                }
            }
            //计算右边第一个比它大的
            for (int y = i + 1; y < n; y++) {
                if (V[y] > V[i]) {
                    r = y;
                    break;
                }
            }
                if (l!=-1) {
                    arr.add(V[l]^V[i]);
                }
                if (r!=-1) {
                    arr.add(V[i]^V[r]);
                }
        }
        Collections.sort(arr, (a, b) -> {
            return b - a;
        });
        System.out.println(arr.get(0));
    }
}

会不会有一部分值永远计算不到啊?

posted @ 2019-05-15 13:10  LeeJuly  阅读(186)  评论(0)    收藏  举报