京东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;
}