Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit88a78d8

Browse files
author
ssjssh
committed
完成leecode第7题。
1 parentd89a2c8 commit88a78d8

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

‎.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,8 @@ docs/_build/
5858
# PyBuilder
5959
target/
6060

61-
test.py
61+
test.py
62+
63+
#c out file
64+
*.out
65+
*.exe

‎src/ssj/leecode/reverse_integer.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
*本题目使用C编写,因为Python的int类型是无限大的,要判断一个数字是否溢出非常简单,祝需要和2^32-1比较就好了
3+
*/
4+
5+
#include<stdio.h>
6+
#include<string.h>
7+
#include<limits.h>
8+
9+
10+
intintToString(inti,char*arr){
11+
intt_len=sprintf(arr,"%d",i);
12+
arr[t_len]='\0';
13+
returnt_len;
14+
}
15+
intwillOverflow(char*result,intis_negative){
16+
charmin_str[40];
17+
charmax_str[40];
18+
intmax_len=intToString(INT_MAX,max_str);
19+
intmin_len=intToString(INT_MIN,min_str);
20+
intresult_len=strlen(result);
21+
if((is_negative&&result_len<min_len)|| (!is_negative&&result_len<max_len)){
22+
return0;
23+
}
24+
returnis_negative?(strcmp(min_str,result)<0):(strcmp(max_str,result)<0);
25+
}
26+
intreverse(intx) {
27+
char*s_char,*tar_c;
28+
intresult=0;
29+
charintStr[40];
30+
intis_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+
return0;
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+
returnresult;
60+
}
61+
62+
63+
64+
intmain(){
65+
printf("%d\n",reverse(1231312));
66+
printf("%d\n",reverse(123));
67+
printf("%d\n",reverse(-1231312));
68+
printf("%d\n",reverse(1000000003));
69+
printf("%d\n",reverse(-1000000003));
70+
return0;
71+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp