File tree Expand file tree Collapse file tree 3 files changed +92
-0
lines changed Expand file tree Collapse file tree 3 files changed +92
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ int reverse (int x) {
4+ long result = 0 ;
5+
6+ while (x != 0 )
7+ {
8+ result *= 10 ;
9+ result += x % 10 ;
10+ x /= 10 ;
11+ }
12+
13+ if (x < 0 )
14+ {
15+ result *= -1 ;
16+ }
17+
18+ if (result > 2147483647 || result < -2147483648 )
19+ {
20+ return 0 ;
21+ }
22+
23+ return (int )result;
24+ }
25+ };
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ int myAtoi (string str) {
4+
5+ long ret = 0 ;
6+ bool minus = false ;
7+ int i = 0 ;
8+
9+ while (isspace (str[i]))
10+ {
11+ ++i;
12+ }
13+
14+ if (str[i] == ' +' || str[i] == ' -' )
15+ {
16+ if (str[i] == ' -' )
17+ {
18+ minus = true ;
19+ }
20+ ++i;
21+ }
22+
23+
24+ while (isdigit (str[i]) && str[i] != ' \0 ' )
25+ {
26+ ret = ret * 10 + str[i] - ' 0' ;
27+ ++i;
28+
29+ if (ret > INT_MAX)
30+ {
31+ return minus ? INT_MIN : INT_MAX;
32+ }
33+ }
34+
35+ if (minus)
36+ {
37+ ret *= -1 ;
38+ }
39+
40+ return (int )ret;
41+ }
42+ };
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ void merge (vector<int >& nums1, int m, vector<int >& nums2, int n) {
4+ int i = m-1 ;
5+ int j = n-1 ;
6+ int k = m + n - 1 ;
7+
8+ while (i >= 0 && j >= 0 )
9+ {
10+ if (nums1[i] < nums2[j])
11+ {
12+ nums1[k--] = nums2[j--];
13+ }
14+ else
15+ {
16+ nums1[k--] = nums1[i--];
17+ }
18+ }
19+
20+ while ( j >= 0 )
21+ {
22+ nums1[k--] = nums2[j--];
23+ }
24+ }
25+ };
You can’t perform that action at this time.
0 commit comments