码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode记录之9——Palindrome Number

时间:2017-09-02 23:23:32      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:string类   nbsp   for   string   str   题目   isa   determine   ace   

  LeetCode真是个好东西,本来闲了一下午不想看书,感觉太荒废时间了就来刷一道题。能力有限,先把easy的题目给刷完。


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

   确定一个整数是否是回文。 做这个没有额外的空间。


  这道题毕竟是easy级别的,花了大概5分钟就写出来了。我的思路就是判断回文要首尾一一对照么,如果把int转换成string类型的话比较字符就方便多了。

class Solution {
    public boolean isPalindrome(int x) {
        boolean isAbove=true;
        if(x<0){
            return false;
        }
        String num=x+"";
        int length=num.length();
        for(int i=0;i<length/2;i++){
            if(num.charAt(i)!=num.charAt(length-i-1))
                return false;
        }
        return true;
    }
}

 

LeetCode记录之9——Palindrome Number

标签:string类   nbsp   for   string   str   题目   isa   determine   ace   

原文地址:http://www.cnblogs.com/vincentme/p/7468220.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!