题解 | ranko的手表

ranko的手表

https://www.nowcoder.com/practice/37275e85ae7c4453920eae6b9f7f45fc

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String t1Str = scanner.nextLine();
        String t2Str = scanner.nextLine();

        List<Integer> t1List = generatePossibleTimes(t1Str);
        List<Integer> t2List = generatePossibleTimes(t2Str);

        int minDiff = Integer.MAX_VALUE;
        int maxDiff = Integer.MIN_VALUE;

        for (int t1 : t1List) {
            for (int t2 : t2List) {
                if (t2 > t1) {
                    int diff = t2 - t1;
                    if (diff < minDiff) {
                        minDiff = diff;
                    }
                    if (diff > maxDiff) {
                        maxDiff = diff;
                    }
                }
            }
        }

        System.out.println(minDiff + " " + maxDiff);
    }

    private static List<Integer> generatePossibleTimes(String timeStr) {
        String[] parts = timeStr.split(":");
        String hhPart = parts[0];
        String mmPart = parts[1];

        List<Integer> hours = generatePossibleHours(hhPart);
        List<Integer> minutes = generatePossibleMinutes(mmPart);

        List<Integer> times = new ArrayList<>();
        for (int h : hours) {
            for (int m : minutes) {
                times.add(h * 60 + m);
            }
        }
        return times;
    }

    private static List<Integer> generatePossibleHours(String hh) {
        List<Integer> possible = new ArrayList<>();
        char h1Char = hh.charAt(0);
        char h2Char = hh.charAt(1);

        List<Integer> h1Candidates = new ArrayList<>();
        if (h1Char == '?') {
            h1Candidates.add(0);
            h1Candidates.add(1);
            h1Candidates.add(2);
        } else {
            h1Candidates.add(h1Char - '0');
        }

        List<Integer> h2Candidates = new ArrayList<>();
        if (h2Char == '?') {
            for (int i = 0; i <= 9; i++) {
                h2Candidates.add(i);
            }
        } else {
            h2Candidates.add(h2Char - '0');
        }

        for (int h1 : h1Candidates) {
            for (int h2 : h2Candidates) {
                int hour = h1 * 10 + h2;
                if (hour >= 0 && hour <= 23) {
                    possible.add(hour);
                }
            }
        }
        return possible;
    }

    private static List<Integer> generatePossibleMinutes(String mm) {
        List<Integer> possible = new ArrayList<>();
        char m1Char = mm.charAt(0);
        char m2Char = mm.charAt(1);

        List<Integer> m1Candidates = new ArrayList<>();
        if (m1Char == '?') {
            for (int i = 0; i <= 5; i++) {
                m1Candidates.add(i);
            }
        } else {
            m1Candidates.add(m1Char - '0');
        }

        List<Integer> m2Candidates = new ArrayList<>();
        if (m2Char == '?') {
            for (int i = 0; i <= 9; i++) {
                m2Candidates.add(i);
            }
        } else {
            m2Candidates.add(m2Char - '0');
        }

        for (int m1 : m1Candidates) {
            for (int m2 : m2Candidates) {
                int minute = m1 * 10 + m2;
                if (minute >= 0 && minute <= 59) {
                    possible.add(minute);
                }
            }
        }
        return possible;
    }
}

这一题做出来是不难的,直接枚举所有情况,然后转换为分钟来比较就可以了。但是性能的确会低一些。

但是要求性能高的可读性就会变得十分差,而且容易出错,比如性能排名第一的这个提交(https://www.nowcoder.com/discuss/692882813296594944?sourceSSR=users),就出错了(我已在评论中指出)。

所以我还是选择追求可读性了。

全部评论

相关推荐

05-12 17:28
已编辑
门头沟学院 硬件开发
ldf李鑫:不说公司名祝你以后天天遇到这样的公司
点赞 评论 收藏
分享
05-09 12:23
已编辑
华南理工大学 Java
野猪不是猪🐗:给他装的,双九+有实习的能看的上这种厂我直接吃⑨✌们拿它练练面试愣是给他整出幻觉了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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