LeetCode--palindrome-number回文数
1、問題
Determine whether an integer is a palindrome. Do this without extra space.
click to show spoilers.
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.
2、分析
判斷一個整數(shù)是否為回文數(shù),不要用額外的空間。
負(fù)數(shù)可以是回文數(shù)嗎?(不可以)
如果你想用整數(shù)轉(zhuǎn)為字符串,注意不可以使用額外的空間。
你也可以嘗試反轉(zhuǎn)一個整數(shù),但是要考慮整數(shù)溢出的問題。
3、正解
class Huiwenshu {public static void main(String[] args) {boolean b1 = isPalindrome(5);boolean b2 = isPalindrome(-10);boolean b3 = isPalindrome(200);boolean b4 = isPalindrome(12321);System.out.println(b4);}static boolean isPalindrome(int x) {int res = 0;//定義反向數(shù)if(x<0 || x!=0 && x%10==0){//當(dāng)x小于0,或者x是10的整數(shù)倍時,不是回文數(shù)return false;}while(x > res){//只要正向剩下的數(shù)大于反向得到的數(shù),就繼續(xù)判斷res = res *10 + x%10;//反向得到的數(shù)x = x/10;//正向剩下的數(shù)}return(res == x)||(res/10 == x);//偶數(shù)位時兩者相等,奇數(shù)位時res/10==x} }4、第一次錯誤思路:
2332,把最低位移到最高位,移4次會得到2332,判斷改變前后是否相等。但是!所有數(shù)都可以滿足。。。
不過其中也學(xué)習(xí)到一些細(xì)節(jié):
(1)Java中求冪:double d = Math.pow(double a,double b);
(2)強制轉(zhuǎn)換:int? i =(int)d;
//方法是錯誤的 class Palindrome {public static void main(String[] args) {boolean b = isPalindrome(123);System.out.println(b);}public static boolean isPalindrome(int x) {if(x<0){return false;}int n = 1;//獲取位數(shù)int wei = x;while(wei>10){wei = wei/10;//改變了x的值n++; }System.out.println("x="+x);int temp = x;for(int i=1;i<n;i++){temp = temp% 10 * (int)Math.pow(10,n-1) + temp/10;System.out.println("temp="+temp);}//if(x==1)// System.out.println(temp);if(temp == x)return true;elsereturn false;} }5、第二次錯誤解法(AC,但是題目說到了不要用整數(shù)轉(zhuǎn)字符串)
用棧實現(xiàn)整數(shù)的反轉(zhuǎn),且不會溢出,但是不符合要求。
import java.util.Stack; //利用棧實現(xiàn)回文數(shù)的判斷 class Palindrome2 {public static void main(String[] args) {int x = 12121;boolean b = isPalindrome(x);System.out.println(b);}public static boolean isPalindrome(int x) {if(x<0){return false;}String in = x+"";//x轉(zhuǎn)為字符串char [] arr = in.toCharArray();//字符串轉(zhuǎn)為字符數(shù)組Stack<String> st = new Stack<String>();//創(chuàng)建一個String類型的棧for(int i = 0;i<arr.length;i++){st.push(arr[i]+"");//字符進(jìn)棧}String out = "";//出棧序列轉(zhuǎn)為字符串while(!st.isEmpty()){out = out + st.pop();//字符出棧并連接}int res = Integer.parseInt(out);//字符串轉(zhuǎn)為整數(shù)return res == x;} }6、利用字符串的反轉(zhuǎn)功能
static boolean isPalindrome2(int x){//利用字符串的反轉(zhuǎn)if(x<0 || x!=0 && x%10==0){return false;//此時不能字符串反轉(zhuǎn)}StringBuffer sb = new StringBuffer(x+"");sb.reverse();//反轉(zhuǎn)System.out.println(sb.toString());System.out.println(x+"");return sb.toString().equals(x+"");}注意最后比較時,不能用==(比較的是地址),要用equals(內(nèi)部重寫了,比較的是值)。
總結(jié)
以上是生活随笔為你收集整理的LeetCode--palindrome-number回文数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA错误:无法从静态上下文中引用非静
- 下一篇: VS2017无法启动