题解 | #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);
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务