[LeetCode][Java] Palindrome Number

來源:互聯網
上載者:User

標籤:leetcode   java   palindrome number   

題目:

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

Some hints:

Could negative integers be palindromes? (ie, -1)

If you are thinking of converting the integer to string, note the restriction of using extra space.

You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?

There is a more generic way of solving this problem.

題意:

判斷一個整數是否為迴文數。不要利用額外的空間。

一些提示:負數不是迴文數;如果考慮將整數轉化為字串,注意空間的限制;如果嘗試轉置這個整數,要注意溢出的問題。

演算法分析:

這裡利用題目《

Reverse Integer 》,將給定的整數轉置,然後判斷轉置後的整數和原整數是否相等,如果相等就是迴文數。這裡注意一些特殊條件的判斷。

AC代碼:

public class Solution {private  int i;private  int labelnum;private  long finalnum;private  int finalnnum;private  int checkresult;private  int label;    private int y;    private boolean blabel;    public boolean isPalindrome(int x)     {          y=reverse(x);//將原整數進行轉置          if (y==0)          {          if(x==y) blabel=true;//轉制前後都是0          else blabel=false;//原整數不是0,轉置後變為0,表明出現溢出          }          else if(x<0)//負數不是迴文數            blabel=false;          else if(x==y)            blabel=true;          else             blabel=false;          return blabel;    }    public int reverse(int x)     {    String s1=Integer.toString(x);    if(s1.charAt(0)=='-')    {    String s2="-";    String finals2="-";    String Judges2="";    long num1=0L;    for(i=s1.length()-1;i>=1;i--) s2+=s1.charAt(i);    for(i=1;i<s2.length();i++)    {    if(s2.charAt(i)!='0')     {    labelnum=i;    break;    }        }    for(i=labelnum;i<s2.length();i++)     {    finals2+=s2.charAt(i);    }    label=checkstring(finals2);//檢查是否存在溢出問題,label=1表明沒有溢出;label=0表明產生溢出    if(label==1)    {    finalnum=Integer.parseInt(finals2);    }    else    {    finalnum=0;    }            }    else    {    String s2="";    String finals2="";    String Judges2="";    long num1=0L;    for(i=s1.length()-1;i>=0;i--) s2+=s1.charAt(i);    for(i=0;i<s2.length();i++)    {    if(s2.charAt(i)!='0')     {    labelnum=i;    break;    }    }    for(i=labelnum;i<s2.length();i++)    {    finals2+=s2.charAt(i);    }    label=checkstring(finals2);//檢查是否存在溢出問題,label=1表明沒有溢出;label=0表明產生溢出    if(label==1)    {    finalnum=Integer.parseInt(finals2);    }    else{    finalnum=0;    }        }    return (int) finalnum;    }      private  int checkstring(String string)     {        checkresult=1;    /* 異常情況1:字串為null */        if (string == null) checkresult=0;          int length = string.length(), offset = 0;        /* 異常情況2:字串長度為0 */        if (length == 0)  checkresult=0;         boolean negative = string.charAt(offset) == '-';        /* 異常情況3:字串為'-' */        if (negative && ++offset == length)  checkresult=0;         int result = 0;    char[] temp = string.toCharArray();        while (offset < length)         {          char digit = temp[offset++];          if (digit <= '9' && digit >= '0')           {            int currentDigit = digit - '0';            /*             * 異常情況4:已經等於Integer.MAX_VALUE / 10,判斷要添加的最後一位的情況:             * 如果是負數的話,最後一位最大是8 如果是正數的話最後一位最大是7             * Int 範圍:四個位元組,-2147483648~2147483647             */            if (result == Integer.MAX_VALUE / 10)             {              if ((negative == false && currentDigit > 7)                  || (negative && currentDigit > 8))               {              checkresult=0;               }              /*               * 異常情況5:已經大於Integer.MAX_VALUE / 10               * 無論最後一位是什麼都會超過Integer.MAX_VALUE               */            }             else if (result > Integer.MAX_VALUE / 10)            {            checkresult=0;             }             int next = result * 10 + currentDigit;             result = next;           }        }return checkresult;    }   }

別人家的代碼:

public class Solution {    public boolean isPalindrome(int x) {        if(x<0) return false;//負數不是迴文        if(x==0) return true;        //對數值進行翻轉,迴文翻轉之後還等於原來的數        int reverseNum=0;        int num=x;        while(num>0)        {            int modnum=num%10;            //考慮翻轉會溢出的情況            if((reverseNum>Integer.MAX_VALUE/10)||((reverseNum==Integer.MAX_VALUE/10)&&(modnum>Integer.MAX_VALUE%10)))                return false;            reverseNum=reverseNum*10+modnum;            num=num/10;        }        if(reverseNum==x)            return true;        else            return false;    }}


著作權聲明:本文為博主原創文章,轉載註明出處

[LeetCode][Java] Palindrome Number

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.