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[],A[],A[],A[]);
check(A[],A[],A[],A[]);
check(A[],A[],A[],A[]);
check(A[],A[],A[],A[]);
check(A[],A[],A[],A[]);
check(A[],A[],A[],A[]);
return ans;
} void check(int a,int b,int c,int d){
string first = best(a,b,);
string second = best(c,d,);
if(first == ""||second == "")return;
string cand = first + ":" + second;
if(bijiao(cand,ans))ans = cand;
return;
} bool bijiao(string cand,string ans){
if(ans == "")return ;
int a = (cand[]-'')* + (cand[]-'')* + (cand[]-'')* + (cand[]-'');
int b = (ans[]-'')* + (ans[]-'')* + (ans[]-'')* + (ans[]-'');
return a > b;
}
string caonima(int num){
string res = "";
res += (num+'');
return res;
} string best(int a,int b,int limit){
int res = max(a*+b<limit ? a*+b:-,b*+a<limit ? b*+a:-);
if(res >= ) return to_string(res);
else if(res>= && res<)return caonima(res);
else return "";
}
};

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

Leetcode 949. 给定数字能组成的最大时间的更多相关文章

  1. [Swift]LeetCode949. 给定数字能组成的最大时间 | 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 t ...

  2. Leetcode949. Largest Time for Given Digits给定数字能组成最大的时间

    给定一个由 4 位数字组成的数组,返回可以设置的符合 24 小时制的最大时间. 最小的 24 小时制时间是 00:00,而最大的是 23:59.从 00:00 (午夜)开始算起,过得越久,时间越大. ...

  3. LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)

    题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description   Problem: 在已知递减排序的数组中,查找到给定 ...

  4. leetcode 12题 数字转罗马数字

    leetcode 12题 数字转罗马数字 答案一:我的代码 代码本地运行完全正确,在线运行出错 class Solution { public: string intToRoman(int num) ...

  5. [LeetCode] 681. Next Closest Time 下一个最近时间点

    Given a time represented in the format "HH:MM", form the next closest time by reusing the ...

  6. mysql学习1:数据类型:数字型,日期和时间,字符串类型(总结)

    mysql数据类型:数字型,日期和时间,字符串类型 摘要 MySQL中定义数据字段的类型对数据库的优化是非常重要的: MySQL支持多种类型,大致可以分为三类,如下. 数字类型 整数:tinyint. ...

  7. linux time-统计给定命令所花费的总时间

    推荐:更多linux 性能监测与优化 关注:linux命令大全 time命令用于统计给定命令所花费的总时间. 语法 time(参数) 参数 指令:指定需要运行的额指令及其参数. 实例 当测试一个程序或 ...

  8. LeetCode.2-两个数字相加(Add Two Numbers)

    这是悦乐书的第340次更新,第364篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第1题(顺位题号是2).给定两个非空链表,表示两个非负整数. 数字以相反的顺序存储, ...

  9. LeetCode.949-给定数字的最大时间(Largest Time for Given Digits)

    这是悦乐书的第363次更新,第391篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第225题(顺位题号是949).给定4个整数组成的数组,返回最大的24小时时间. 最小的 ...

随机推荐

  1. HDU 5829 Rikka with Subset(NTT)

    题意 给定 \(n\) 个数 \(a_1,a_2,\cdots a_n\),对于每个 \(K\in[1,n]\) ,求出 \(n\) 个数的每个子集的前 \(K\) 大数的和,输出每个值,对 \(99 ...

  2. 剥开比原看代码09:通过dashboard创建密钥时,前端的数据是如何传到后端的?

    作者:freewind 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchai ...

  3. printf和std::cout ...endl

    printf效率要比std::cout...endl高些,可以减少打印所花时间

  4. Leetcode1 - A + B Problem - Easy

    Write a function that add two numbers A and B. Example Example 1: Input: a = 1, b = 2 Output: 3 Expl ...

  5. Lintcode27-Reverse 3-digit Integer

    Reverse a 3-digit integer. Example Example 1: Input: number = 123 Output: 321 Example 2: Input: numb ...

  6. Leetcode88_Merge Sorted Array_Easy

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...

  7. layout_gravity与gravity的区别,和padding margin的区别

    https://blog.csdn.net/github_39688629/article/details/77790541

  8. DataGrip激活码

    引言: 网上有有很多datagirp的激活码,但是经过尝试很多都失效了,找了半天终于 找到了一个可用的激活码! 1. 激活码 适用版本: DataGrip2018.2.3,2018.1.1,其他版本没 ...

  9. github Bash教程

    1.只在本地使用 配置github git config --global user.name 你的英文名 git config --global user.email 你的邮箱 git config ...

  10. leecode第四十三题(字符串相乘)

    class Solution { public: string multiply(string num1, string num2) { ";//特殊情况 ] == ] == ') retu ...