Skip to content

Commit 6e42aa0

Browse files
author
ssjssh
committed
完成leecode第9题。
1 parent 8091a52 commit 6e42aa0

File tree

2 files changed

+54
-51
lines changed

2 files changed

+54
-51
lines changed

src/ssj/leecode/palindrome_number.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
*Determine whether an integer is a palindrome. Do this without extra space.
3+
*/
4+
5+
/**
6+
可以直接把int逆转,然后只需要判断他们是不是相等就可以了
7+
关于溢出的问题
8+
1,如果原来的数字没有溢出,那么如果他们是回文,那么逆转后的数字也不会越界。这样保证如果是回文,那么就不会越界。
9+
2,如果原来的数字不是回文,那么逆转后的数字一定不会溢出,因此一旦在逆转数字的时候出现溢出,那么就可以判断这个数字不是回文
10+
**/
11+
12+
#include <stdio.h>
13+
#include <limits.h>
14+
#include <stdlib.h>
15+
#include <stdbool.h>
16+
17+
bool isPalindrome(int x){
18+
if(x<0) return 0;
19+
int res = 0;
20+
int tmp = x;
21+
while (x != 0) {
22+
/**
23+
* 假设INT_MAX / 10 = m,因为他是一个浮点数,那么假设他在n-1,n之间
24+
* 那么也就是abs(res)的最大值是n-1,这个数字是在接下来的计算中可能会越界(依赖于x % 10的值)
25+
* 如果abs(res)为n,那么就一定会发生越界
26+
*/
27+
if (abs(res) > INT_MAX / 10 || abs(res) * 10 > INT_MAX - abs(x) % 10) return 0;
28+
res = res * 10 + x % 10;
29+
x /= 10;
30+
}
31+
return res==tmp;
32+
}
33+
34+
int main(){
35+
printf("%d\n",isPalindrome(1));
36+
printf("%d\n",isPalindrome(0));
37+
printf("%d\n",isPalindrome(12311321));
38+
printf("%d\n",isPalindrome(-12311321));
39+
printf("%d\n",isPalindrome(-2147447412));
40+
return 0;
41+
}

src/ssj/leecode/reverse_integer.c

Lines changed: 13 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,22 @@
33
*/
44

55
#include <stdio.h>
6-
#include <string.h>
76
#include <limits.h>
7+
#include <stdlib.h>
88

9-
10-
int intToString(int i,char *arr){
11-
int t_len = sprintf(arr,"%d",i);
12-
arr[t_len] = '\0';
13-
return t_len;
14-
}
15-
int willOverflow(char *result,int is_negative){
16-
char min_str[40];
17-
char max_str[40];
18-
int max_len = intToString(INT_MAX,max_str);
19-
int min_len = intToString(INT_MIN,min_str);
20-
int result_len = strlen(result);
21-
if((is_negative&&result_len<min_len) || (!is_negative&&result_len<max_len)){
22-
return 0;
23-
}
24-
return is_negative?(strcmp(min_str,result)<0):(strcmp(max_str,result)<0);
25-
}
269
int reverse(int x) {
27-
char *s_char,*tar_c;
28-
int result=0;
29-
char intStr[40];
30-
int is_negative=0;
31-
tar_c = intStr;
32-
if(x<0){
33-
*tar_c = '-';
34-
tar_c++;
35-
x = -x;
36-
is_negative=1;
37-
}
38-
39-
while(x > 0){
40-
*tar_c = x%10+'0';
41-
x /= 10;
42-
tar_c++;
43-
}
44-
*tar_c='\0';
45-
if(willOverflow(intStr,is_negative)){
46-
return 0;
47-
}
48-
for(tar_c = intStr;(*tar_c)!='\0';tar_c++){
49-
if ((*tar_c)!='-'){
50-
51-
result *= 10;
52-
result += (*tar_c)-'0';
53-
}
54-
}
55-
if(intStr[0]=='-'){
56-
result = -result;
57-
}
58-
59-
return result;
10+
int res = 0;
11+
while (x != 0) {
12+
/**
13+
* 假设INT_MAX / 10 = m,因为他是一个浮点数,那么假设他在n-1,n之间
14+
* 那么也就是abs(res)的最大值是n-1,这个数字是在接下来的计算中可能会越界(依赖于x % 10的值)
15+
* 如果abs(res)为n,那么就一定会发生越界
16+
*/
17+
if (abs(res) > INT_MAX / 10 || abs(res) * 10 > INT_MAX - abs(x) % 10) return 0;
18+
res = res * 10 + x % 10;
19+
x /= 10;
20+
}
21+
return res;
6022
}
6123

6224

0 commit comments

Comments
 (0)