题解 | #MP3光标位置#
MP3光标位置
https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 滑动窗口法
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
char[] op = br.readLine().toCharArray();
int temp = 1;
int start=1;
int end=Math.min(N, 4);
for (char c : op) {
if (c == 'U') {
temp--;
if (temp < 1)
temp = N;
} else {
temp++;
if (temp > N)
temp = 1;
}
if(temp<start){
start=temp;
end=start+3;
}else if(temp>end){
end=temp;
start=end-3;
}
}
for(int i=start;i<=end;i++)
System.out.print(i+" ");
System.out.println("");
System.out.println(temp);
}
}
查看8道真题和解析
海康威视公司福利 1396人发布