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仅仅有一位数.所以返回它. ...
随机推荐
- bootstrap-----流体布局解析
流体布局容器 容器的width为auto,只是两边加了15px的padding. 流体布局容器 容器的width为auto,只是两边加了15px的padding. <div class=&quo ...
- C++相对路径和绝对路径
学习备忘 转自:http://www.cnblogs.com/vranger/p/3820783.html 电脑硬盘E盘下,建文件夹“test”,"test"下建立子文件夹“fil ...
- leetcode-219-存在重复元素②
题目描述: 第一次提交:超时 class Solution: def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool ...
- 单独编译和使用webrtc音频降噪模块(附完整源码+测试音频文件)
单独编译和使用webrtc音频增益模块(附完整源码+测试音频文件) 单独编译和使用webrtc音频回声消除模块(附完整源码+测试音频文件) webrtc的音频处理模块分为降噪ns,回音消除aec,回声 ...
- System.Web.Mvc.HttpDeleteAttribute.cs
ylbtech-System.Web.Mvc.HttpDeleteAttribute.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral ...
- 使用docker安装redis
1.安装docker .检查内核版本,必须是3.10及以上 [root@localhost ~]# uname -r .安装docker [root@localhost ~]# yum install ...
- PAT甲级——A1107 Social Clusters
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- 建立ftp服务器和客户端
参考:https://www.cnblogs.com/judes/p/9546447.html 补充: 权限设置:如下所示,如果需要上传文件需要勾选write权限,需要在文件中添加内容勾选append ...
- ubuntu install redis/mongo 以及 监控安装
sudo apt-get updatesudo apt-get install redis-server mongodb sudo apt-get install htopsudo apt-get i ...
- 打开新窗口(window.open) open() 方法可以查找一个已经存在或者新建的浏览器窗口。 语法: window.open([URL], [窗口名称], [参数字符串])
打开新窗口(window.open) open() 方法可以查找一个已经存在或者新建的浏览器窗口. 语法: window.open([URL], [窗口名称], [参数字符串]) 参数说明: URL: ...