给定一个字符串S ,以24 小时格式“HH:MM”表示时间,其中一些数字用“?”表示。 , 任务是替换‘?’任何可能的数字,使得结果时间是最大可能的时间。
例子:
Input: S = “?4:5?”
Output: 14:59
Explanation:
After replacing the first and the second ‘?’ with the digits 1 and 9, modifies the given time to “14:59”, which is maximum among all the possible time that can be made by replacing ‘?’.
Input: S = “0?:??”
Output: 09:59
方法:给定的问题可以通过遍历给定的字符串S并替换‘?’来解决。以这样的方式,字符‘:’之前的子字符串位于[0, 23]范围内, ‘:’之后的子字符串最多必须为59并打印获得的最大时间。按照以下步骤解决给定的问题:
- 如果索引0处的字符S的值为‘?’并且索引1处的字符是‘3’或‘?’ ,然后将S[0]的值更新为‘2’ 。否则,将S[0]的值更新为‘1’ 。
- 如果索引1处的字符S的值为‘?’并且索引0处的字符不是‘2’ ,然后将S[1]的值更新为‘9’ 。否则,将S[1]的值更新为‘3’ 。
- 如果索引3处的字符S的值为‘?’ ,然后将S[3]的值更新为‘5’ 。
- 如果索引4处的字符S的值为‘?’ ,然后将S[4]的值更新为‘9’ 。
- 完成上述步骤后,打印字符串S的值作为结果时间。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum time
// by replacing '?' by any digits
void maxTime(string s)
{
// Convert the string to the
// character array
// If the 0th index is '?'
if (s[0] == '?') {
if (s[1] <= '3' || s[1] == '?')
s[0] = '2';
else
s[0] = '1';
}
// If the 1st index is '?'
if (s[1] == '?') {
if (s[0] != '2') {
s[1] = 9;
}
else
s[1] = 3;
}
// If the 3rd index is '?'
if (s[3] == '?')
s[3] = '5';
// If the 4th index is '?'
if (s[4] == '?')
s[4] = '9';
// Return new string
cout << s << endl;
}
// Driver Code
int main()
{
string S = "?4:5?";
maxTime(S);
return 0;
}
// This code is contributed by Potta Lokesh
Java
// Java program for the above approach
public class Main {
// Function to find the maximum time
// by replacing '?' by any digits
public static void maxTime(String S)
{
// Convert the string to the
// character array
char[] s = S.toCharArray();
// If the 0th index is '?'
if (s[0] == '?') {
if (s[1] <= '3' || s[1] == '?')
s[0] = '2';
else
s[0] = '1';
}
// If the 1st index is '?'
if (s[1] == '?') {
if (s[0] != '2') {
s[1] = 9;
}
else
s[1] = 3;
}
// If the 3rd index is '?'
if (s[3] == '?')
s[3] = '5';
// If the 4th index is '?'
if (s[4] == '?')
s[4] = '9';
// Return new string
System.out.println(
new String(s));
}
// Driver Code
public static void main(String[] args)
{
String S = "?4:5?";
maxTime(S);
}
}
// This code is contributed by lokeshpotta20.
Python3
# Python3 program for the above approach
# Function to find the maximum time
# by replacing '?' by any digits
def maxTime(s):
# Convert the string to the
# character array
# If the 0th index is '?'
s = list(s)
if (s[0] == '?'):
if (s[1] <= '3' or s[1] == '?'):
s[0] = '2'
else:
s[0] = '1'
# If the 1st index is '?'
if (s[1] == '?'):
if (s[0] != '2'):
s[1] = 9
else:
s[1] = 3
# If the 3rd index is '?'
if (s[3] == '?'):
s[3] = '5'
# If the 4th index is '?'
if (s[4] == '?'):
s[4] = '9'
# Return new string
print("".join(s))
# Driver Code
S = "?4:5?"
maxTime(S)
# This code is contributed by _saurabh_jaiswal
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class gfg {
// Function to find the maximum time
// by replacing '?' by any digits
public static void maxTime(String S)
{
// Convert the string to the
// character array
char[] s = S.ToCharArray();
// If the 0th index is '?'
if (s[0] == '?') {
if (s[1] <= '3' || s[1] == '?')
s[0] = '2';
else
s[0] = '1';
}
// If the 1st index is '?'
if (s[1] == '?') {
if (s[0] != '2') {
s[1] = '9';
}
else
s[1] = '3';
}
// If the 3rd index is '?'
if (s[3] == '?')
s[3] = '5';
// If the 4th index is '?'
if (s[4] == '?')
s[4] = '9';
// Return new string
Console.Write(new String(s));
}
// Driver Code
public static void Main(String[] args)
{
String S = "?4:5?";
maxTime(S);
}
}
// This code is contributed by shivanisinghss2110
Javascript
输出:
14:59
时间复杂度: O(1)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。