题解 | #MP3光标位置#

MP3光标位置

https://www.nowcoder.com/practice/eaf5b886bd6645dd9cfb5406f3753e15

import java.util.Scanner;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int size = in.nextInt();
            String command = in.next();
            String[] commands = command.split("");
            List<Integer> screen = new ArrayList<>();
            int screenSize = size;
            if (size > 4) {
                screenSize = 4;
            }
            //初始屏幕
            for (int i = 1; i <= screenSize; i++) {
                screen.add(i);
            }
            getResult(1, size, screen, commands);
            
        }
    }
    private static void getResult(int location, int size, List<Integer> screen,
                                  String[] commands) {
        for (String c : commands) {
            //光标上移
            if (c.equals("U")) {
                //光标在当前屏幕第一首
                if (screen.indexOf(location) == 0) {
                    //光标在第一页第一首
                    if (location == 1) {
                        //光标移动到最后一页最后一首
                        location = size;
                        //切换到最后一页屏幕
                        screen = lastScreen(screen.size(), size);
                    } else {
                        //光标上移一位
                        location--;
                        //屏幕向上滑动
                        screen = updateScreen(screen, c, size);
                    }
                } else {
                    //光标上移一位
                    location--;
                }
            }
            //光标下移
            if (c.equals("D")) {
                //光标在最后一首
                if (screen.indexOf(location) == screen.size()-1) {
                    //光标在最后一页最后一首
                    if (location == size) {
                        //光标移动到第一页第一首
                        location = 1;
                        //切换到第一页屏幕
                        screen = firstScreen(screen.size());
                    } else {
                        //光标下移一位
                        location++;
                        //屏幕向下滑动
                        screen = updateScreen(screen, c, size);
                    }
                } else {
                    //光标下移一位
                    location++;
                }
            }
        }
        for (Integer s : screen) {
            System.out.print(s + " ");
        }
        System.out.println();
        System.out.println(location);
    }
    private static List<Integer> updateScreen(List<Integer> screen, String command,
            int size) {
        //屏幕向上滑动
        if (command.equals("U")) {
            for (int i = 0; i < screen.size(); i++) {
                screen.set(i, screen.get(i) - 1);
            }
        }
        //屏幕向下滑动
        if (command.equals("D")) {
            for (int i = 0; i < screen.size(); i++) {
                screen.set(i, screen.get(i) + 1);
            }
        }
        return screen;
    }
    private static List<Integer> firstScreen(int size) {
        List<Integer> firstScreen = new ArrayList<>();
        //生成第一页屏幕
        for (int i = 1; i <= size; i++) {
            firstScreen.add(i);
        }
        return firstScreen;
    }
    private static List<Integer> lastScreen(int size, int max) {
        List<Integer> lastScreen = new ArrayList<>();
        //生成最后一页屏幕
        for (int i = 1; i <= size; i++) {
            lastScreen.add(max - size + i);
        }
        return lastScreen;
    }
}

全部评论

相关推荐

03-10 14:19
已编辑
重庆邮电大学 前端工程师
球Offer上岸👑:测试也难求一面 逆天
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务