Leetcode-539: encountered a big problem when brushing the minimum time difference:
Errors are reported as follows:
terminate called after throwing an instance of 'std::length_error'
what(): basic_string::_M_create
The error code is as follows:
class Solution {
public:
static bool cmp(string x,string y){ //Handwritten sorting rules Sort by chronological ascending order
int Xshi= (x[0]-'0')*10+x[1]-'0';
int Xfen= (x[3]-'0')*10+x[4]-'0';
int Yshi= (y[0]-'0')*10+y[1]-'0';
int Yfen= (y[3]-'0')*10+y[4]-'0';
if(Xshi>Yshi || (Xshi==Yshi && Xfen> Yfen)) return false;
else return true;
}
//input time has been sorted x is less than y to find two time minute difference function
int Time_diff(string x,string y){
int Xshi= (x[0]-'0')*10+x[1]-'0';
int Xfen= (x[3]-'0')*10+x[4]-'0';
int Yshi= (y[0]-'0')*10+y[1]-'0';
int Yfen= (y[3]-'0')*10+y[4]-'0';
int diff=(Yshi-Xshi)*60;
if(Yfen>Xfen) diff+=Yfen-Xfen;
else diff-=Xfen-Yfen;
return diff;
}
int findMinDifference(vector<string>& timePoints) {
//After sorting, compare the two adjacent to each other and also compare the first and last
sort(timePoints.begin(),timePoints.end(),cmp);
//compare the first and the last
int T_min=min( Time_diff(timePoints[0],timePoints[timePoints.size()-1]),
1440-(Time_diff(timePoints[0],timePoints[timePoints.size()-1])) );
//Two adjacent to each other for comparison
for(int i=1;i<timePoints.size();i++)
{
T_min=min(T_min, Time_diff(timePoints[i-1],timePoints[i]) );
}
return T_min;
}
};
Looking back and forth at the code, I was stunned that I couldn’t see any problems. I consulted hundreds of relevant web pages, basically for the following two reasons:
-
- an empty string was entered. The array subscript is out of bounds
However, these two problems are obviously not in my code. Finally, I checked all night and found the official description of sort sorting comparison.
https://en.cppreference.com/w/cpp/named_req/Compare
Let’s focus here:
In other words, for the CMP comparison function we handwritten, the following three points should be met:
-
-
- for all input a, input two identical elements, namely CMP (a, a). If the returned result is false, it cannot be false. If the result of entering CMP (a, b) is true, conversely, the result of entering CMP (B, a) is false, that is, if a > b. No more B > a. Otherwise, there will be contradictions. If the result of input CMP (a, b) is true, and the result of input CMP (B, c) is true, then the result of CMP (a, c) should also be true, which is also well understood. If a > B and B > C then a must be greater than C
-
Here, we should mainly pay attention to the first point. When the two numbers are the same, the returned result should be false, and my code:
static bool cmp(string x,string y){ //Handwritten sorting rules Sort by chronological ascending order
int Xshi= (x[0]-'0')*10+x[1]-'0';
int Xfen= (x[3]-'0')*10+x[4]-'0';
int Yshi= (y[0]-'0')*10+y[1]-'0';
int Yfen= (y[3]-'0')*10+y[4]-'0';
if(Xshi>Yshi || (Xshi==Yshi && Xfen> Yfen)) return false;
else return true;
}
This rule is not satisfied. So add one = Number to make it comply with the rules.
static bool cmp(string x,string y){ //Handwritten sorting rules Sort by chronological ascending order
int Xshi= (x[0]-'0')*10+x[1]-'0';
int Xfen= (x[3]-'0')*10+x[4]-'0';
int Yshi= (y[0]-'0')*10+y[1]-'0';
int Yfen= (y[3]-'0')*10+y[4]-'0';
if(Xshi>Yshi || (Xshi==Yshi && Xfen>= Yfen)) return false;
else return true;
}
The problem that bothered me all night was solved.
Read More:
- c++ terminate called after throwing an instance of ‘std::system_error‘ what(): Unknown error -1
- How to Fix distributed training report terminate called after throwing an instance of’std::length_error’
- Runtime error: terminate called after throwing an instance of ‘STD:: Logic_ error’
- C# Member XXX cannot be accessed with an instance with an instance reference;qualify it with a type
- Differences between length() size() and C strlen() of C + + string member functions
- Python – get the information of calling function from called function
- The C language qsort() function reports an error for overflow of – 2147483648 and 2147483648
- error C2784: ‘bool std::operator <(const std::_Tre
- Error: Exception was raised when calling per-thread-terminate function in extension lrwreplaymain.dl
- Error c2259 cannot instance abstract class due to following members
- Teradata bteq does not support the length function
- Pandas sort according to a column_ values)
- The reason and solution of the error of join function: sequence item 0: expected STR instance, int found
- Map to vector pair map.second sort
- C / C + + rounding function: round function
- Solve the problem of error: cannot pass objects of non trivially copyable type ‘STD:: String’ in C / C + +
- C / C + + library function (tower / tower) realizes the conversion of letter case
- Analysis of R language error replacement has length zero problem
- An undetermined call to function ‘shell’: missing ‘. Stop. Problem encountered when using shell command in makefile