class Solution: # @return an integer def reverse(self, x): if not x: return x s = str(x) first = s[0] result = 0 if (first.isdigit()): sub = int(s[::-1]) if sub < 2147483648: result = sub else: sub = int(s[1:][::-1]) if sub < 2147483648: result = int(first + s[1:][::-1]) return result
class Solution { public: int titleToNumber(string s) { int result = 0; for (int ii = 0; ii < s.size(); ++ii) { result = result * 26 + s[ii] - 'A' + 1; } return result; } };
class Solution { public: string countAndSay(int n) { string s = "1"; for (int ii = 0; ii < n-1; ++ii) { s = getNext(s); } return s;
}
string getNext(string s) { string say = ""; for(int ii = 0; ii < s.size(); ++ii) { int repeat = countRepeat(s, ii); say.push_back(repeat + '0'); say.push_back(s[ii]); ii += repeat-1; } return say; }
unsigned int countRepeat(string s, unsigned int pos) { char c = s[pos]; int ii = pos; for(; ii < s.size(); ++ii){ if (s[ii] != c) break; } return ii - pos; } };
Total Accepted:10377Total Submissions:70133My Submissions
Compare two version numbersversion1andversion1.
Ifversion1>version2return 1, ifversion1<version2return -1, otherwise return 0.
You may assume that the version strings are non-empty and contain only digits and the.character.
The.character does not represent a decimal point and is used to separate number sequences.
For instance,2.5is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.
Here is an example of version numbers ordering:
0.1 < 1.1 < 1.2 < 13.37
Credits:
Special thanks to@tsfor adding this problem and creating all test cases.