自已一开始的思路,模拟。代码如下:
出现的错误
1:小时模拟代码比较复杂。
2:数字->字符:+‘0’
3:字符转数字:-‘0’
思路二:都分别直接转化为距离00:00过了多少分钟,也就是转化成60进制
class Solution {
public:
int convertTime(string cu, string co) {
int a = stoi(cu.substr(0, 2)) * 60 + stoi(cu.substr(3, 2));
int b = stoi(co.substr(0, 2)) * 60 + stoi(co.substr(3, 2));
int c = (b - a + 1440) % 1440; //
int ans = c / 60;
c = c % 60;
ans += c / 15;
c = c % 15;
ans += c / 5;
c = c % 5;
ans += c;
return ans;
}
};
注意: 如果题目没有cu <= co的话这一步要加:c = (b - a + 1440) % 1440;