LintCode之回文数

题目描述:

我的代码:

 1 public class Solution {
 2     /*
 3      * @param num: a positive number
 4      * @return: true if it's a palindrome or false
 5      */
 6     public boolean isPalindrome(int num) {
 7         // write your code here
 8         int i=0,count=0;
 9         int n = num;
10         while(n != 0) {
11             n/=10;
12             count++;
13         }
14         int[] a = new int[count];
15         int[] c = new int[count];
16         boolean b = true;
17         while(num != 0) {
18             a[i] = num%10;
19             num/=10;
20             i++;
21         }
22         i=0;
23         //将数组a的值从后往前赋值给c
24         for(int j=a.length-1; j>=0; j--) {
25             c[i] = a[j];
26             i++;
27         }
28         for(int j=0; j<a.length; j++) {
29             if(a[j] != c[j]) {
30                 b = false;
31                 break;
32             }
33         }
34         return b;
35     }
36 }

总结:这道题我用了两个数组,其中数组a存放num的各位数字,然后将数组a从后至前赋值给数组c,此时数组a中的值得顺序与数组c中的值得顺序是相反的(例如:num=32,那么a=[3,2],c=[2,3]),如果该数是回文的话,那么这两个数组按相同的顺序从前往后读的值也应该是相同的。

 

posted @ 2017-10-26 08:41  zwt3  阅读(154)  评论(0编辑  收藏  举报