public class Solution
{
public string LargestTimeFromDigits(int[] A)
{
var list = Perm(A, );
var maxHour = -;
var maxMinute = -;
foreach (var nums in list)
{
var hour = nums[].ToString() + nums[].ToString();
var minute = nums[].ToString() + nums[].ToString();
var strtime = hour + ":" + minute;
if (ValidTime(strtime))
{
var Inthour = Convert.ToInt32(hour);
var IntMinute = Convert.ToInt32(minute);
if ((Inthour > maxHour) || (Inthour == maxHour && IntMinute >= maxMinute))
{
maxHour = Inthour;
maxMinute = IntMinute;
}
}
}
if (maxHour >= )
{
var tempHour = maxHour.ToString();
var tempMinute = maxMinute.ToString();
if (tempHour.Length == )
{
tempHour = "" + tempHour;
}
if (tempMinute.Length == )
{
tempMinute = "" + tempMinute;
}
return tempHour + ":" + tempMinute;
}
else
{
return "";
}
} private void Swap<T>(ref T a, ref T b)
{
T t = a;
a = b;
b = t;
}
private IEnumerable<int[]> Perm(int[] arr, int pos)
{
if (pos == arr.Length)
{
yield return arr;
}
for (int i = pos; i < arr.Length; ++i)
{
Swap(ref arr[i], ref arr[pos]);
foreach (var j in Perm(arr, pos + )) yield return j;
Swap(ref arr[i], ref arr[pos]);
}
} /// <summary>
/// 判断时间格式是否合法
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
private bool ValidTime(string time)
{
var part = time.Split(':');
var hour = Convert.ToInt32(part[]);
var minute = Convert.ToInt32(part[]);
if (hour >= && hour <= && minute >= && minute <= )
{
return true;
}
return false;
}
}

这题又长又卵,没意思。

05-06 19:34