Skip to content

Commit b79e832

Browse files
committed
ancient code
0 parents  commit b79e832

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

7.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
};

8.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
};

88.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
};

0 commit comments

Comments
 (0)