isValid("23:15") 在应该返回 1 时返回 0
bool isValid(string s){
int pos = s.find(":");
if(s.length() < 4 || s.length() > 5)
return false;
else if(s.length() == 5)
{
if(s[0] > 2)
return false;
}
if(s[pos + 1] > 5 )
{
return false;
}
return true;
}
实际输出= 0
最佳答案
我猜你把数字和数字搞混了。
if(s[0] > 2)
应该是
if (s[0] > '2')
和
if(s[pos + 1] > 5 )
应该是
if (s[pos + 1] > '5')
您还需要考虑如果 s
不包含冒号会发生什么情况。
关于c++ - bool 函数返回意外值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57639249/