zoukankan      html  css  js  c++  java
  • Leetcode 949. 给定数字能组成的最大时间

    949. 给定数字能组成的最大时间

     
     
    • 用户通过次数125
    • 用户尝试次数213
    • 通过次数127
    • 提交次数774
    • 题目难度Easy

    给定一个由 4 位数字组成的数组,返回可以设置的符合 24 小时制的最大时间。

    最小的 24 小时制时间是 00:00,而最大的是 23:59。从 00:00 (午夜)开始算起,过得越久,时间越大。

    以长度为 5 的字符串返回答案。如果不能确定有效时间,则返回空字符串。

    示例 1:

    输入:[1,2,3,4]
    输出:"23:41"
    

    示例 2:

    输入:[5,5,5,5]
    输出:""
    

    提示:

    1. A.length == 4
    2. 0 <= A[i] <= 9
    class Solution {
    public: 
        string ans = "";
        string largestTimeFromDigits(vector<int>& A) {
            check(A[0],A[1],A[2],A[3]);
            check(A[0],A[2],A[1],A[3]);
            check(A[0],A[3],A[1],A[2]);
            check(A[1],A[2],A[0],A[3]);
            check(A[1],A[3],A[2],A[0]);
            check(A[2],A[3],A[1],A[0]);
            return ans;
        }
        
        void check(int a,int b,int c,int d){
            string first = best(a,b,24);
            string second = best(c,d,60);
            if(first == ""||second == "")return;
            string cand = first + ":" + second;
            if(bijiao(cand,ans))ans = cand;
            return;
        }
        
        bool bijiao(string cand,string ans){
            if(ans == "")return 1;
            int a = (cand[0]-'0')*1000 + (cand[1]-'0')*100 + (cand[3]-'0')*10 + (cand[4]-'0');
            int b = (ans[0]-'0')*1000 + (ans[1]-'0')*100 + (ans[3]-'0')*10 + (ans[4]-'0');
            return a > b;
        }
        string caonima(int num){
            string res = "0";
            res += (num+'0');
            return res;
        }
        
        string best(int a,int b,int limit){
            int res = max(a*10+b<limit ? a*10+b:-1,b*10+a<limit ? b*10+a:-1);
            if(res >= 10) return to_string(res);
            else if(res>=0 && res<10)return caonima(res);
            else return "";
        }
    };

    _再也不会做这种恶心死了的题,烦死了快,各种边界条件,输出格式!!

  • 相关阅读:
    ORACLE 计算时间相减间隔
    oracle中游标详细用法
    oracle中计算某月的天数
    Unity3D导出的EXE不用显示分辨率选择界面
    Unity3D 之暂停和继续的实现
    double的值太大,以及补0
    Unity3D鼠标点击物体产生事件
    java POi excel 写入大批量数据
    Unity3D 判断鼠标是否按在UGUI上
    Unity3D 之UGUI 滚动条
  • 原文地址:https://www.cnblogs.com/cunyusup/p/10604938.html
Copyright © 2011-2022 走看看