leetcode-9 palindrome-number(回文数)

先看一下题目描述:

 

 我们应该都知道回文数是什么意思了,应该想到数组来存储数字,但是怎样进行判断呢。首先负数和0直接返回false,

再对数组中的第一位和最后一位比较,第二位和倒数第二位比较,因为回文数是高度对称的。

     public static boolean isPalindrome(int x) {
          ArrayList<Integer> list = new ArrayList<>();
          if(x==0)return true;
          if(x<0)return false;
          while(x!=0){
          list.add(x%10);
          x = x/10;
     }
          int head =0;
          int tail = list.size()-1;
          while (head<tail){
              int headint = list.get(head);
              int tailint = list.get(tail);
              if(headint!=tailint)return false;
              head++;
              tail--;
          }
        return true;
     }
}

 

 

 

 

 

s

 

d

 

s

 

posted @ 2018-11-21 11:18  青衫z  阅读(190)  评论(0)    收藏  举报