java开发美团二面算法题

1.题目:

java开发美团二面算法题_第1张图片

java开发美团二面算法题_第2张图片

2.代码

java开发美团二面算法题_第3张图片

链接:https://www.nowcoder.com/questionTerminal/4e1012fe691b446d88eba5db8f511692
来源:牛客网
 

import java.util.*;

public class test20170522F {

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        Scanner sc = new Scanner(System.in);

        while (sc.hasNext()) {

            int N = sc.nextInt();

            int[] a = new int[N];

            int[] maxLen = new int[N];

            for(int i=0;i

                a[i] = sc.nextInt();

            }

            //对a[0]特殊处理

            if (a[1] >= 2) {

                maxLen[0] = 2;

                for (int i = 2; i < N; i++) {

                    if (a[i] > a[i - 1])

                        maxLen[0]++;

                    else

                        break;

                }

            }

            //对a[N-1]特殊处理

            maxLen[N - 1] = 2;

            for (int i = N - 2; i >= 0; i--) {

                if (a[i] > a[i - 1])

                    maxLen[N - 1]++;

                else

                    break;

            }

            //对标号是1~N-2的进行处理

            for (int i = 1; i <= N - 2; i++) {

                if (a[i + 1] - a[i - 1] >= 2) {

                    maxLen[i] = 3;

                    for (int j = i - 1; j > 0; j--) {

                        if (a[j] > a[j - 1])

                            maxLen[i]++;

                        else

                            break;

                    }

                    for (int j = i + 1; j < N-1; j++) {

                        if (a[j + 1] > a[j])

                            maxLen[i]++;

                        else

                            break;

                    }

                } else

                    maxLen[i] = 2;

            }

            Arrays.sort(maxLen);

            System.out.println(maxLen[N-1]);

        }

    }

}

你可能感兴趣的:(面经汇总)