zoukankan      html  css  js  c++  java
  • Palindrome Number(回文)

     1 public class Solution{
     2     public boolean isPalindrome(int x) {
     3         //int count=1;
     4         int count1=0;
     5         int count2=0;
     6         int []bs=new int[100];
     7        if (x<0){//当输入的值为负的时候,就不是回文了
     8            System.out.println("false");
     9                return false;
    10        }
    11         String s=String.valueOf(x);//将输入的int型x转为字符串型
    12         int t =s.length();//求出字符串型的x的长度
    13         if(x>0&&x<10){//当输入的值只有一位数一定是回文
    14             System.out.println("true");
    15             return true;
    16         }
    17       //这一步其实不太懂,经过反复的代码验证可以得出,输入的值的位数是有算上符号的
    18         /*if(x>0)
    19             t=t;
    20         else t=t-1;
    21         //所以当输入的值为负的时候需要减一。再次回顾发现既然只要负数就不是回文,那么这段代码就是累赘了
    22         //System.out.println(t);
    23         x=Math.abs(x);*/
    24         if(x!=0){
    25             for(int i =0;i<t;i++){
    26                 bs[i]=x%10;
    27                 x=(x-bs[i])/10;
    28                 //count++;   //x的长度
    29                 //System.out.println(bs[i]);
    30             }
    31         }
    32         if(t%2!=0){//当x为奇数的时候
    33             for(int j=0;j<t/2;j++){
    34                 if(bs[(t/2-j-1)]==bs[(t/2+j+1)])
    35                 {
    36                     count1++;
    37                     //System.out.println(count1);
    38                 }
    39                 else{
    40                     System.out.println("false");
    41                     return false;
    42                     //System.exit(-1);//强制退出
    43                 }
    44              }
    45         }
    46       
    47     
    48         
    49          if(t%2==0){
    50             for(int j2=0;j2<t/2;j2++){
    51                 if(bs[(t/2-j2-1)]==bs[(t/2+j2)]){
    52                     count2++;
    53                     
    54                 }
    55                 else{
    56                     System.out.println("false");
    57                     //System.exit(-1);
    58                     return false; 
    59                 }
    60             }
    61          }
    62              //System.out.println(count2);
    63             if((count2==t/2)||(count1==t/2)){
    64                 System.out.println("true");
    65                 return true;
    66             }
    67             else{       
    68                 System.out.println("false");
    69                 return false;
    70             }
    71       
    72         
    73         }
    74         
    75             
    76        
    77     }
    78 
    79 /*class a{
    80     public static void main(String[] args){
    81         // TODO Auto-generated method stub
    82         Solution s = new Solution();
    83         s.isPalindrome(1001);
    84     }
    85 }
    86 这一段代码是为了验证添加的
    87 */
  • 相关阅读:
    学习OSGI---建项目,运行配置
    MongoDB 安装
    利用 ^ 异或运算符 进行交换(不需要第三方变量)
    2019HDU暑期多校训练-1004equation-方程求解
    HDU 4417-Super Mario-线段树+离线
    HDU 3333-Turing Tree-线段树+离散+离线
    POJ 2528-Mayor's posters-线段树+离散化
    POJ 2631-Roads in the North-树的直径
    POJ 2299-Ultra-QuickSort-线段树的两种建树方式
    noip2009最优贸易——spfa
  • 原文地址:https://www.cnblogs.com/anylemons/p/6441539.html
Copyright © 2011-2022 走看看