java判斷是不是迴文數

public class HuiWenTest  
{  
   
 //方法一:若爲迴文數,則反轉後與原來的數應該相等  
 public  static boolean isCircleNumber1(int num)  
 {  
    String temp = String.valueOf(num);  
    StringBuffer sb = new StringBuffer(temp);  
    return sb.reverse().toString().equals(temp);  
  }   
//方法二:首位與末尾位是否相等判斷,若爲迴文數,則必然相等 
 public static boolean isCircleNumber2(int num){  
    boolean flag = true;  
    String temp = String.valueOf(num);  
    char[] arr =  temp.toCharArray();  
    int len =arr.length;  
    for(int i=0,j=len-1;i<j;){  
     if(arr[i++] == arr[j--]){  
     }else{  
      flag = false;  
      break;  
     }  
    }  
   return flag;  
 }  
    public static void main(String args[]) {  
       int num = 12321;   
       System.out.println(isCircleNumber1(num));  
       System.out.println(isCircleNumber2(num));   
   }  
   
}
相關文章
相關標籤/搜索