找到最早的有效数字24小时格式时间,这可以通过给定的6位数

时间:2018-02-12 16:39:41

标签: java algorithm data-structures java-8

最近,我举了一个例子,找到了最早的有效数字24小时格式时间,这个时间可以用给定的6位数字,如果不能返回空的话。

例如,1 8 3 2 6 4

12时36分48秒

我已经编写了代码,但是由于运行测试花费了太多时间而失败了。是的,我同意我的代码充满了完整的if else语句,一点也不好,对于它来说,这是一个很好的解决方案吗?

我的代码:

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;
}

4 个答案:

答案 0 :(得分:2)

我有一个方法,考虑到一些输入示例后,我想出了这个算法。

我们将6个数字分为两类high_numberlow_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.

尝试使用一堆示例测试此算法,您将获得数据。

希望这有帮助!

现在有三个主要案例

答案 1 :(得分:1)

您可以先填写最大(可能有冲突)的数字。您可以在下面的代码中看到只有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";
  }
}

答案 2 :(得分:0)

在这个问题上,我从answer借用了一些概念, 这是我解决方案的基本逻辑

  

[0,3]范围内至少需要2个整数才能构成有效小时

     

至少需要3个整数[0,5]才能使秒,分,小时有效

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;
    }

}

此解决方案有效,但不确定性能。

这具有良好的性能。多亏了this answer, 相同的代码,稍作修改

 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";
    }

答案 3 :(得分:0)

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);

    }