• 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] 输出:""

    提示:

    1. A.length == 4
    2. 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 "";
    	}
    };
  • 相关阅读:
    求欧拉回路的算法学习
    2020牛客暑期多校训练营(第六场 )C Combination of Physics and Maths(思维)
    2020牛客暑期多校训练营(第六场)E.Easy Construction(思维构造,附图解)
    CF1038D Slime(思维+枚举+贪心)(来自洛谷)
    CF1250B The Feast and the Bus(贪心+枚举)(来自洛谷)
    Codeforces Round #659 (Div. 2) A.Common Prefixes
    IDEA本人亲测可用的破解方法
    Codeforces Round #658 (Div. 2)(A,B博弈,C1,C2)
    2020牛客暑期多校训练营(第四场)B.Basic Gcd Problem(数学)
    2020牛客暑期多校训练营(第三场)B.Classical String Problem(思维)
  • 原文地址:https://www.cnblogs.com/lMonster81/p/10433786.html
Copyright © 2020-2023  润新知