谷歌面试原题:最大时刻
LintCode 1871
最大时刻
题目描述 给一个24小时制的时间(00:00-23:59),其中有一个或多个数字是问号。问号处可以用任何一个数字代替,问可以表示的最大时间是多少。
题目描述
扫码免费做题 ↓↓↓
样例 1
输入:
time = "2?:00"
输出:
"23:00"
样例 2
输入:
time = "??:??"
输出:
"23:59"
解题思路
直接对于问号处的数字变成最大,要判断是否满足时间的标准。
复杂度分析
时间复杂度:O(1)
空间复杂度:O(1)
源代码
publicclassSolution{
/**
* @param time: a string of Time
* @return: The MaximumMoment
*/
public String MaximumMoment(String time){
char[] timeChars = time.toCharArray();
if (timeChars[0] == '?') {
if (timeChars[1] <= '9' && timeChars[1] >= '4')
timeChars[0] = '1';
else
timeChars[0]='2';
}
if (timeChars[1] == '?') {
if (timeChars[0] != '2')
timeChars[1] = '9';
else
timeChars[1] = '3';
}
if (timeChars[3] == '?')
timeChars[3] = '5';
if (timeChars[4] == '?')
timeChars[4] = '9';
return String.valueOf(timeChars);
}
}
查看完整代码,向左滑动
点击【阅读原文】,查看领扣原题。
阅读原文 最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。