leetcode 7. Reverse Integer
代码很好写,需要考虑呢的是反置的数字超出int 型的时候就要返回0.这里参考了https://www.cnblogs.com/ariel-dreamland/p/8706403.html这篇博客。
然后把leetcode里面默认给的数据类型也改了
long long int reverse(int x) { int i; long long int result=0; int y=x; int k; int num=0; while(y) { y=y/10; num++; } //printf("%d\n",num); k=num; for(i=0;i<k;i++) { //printf("%d ",x%10); result+=((x%10)*(long long int)pow(10,--num)); //printf("%lld ",(long long int)pow(10,num)); //printf("%lld %d %d\n",result,x,num); x=x/10; } // printf("%lld",result); if(result>pow(2,31)-1||result<-pow(2,31)) return 0; else return result; }
Your input
1534236469
Output
0
0
0
Diff
Expected
Line 50: ValueError: invalid literal for int() with base 10: ''
这里testcase报错了,但是我提交了还是过了,这个错误信息暂时还没弄明白?
posted on 2019-01-06 21:29 Gavinthoms 阅读(...) 评论(...) 编辑 收藏