LeetCode刷题之Palindrome Number

Problem

Determine whether an integer is a palindrome. Do this without extra space.

My Solution

public class Solution {
    int[] numbers = new int[20];

    public boolean isPalindrome(int x) {
        if (x >= 0) {
            if (reverse(x) == x) {
                return true;
            }
        }
        return false;
    }

    public int reverse(int x) {
        int i = 0, rX = 0, count = 0;
        for (i = 0; x != 0; i++) {
            numbers[i] = x % 10;
            x /= 10;
        }
        count = i;
        for (i = 0; i < count; i++) {
            rX = rX * 10 + numbers[i];
        }
        return rX;
    }
}
Great Solution

public boolean isPalindrome(int x) {
    if(x<0 || (x!=0 && x%10==0))
        return false;
    int res = 0;
    while(x>res){
        res = res*10 + x%10;
        x = x/10;
     }
    return (x==res || x==res/10);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,486评论 0 10
  • 我没想到吃年糕火锅会凌晨爬起来呕吐 我没想到看到餐厅窗外烂醉发疯的大叔会心生怜悯 我没想到有一天会答应耶稣教徒的应...
    穆清yvonne阅读 213评论 2 2
  • 我想说的是我姑父和姑妈的故事,在这个物质纵横的社会,房子车子票子成为了每个人心目中的目标,安全感的缺失,幸福感的缺...
    我是沐浠阅读 648评论 0 3
  • 生活不止眼前的苟且,还有诗和远方。 做一个简单、潇洒、坚定的女孩儿。 面对烦恼要大大咧咧,没心没...
    Healer17阅读 193评论 0 1
  • 没有任何的散去,只有人散,没有曲终。 心里的想法只有自己知道,也许也说不清,每一次的在一起是为了去分开,亲密关系是...
    Serene汤先允阅读 105评论 0 0