zoukankan      html  css  js  c++  java
  • 949. Largest Time for Given Digits

    Given an array of 4 digits, return the largest 24 hour time that can be made.

    The smallest 24 hour time is 00:00, and the largest is 23:59.  Starting from 00:00, a time is larger if more time has elapsed since midnight.

    Return the answer as a string of length 5.  If no valid time can be made, return an empty string.

    Example 1:

    Input: [1,2,3,4]
    Output: "23:41"
    

    Example 2:

    Input: [5,5,5,5]
    Output: ""
    

    Note:

    1. A.length == 4
    2. 0 <= A[i] <= 9
    class Solution {
         public String largestTimeFromDigits(int[] A) {
                String ans = "";
                for (int i = 0; i < 4; ++i) {
                    for (int j = 0; j < 4; ++j) {
                        for (int k = 0; k < 4; ++k) {
                            for(int l = 0; l < 4; l++) {
                                if (i == j || i == k || i == l || j == k || j == l || k == l) continue; // avoid duplicate among i, j & k.
                            String h = "" + A[i] + A[j], m = "" + A[k] + A[l], t = h + ":" + m; // hour, minutes, & time.
                            if (h.compareTo("24") < 0 && m.compareTo("60") < 0 && ans.compareTo(t) < 0) ans = t; // hour < 24; minute < 60; update result.
                            }
                            
                        }
                    }
                }
                return ans;
            }
    }

    垃圾题。。permutation数组里的四位,符合条件且更大就update

  • 相关阅读:
    机器学习算法
    消息队列
    net core 静态文件
    Startup 和 Middleware(中间件)
    HLS 协议
    Web APIs 基于令牌TOKEN验证的实现
    cyq.data开源
    FluentConsole是一个托管在github的C#开源组件
    数据层全栈式编程架构
    Java NIO 完全学习笔记(转)
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/13599316.html
Copyright © 2011-2022 走看看