- 首页 > it技术 > >
package suanfa2;
import java.util.Stack;
public class MaxNumLength {
public static void main(String[] args) {
//int[] arr = new int[]{1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 7};
//int[] arr = new int[]{2, 2, 3, 1, 4, 5, 1, 1, 0, 1, 7};
//int[] arr = new int[]{2, 2, 3, 1, 1, 1};
int[] arr = new int[]{1, 1, 1};
int start = 0;
int ct = 0;
int max = 0;
int maxStart = 0;
boolean restart = true;
for (int i = 0;
i < arr.length;
i++) {
if (restart || arr[i] == arr[start] && ct == 0) {
restart = false;
continue;
}
if (arr[start] != arr[i] && ct == 0) {
ct = i;
continue;
}if ((ct != 0 && arr[start] != arr[i])) {
int len = i - start - 1;
if (len > max) {
maxStart = start;
max = len;
}
start = ct;
ct = 0;
// 清空
restart = true;
i = start - 1;
}
}
int len = arr.length - start - 1;
if (ct == 0) {
len++;
}
if (len > max) {
maxStart = start;
max = len;
}
System.out.println(maxStart + " " + max);
for (int i = 0;
i < arr.length;
i++) {
if (i > maxStart + max || i < maxStart) {
if (arr[i] == arr[maxStart]) {
max++;
break;
}
}
}
System.out.println(max);
}
}
推荐阅读