Leetcode949. Largest Time for Given Digits给定数字能组成最大的时间
给定一个由 4 位数字组成的数组,返回可以设置的符合 24 小时制的最大时间。
最小的 24 小时制时间是 00:00,而最大的是 23:59。从 00:00 (午夜)开始算起,过得越久,时间越大。
以长度为 5 的字符串返回答案。如果不能确定有效时间,则返回空字符串。
示例 1:
输入:[1,2,3,4] 输出:"23:41"
示例 2:
输入:[5,5,5,5] 输出:""
提示:
- A.length == 4
- 0 <= A[i] <= 9
题目有很多格式没有说明清楚的地方。
类似DFS的循环做法。遍历,注意好判断条件
最初无修改,击败14%
bool cmp(int x, int y)
{
return x > y;
}
class Solution {
public:
string largestTimeFromDigits(vector<int>& A)
{
sort(A.begin(), A.end(), cmp);
bool visit[4];
memset(visit, 0, sizeof(visit));
int MIN = 0;
int MAX = 1439;
int res = 0;
int flag = false;
for (int i = 0; i < 4; i++)
{
visit[i] = true;
for (int j = 0; j < 4; j++)
{
if (visit[j] == true)
continue;
visit[j] = true;
for (int k = 0; k < 4; k++)
{
if (visit[k] == true)
continue;
visit[k] = true;
for (int l = 0; l < 4; l++)
{
if (visit[l] == true)
continue;
//注意
if(A[i] > 2 || A[k] > 5)
continue;
int temp = (A[i] * 10 + A[j]) * 60 + (A[k] * 10 + A[l]);
if (temp < MIN || temp > MAX)
continue;
flag = true;
res = max(res, temp);
}
visit[k] = false;
}
visit[j] = false;
}
visit[i] = false;
}
if (!flag)
return "";
if (res == 0)
return "00:00";
string str = "";
if (res / 60 < 10)
{
str += "0" + to_string(res / 60) + ":";
}
else
{
str += to_string(res / 60) + ":";
}
if (res % 60 < 10)
{
str = str + "0" + to_string(res % 60);
}
else
{
str += to_string(res % 60);
}
return str;
}
};
因为从大到小排过序,所以第一次能够组成的一定是最大的。
修改一下,击败100%
bool cmp(int x, int y)
{
return x > y;
}
class Solution {
public:
string largestTimeFromDigits(vector<int>& A)
{
sort(A.begin(), A.end(), cmp);
bool visit[4];
memset(visit, 0, sizeof(visit));
int MIN = 0;
int MAX = 1439;
int res = 0;
for (int i = 0; i < 4; i++)
{
visit[i] = true;
for (int j = 0; j < 4; j++)
{
if (visit[j] == true)
continue;
visit[j] = true;
for (int k = 0; k < 4; k++)
{
if (visit[k] == true)
continue;
visit[k] = true;
for (int l = 0; l < 4; l++)
{
if (visit[l] == true)
continue;
//注意
if(A[i] > 2 || A[k] > 5)
continue;
int temp = (A[i] * 10 + A[j]) * 60 + (A[k] * 10 + A[l]);
if (temp < MIN || temp > MAX)
continue;
res = max(res, temp);
if (res == 0)
return "00:00";
string str = "";
if (res / 60 < 10)
{
str += "0" + to_string(res / 60) + ":";
}
else
{
str += to_string(res / 60) + ":";
}
if (res % 60 < 10)
{
str = str + "0" + to_string(res % 60);
}
else
{
str += to_string(res % 60);
}
return str;
}
visit[k] = false;
}
visit[j] = false;
}
visit[i] = false;
}
return "";
}
};
Leetcode949. Largest Time for Given Digits给定数字能组成最大的时间的更多相关文章
- Leetcode 949. 给定数字能组成的最大时间
949. 给定数字能组成的最大时间 显示英文描述 我的提交返回竞赛 用户通过次数125 用户尝试次数213 通过次数127 提交次数774 题目难度Easy 给定一个由 4 位数字组成的数组,返 ...
- [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 ...
- LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)
题目链接: https://leetcode.com/problems/search-for-a-range/?tab=Description Problem: 在已知递减排序的数组中,查找到给定 ...
- 【Leetcode_easy】949. Largest Time for Given Digits
problem 949. Largest Time for Given Digits solution: class Solution { public: string largestTimeFrom ...
- mysql学习1:数据类型:数字型,日期和时间,字符串类型(总结)
mysql数据类型:数字型,日期和时间,字符串类型 摘要 MySQL中定义数据字段的类型对数据库的优化是非常重要的: MySQL支持多种类型,大致可以分为三类,如下. 数字类型 整数:tinyint. ...
- linux time-统计给定命令所花费的总时间
推荐:更多linux 性能监测与优化 关注:linux命令大全 time命令用于统计给定命令所花费的总时间. 语法 time(参数) 参数 指令:指定需要运行的额指令及其参数. 实例 当测试一个程序或 ...
- LeetCode.949-给定数字的最大时间(Largest Time for Given Digits)
这是悦乐书的第363次更新,第391篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第225题(顺位题号是949).给定4个整数组成的数组,返回最大的24小时时间. 最小的 ...
- 113th LeetCode Weekly Contest 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 ...
- LeetCode 258 Add Digits(数字相加,数字根)
翻译 给定一个非负整型数字,反复相加其全部的数字直到最后的结果仅仅有一位数. 比如: 给定sum = 38,这个过程就像是:3 + 8 = 11.1 + 1 = 2.由于2仅仅有一位数.所以返回它. ...
随机推荐
- BCZM : 1.6
https://blog.csdn.net/kabini/article/details/2311946 题目大意: 水房能容纳饮料的总量是V,有一批饮料,每种饮料单个容量都是2的方幂,每种饮料信息如 ...
- java 和 IntelliJ IDEA 的一些配置
jdk 的下载与配置https://jingyan.baidu.com/article/ca41422fe3b7261eae99edc6.html intellij IDEA软件java项目No SD ...
- not registered via @EnableConfigurationProperties or marked as Spring component
利用@ConfigurationProperties(prefix = "")来绑定属性时报错: not registered via @EnableConfigurationPr ...
- BeanPostProcessor原理--使用讲解
<Spring源码解析>笔记 BeanPostProcessor原理学习 在学习BeanPostProcessor的原理学习完之后,对Spring如何使用充满好奇,尝试使用例子进行理解,以 ...
- 网站统计中的PV-UV-IP的定义与区别
--------首先来看看ip.uv和pv的定义---------- PV(访问量):即Page View, 即页面浏览量或点击量,用户每次刷新即被计算一次.UV(独立访客):即Unique Visi ...
- day19 装饰器
Python之路,Day7 = Python基础7 randomwrapper 包装材料:包装纸:书皮global a 全局的(也就是,函数最外面的那个)nonlocal a 局部的,上层的函数的变量 ...
- BZOJ 1089 (SCOI 2003) 严格n元树
Description 如果一棵树的所有非叶节点都恰好有n个儿子,那么我们称它为严格n元树.如果该树中最底层的节点深度为d (根的深度为0),那么我们称它为一棵深度为d的严格n元树.例如,深度为2的严 ...
- iOS ARC下命名规则
当我在ARC模式下写以下代码的时候,编译器报错 Semantic Issue: Property's synthesized getter follows Cocoa naming conventio ...
- (转)SQL盲注攻击的简单介绍
转:http://hi.baidu.com/duwang1104/item/65a6603056aee780c3cf2968 1 简介 1.1 普通SQL注入技术概述 目前没有对SQL ...
- SGLTE/SVLTE、CSFB、SRVCC概念
SGLTE:Simultaneous GSM and LTE,手机可以同时驻留在GSM和LTE网络中,打电话通过GSM网络进行,数据业务通过LTE网络进行. SVLTE:Simultaneous V ...