最近,我以示例为了找到最早的有效数字24小时格式的任务,而给定的6位数字可能是可能的,如果无法返回空。
示例,1 8 3 2 6 4
12:36:48
我已经编写了代码,但是在运行测试的时间太多的情况下失败了。是的,我同意我的代码填写完整的,如果其他陈述(一点也不好(是他们的任何好解决方案吗?
我的代码:
public String solution(int A, int B, int C, int D, int E, int F) {
List<Integer> nums = new ArrayList<>();
nums.add(A);
nums.add(B);
nums.add(C);
nums.add(D);
nums.add(E);
nums.add(F);
String earlyTime = "NOT POSSIBLE";
Collections.sort(nums);
if(nums.get(0)==0 && nums.get(1)==0 && nums.get(2)==0){
earlyTime = nums.get(0)+""+nums.get(3)+":"+nums.get(1)+""+nums.get(4)+":"+nums.get(2)+""+nums.get(5);
}
else if(nums.get(0) <= 2 && nums.get(0) > -1){
if(nums.get(1) <=3 && nums.get(1) > -1){
if(nums.get(2) <= 5 && nums.get(2) > -1){
if(nums.get(3) <= 9 && nums.get(3) > -1){
if(nums.get(4) <= 5 && nums.get(4) > -1){
if(nums.get(5) <= 9 && nums.get(5) > -1){
earlyTime = nums.get(0)+""+nums.get(1)+":"+nums.get(2)+""+nums.get(3)+":"+nums.get(4)+""+nums.get(5);
}
}
else if(nums.get(4) > 5){
int tmp = nums.get(3);
nums.set(3, nums.get(4));
nums.set(4, tmp);
if(Integer.parseInt(nums.get(4)+""+nums.get(5)) < 59 && Integer.parseInt(nums.get(4)+""+nums.get(5)) > -1){
earlyTime = nums.get(0)+""+nums.get(1)+":"+nums.get(2)+""+nums.get(3)+":"+nums.get(4)+""+nums.get(5);
}
}
}
}
}
}else{
return "NOT POSSIBLE";
}
return earlyTime;
}
我有一种方法,在考虑了一些输入示例之后,我想到了此算法。
让我们将6个数字分为两个类别high_number
和low_number
。
high_number
: - 在 [6,9]
low_number
: - 在 [0,5]
Algorithm:-
Step 1:- Now iterate once over this 6 digits, check whether each digit is either `high_number` or `low_number` and store them in the array called `high_number_array` and `low_number_array` respectively.
Step 2:- Sort both the above array `high_number_array` and `low_number_array`.
Step 3:- There will be three cases here.
(i) HC = size of high_number_array , LC = size of low_number_array
(ii) Case HC > LC { not possible to make a valid time }
(iii) Case HC == LC { pick alternately from low_number_array and high_number_array and we will get a valid smallest time.
(iv) Case HC < LC {if HC == 0 or HC == 1 then combine low_number_array and high_number_array }
(v) Case HC < LC { if HC == 2, then after combining both arrays, swap 4th and 5th digit.
尝试使用一堆Exampels测试此算法,U将获取数据。
希望这会有所帮助!
现在有三种主要情况
您可以首先填充最大的(可能相互冲突的(数字。如下所示,只有3种可能的配置:
public String solve(int A, int B, int C, int D, int E, int F) {
int[] d = {A, B, C, D, E, F};
Arrays.sort(d);
if (d[4] < 6) { // 2nd largest digit is smaller 6, we can just fill up
if (10 * d[0] + d[1] < 24)
return "" + d[0] + d[1] + ":" + d[2] + d[3] + ":" + d[4] + d[5];
else
return "impossible";
} else if (d[3] < 6) { // 3rd largest digit is smaller 6, put 2nd largest in 4th position
if (10 * d[0] + d[1] < 24)
return "" + d[0] + d[1] + ":" + d[2] + d[4] + ":" + d[3] + d[5];
else
return "impossible";
} else if (d[2] < 6) { // 4th largest digit is smaller 6, put 3rd largest in 2nd position
if (10 * d[0] + d[3] < 24)
return "" + d[0] + d[3] + ":" + d[1] + d[4] + ":" + d[2] + d[5];
else
return "impossible";
} else {
return "impossible";
}
}
private String solution(int i, int j, int k, int l, int m, int n) {
List<Integer> list = new ArrayList<Integer>();
list.add(i);
list.add(j);
list.add(k);
list.add(l);
list.add(m);
list.add(n);
Collections.sort(list);
// first two needs to form HH
// hence HH = list [0] and list[1]
if (list.get(3) > 5) { // place where 1 needs to be swaped with 3rd
int temp = list.get(1);
list.set(1, list.get(3));
list.set(3, temp);
}
System.out.println(list);
String HH = "" + list.get(0) + list.get(1);
if (Integer.parseInt(HH) > 23) {
return "NOT POSSIBLE";
}
// now check phisiblility of SS after MM
if ((list.get(2) * 10 + list.get(3)) < 60 && (list.get(4) * 10 + list.get(5)) < 60) {
} else if ((list.get(2) * 10 + list.get(3)) < 60 && (list.get(4) * 10 + list.get(5)) > 59) {
// swap //3 and 4
System.out.println(list);
int temp = list.get(3);
list.set(3, list.get(4));
list.set(4, temp);
System.out.println(list);
} else {
return "NOT POSSIBLE";
}
return HH + ":" + list.get(2) + list.get(3) + ":" + list.get(4) + list.get(5);
}
在此问题中,我从答案中借了一些概念,这是我解决方案的基本逻辑
至少需要2个整数[0,3]才能制作一个有效的小时
至少需要3个整数[0,5]才能制作有效的秒,最小值,小时
最多两个整数[0,9(只能存在。
import java.util.stream.IntStream;
import java.util.Arrays;
public class Demo {
public static void main(String[] args) {
System.out.println(solution(1, 8, 2, 3, 6, 4));
System.out.println(solution(0, 0, 0, 7, 8, 9));
System.out.println(solution(2, 4, 5, 9, 5, 9));
System.out.println(solution(2, 5, 5, 9, 5, 9));
System.out.println(solution(6, 5, 4, 3, 2, 1));
System.out.println(solution(8, 1, 2, 4, 3, 6));
System.out.println(solution(9, 2, 8, 6, 7, 0));
System.out.println(solution(0, 0, 0, 0, 0, 0));
System.out.println(solution(0, 0, 0, 0, 0, 1));
System.out.println(solution(2, 3, 5, 9, 5, 9));
System.out.println(solution(0, 0, 2, 4, 0, 0));
System.out.println(solution(4, 4, 4, 5, 9, 9));
System.out.println(solution(4, -1, 4, 5, 9, 9));
}
public static String solution(int A,int B, int C,int D,int E,int F) {
//We need minimum of 2 integers in range [0,3] to make a valid hour
//We need minimum of 3 integers [0,5] to make valid seconds,min,hour
//Sort the number in ASC order
int[] nums= IntStream.of(A,B,C,D,E,F).filter(n -> n>=0).sorted().toArray();
if(nums.length !=6)
return "NOT POSSIBLE";
int[] splittedTime= new int[] {nums[0]*10+nums[1],nums[2]*10+nums[3],nums[4]*10+nums[5]};
//splittedTime[0]=hour:splittedTime[1]=minutes:splittedTime[2]=seconds
if(!validateHour(splittedTime[0]))
//This means among given 6 integers , 5 integers are > 3, so can't make up a valid hour under 24
return "NOT POSSIBLE";
if(!validateMinute(splittedTime[1])) {
// this means among given 6 integers, 4 integers are greater than > 5, so can't make up valid minutes, seconds
return "NOT POSSIBLE";
}
if(!validateSecond(splittedTime[2])) {
if(nums[3]<6) {
//swapping maximum from minute with, minimum from second
int swapMin=nums[4];
int swapSecond=nums[3];
nums[3]=swapMin;
nums[4]=swapSecond;
//Can directly assign no need to keep it in temp variable
}else if(nums[3]>6) {
// Kind of circular swapping between hour, minute and second
int[] firstHalf = Arrays.copyOfRange(nums, 0, nums.length/2);
int[] secondHalf = Arrays.copyOfRange(nums, nums.length/2, nums.length);
nums=IntStream.of(firstHalf[0], secondHalf[0], firstHalf[1], secondHalf[1], firstHalf[2], secondHalf[2]).toArray();
}
}
}
return String.format ("%1$d%2$d:%3$d%4$d:%5$d%6$d", nums[0], nums[1], nums[2], nums[3], nums[4], nums[5]);
}
public static boolean validateHour(int hour) {
return hour<24;
}
public static boolean validateMinute(int minute) {
return minute<60;
}
public static boolean validateSecond(int second) {
return second<60;
}
}
此解决方案有效,但不确定性能。
这可以表现出色。多亏了这个答案,相同的代码和轻微的修改
public String solve(int A, int B, int C, int D, int E, int F) {
int[] d = IntStream.of(A, B, C, D, E, F).filter(n -> n >= 0).sorted().toArray();
if (d[4] < 6) { // 2nd largest digit is smaller 6, we can just fill up
if (10 * d[0] + d[1] < 24)
return "" + d[0] + d[1] + ":" + d[2] + d[3] + ":" + d[4] + d[5];
} else if (d[3] < 6) { // 3rd largest digit is smaller 6, put 2nd largest in 4th position
if (10 * d[0] + d[1] < 24)
return "" + d[0] + d[1] + ":" + d[2] + d[4] + ":" + d[3] + d[5];
} else if (d[2] < 6) { // 4th largest digit is smaller 6, put 3rd largest in 2nd position
if (10 * d[0] + d[3] < 24)
return "" + d[0] + d[3] + ":" + d[1] + d[4] + ":" + d[2] + d[5];
}
return "NOT POSSIBLE";
}