1数组山谷
数组里的山谷是指一个数组A中的连续子数组B满足以下条件:
(1)B.length>=3;
(2)存在满足:0<i<B.length-1并且B[0]>B[1]>...>B[i-1]>B[i]<B[i+1]<...<B[B.length-1];
现给定一个整形数组A,找出数组A里的最长山谷B的长度,如果没有,则输出0.
public static int longestMountain(int[] A) {
int res = 0, up = 0, down = 0;
for (int i = 1; i < A.length; ++i) {
if (up > 0 && A[i - 1] > A[i] || A[i - 1] == A[i])
up = down = 0;
if (A[i - 1] < A[i])
up++;
if (A[i - 1] > A[i])
down++;
if (up > 0 && down > 0 && up + down + 1 > res)
res = up + down + 1;
}
return res;
}
2字符串构造
有一个长度为n的字符串P,我们可以通过P构造出一个无限长度的字符串S,其中S[i]=P[i%n]。给定一个字符串S,