Uriel's Corner

Research Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learning
posts - 0, comments - 50, trackbacks - 0, articles - 594

[LeetCode]Reverse Integer-2014.01.08

Posted on 2014-01-11 02:11 Uriel 阅读(79) 评论(0)  编辑 收藏 引用 所属分类: LeetCode
将一个数字翻转,水题,每次取模然后求和就行,但是要注意负数的处理!

 1 class Solution {
 2 public:
 3     int reverse(int x) {
 4         int fg = 1;
 5         int tp = x, nt = 0;
 6         if(x < 0) {
 7             fg = 0;
 8             tp = -tp;
 9         }
10         while(tp > 0) {
11             nt = nt * 10 + (tp % 10);
12             tp /= 10;
13         }
14         if(!fg) nt = -nt;
15         return nt;
16     }
17 };

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理