版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hsx1612727380/article/details/85807930
Title: Largest Time for Given Digits 949
Difficulty:Easy
原题leetcode地址: https://leetcode.com/problems/largest-time-for-given-digits/
1. 见代码注释
时间复杂度:O(n^3),三层for循环。
空间复杂度:O(1),没有申请额外空间。
/**
* 3层循环,注意每一个数不相等,同时最后一个数就是经过3层循环后剩余的
* @param A
* @return
*/
public static String largestTimeFromDigits(int[] A) {
int res = -1;
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < A.length; j++) {
if (i != j) {
for (int k = 0; k < A.length; k++) {
if (k != i && k != j) {
int l = 6 - i - j - k;
int h = 10 * A[i] + A[j];
int m = 10 * A[k] + A[l];
if (h < 24 && m < 60) {
res = Math.max(res, h * 60 + m);
}
}
}
}
}
}
return res >= 0 ? String.format("%02d:%02d", res / 60, res % 60) : "";
}