Java最长连号

题目描述

输入长度为 n 的一个正整数序列,要求输出序列中最长连号的长度。

连号指在序列中,从小到大的连续自然数。

输入格式

第一行,一个整数 n。

第二行,n 个整数 ai​,之间用空格隔开。

输出格式

一个数,最长连号的个数。

输入输出样例

输入 #1

10
1 5 6 2 3 4 5 6 8 9

输出 #1

5

说明/提示

数据规模与约定

对于 100% 的数据,保证 1≤n≤104,1≤ai​≤109。

代码如下:

// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int a = scanner.nextInt();
        int[] arr=new int [30000];
        int s=1,max=0;
        for(int i=1;i<=a;i++){
            arr[i]=scanner.nextInt();
            if(i>=2){
                if(arr[i]==arr[i-1]+1){
                    s++;
                }else s=1;

                if(s>max) max=s;
            }
        }
        System.out.println(max);
    }
}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值