您的位置:首页 > 文旅 > 旅游 > 最大时间

最大时间

2024/10/6 6:45:26 来源:https://blog.csdn.net/weixin_64742764/article/details/142018155  浏览:    关键词:最大时间

题目描述

给定一个数组,里面有6个整数,求这个数组能够表示的最大 24 进制的时间是多少,输出这个时间,无法表示输出 invalid.

输入描述

输入为一个整数数组,数组内有六个整数。
输入整数数组长度为6,不需要考虑其它长度,元素值为0或者正整数,6 个数字每个数字只能使用一次。

输出描述

输出为一个 24 进制格式的时间,或者字符串"invalid".

我也不是很明白,有的步骤为什么这么写,但是大体流程是知道的,可能练习不够,还需多加练习吧

 //[0,2,3,0,5,6]
public class 最大时间Main {private static final Pattern c = Pattern.compile("(([01][0-9])|([2][0-3])):([0-5][0-9]):([0-5][0-9])");public static void main(String[] args) {Scanner sc = new Scanner(System.in);String s = sc.nextLine();Integer[] arr = Arrays.stream(s.substring(1, s.length() - 1).split(",")).map(Integer::parseInt).toArray(Integer[]::new);System.out.println(getResult(arr));}public static String getResult(Integer[] arr) {ArrayList<String> res = new ArrayList<>();dfs(arr, new boolean[arr.length], new LinkedList<>(), res);if (res.isEmpty()) return "invalid";Collections.sort(res, Collections.reverseOrder());return res.get(0);}public static void dfs(Integer[] arr, boolean[] used, LinkedList<Integer> path, ArrayList<String> res) {int pathLength = path.size();if (pathLength == arr.length) {Integer[] t = path.toArray(new Integer[0]);String time = t[0] + "" + t[1] + ":" + t[2] + "" + t[3] + ":" + t[4] + "" + t[5];if (c.matcher(time).matches()) res.add(time);return;}for (int i = 0; i < arr.length; i++) {if (!used[i]) {path.add(arr[i]);used[i] = true;dfs(arr, used, path, res);used[i] = false;path.removeLast();}}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com