这是我根据您的思路写的代码,这么写对吗?
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = 0; int k = 0;
String[] s = cin.nextLine().split(" ");
n = Integer.valueOf(s[0]);
k = Integer.valueOf(s[1]);
String[] str = cin.nextLine().split(" ");
System.out.println(find(n,k,str));
}
private static int find(int n, int k, String[] str) {
if(k>=n)
return n;
int max = 0;
int l=0; int r=-1;
while(r < n-1){
r++;
if(str[r].equals("0")){
--k;
}
while(k<0){
if(str[l].equals("0")){
k++;
l++;
}
else
l++;
}
max = max > r-l+1 ? max :r-l+1;
}
return max;
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = 0; int k = 0;
String[] s = cin.nextLine().split(" ");
n = Integer.valueOf(s[0]);
k = Integer.valueOf(s[1]);
String[] str = cin.nextLine().split(" ");
System.out.println(find(n,k,str));
}
private static int find(int n, int k, String[] str) {
if(k>=n)
return n;
int max = 0;
int l=0; int r=-1;
while(r < n-1){
r++;
if(str[r].equals("0")){
--k;
}
while(k<0){
if(str[l].equals("0")){
k++;
l++;
}
else
l++;
}
max = max > r-l+1 ? max :r-l+1;
}
return max;
}