京东2019年春招题(前端)

京东2019年春招题

最长区间

在这里插入图片描述

思路:

把字符串与自身拼接,找新字符串中的连续1的区间长度,考虑原字符串全为1或全为0的情况,

coding

#include<bits/stdc++.h>
using namespace std;
int main(){
    
    
    string s,t;
    int n,r=0;
    cin>>s;
        t=s+s;
        n=t.length();
        for(int i=0;i<n;i++){
    
    
            int j=i;
            while(i<n&&t[i]=='1') i++;
            r=max(r,i-j);
        }
        if(r==2*s.length()) cout<<r/2<<endl;
        else cout<<r<<endl;
    return 0;
}

在这里插入图片描述

思路:

遍历一遍数组,寻找最长的递增序列长度,然后简单数学运算就完事了

coding

#include<bits/stdc++.h>
using namespace std;
int res[100001];
int main(){
    
    
    int i,n,count;
    while(scanf("%d",&n)!=EOF){
    
    
        for(int i=0;i<n;i++){
    
    
            scanf("%d",&res[i]);
        }
        count=0;
        for(i=0;i<n-1;i++){
    
    
            if(res[i+1]>res[i]) count++;
            else count=0;
        }
        printf("%d\n",n-count-1);
    }
    return 0;
}

欢乐的时光总是短暂的,让我们下一次再见!!!

good good study,day day up! (study hard, improve every day)

预知后事,请听下回分解!!!!

猜你喜欢

转载自blog.csdn.net/qq_41606378/article/details/115136739