日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

9. Palindrome Number

發布時間:2025/7/14 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 9. Palindrome Number 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目:

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.

鏈接:?http://leetcode.com/problems/palindrome-number/

題解:

可以用除法和模運算rebuild一個結果,然后比較和輸入是否相等。這里其實有可能會overflow,就是在 result * 10的時候,但越界的話也會返回一個32位整數,這個整數是 result * 10結果轉換為64位里面的low 32位,一般來說與輸入不同。所以結果返回false,也能過AC,但是不嚴謹。

Time Complexity - O(logx), Space Complexity - O(1)。

public class Solution {public boolean isPalindrome(int x) {if(x == 0)return true;int result = 0, temp = x; while(temp > 0){result = 10 * result + temp % 10;temp /= 10;}return result == x;} }

?

另外一種方法可以避免overflow。先用對數計算出x有幾位,然后通過數學運算比較第一位和最后一位是否相等,接下來去掉最大的一位和最小的一位,再將digitNum - 2,繼續進行計算。

Time Complexity - O(logx), Space Complexity - O(1)。

public class Solution {public boolean isPalindrome(int x) {if(x < 0)return false;int digitNum= (int)(Math.log(x) / Math.log(10)); int left = 0, right = 0;while(x > 0){int powInTens = (int)Math.pow(10, digitNum);left = x / powInTens;right = x % 10;if(left != right)return false;x -= left * powInTens;x /= 10;digitNum -= 2 ;}return true;} }

?

二刷:

Java: - Reverse all digits:

public class Solution {public boolean isPalindrome(int x) {if (x < 0) {return false;}int res = 0, tmp = x;while (tmp > 0) {res = res * 10 + tmp % 10;tmp /= 10;}return res == x;} }

Reverse half digits:

public class Solution {public boolean isPalindrome(int x) {if (x < 0 || (x != 0 && x % 10 == 0)) {return false;}int res = 0;while (x > res) {res = res * 10 + x % 10;x /= 10;}return (res == x) || (x == res / 10);} }

?

Python:

class Solution(object):def isPalindrome(self, x):""":type x: int:rtype: bool"""if x < 0 or (x != 0 and x % 10 == 0):return Falseres = 0while x > res:res = res * 10 + x % 10x /= 10return (x == res) or (x == res / 10)

?

三刷:

Java:

計算全部digits: ?設立一個int n = x, 然后我們計算一下n的按位反轉數res,最后比較 n == res

public class Solution {public boolean isPalindrome(int x) {if(x < 0) {return false;}int n = x;int res = 0;while (x > 0) {res = res * 10 + x % 10;x /= 10;}return res == n;} }

?

計算一半digits:

這里我們要先剪掉特殊情況 x % 10 == 0,這時候假如x不為0的話,肯定不是palindromic number。

之后while循環作比較的條件是 ?x ?> res。 我們只需要計算一半的digits,然后比較是否 x == res, 或者 x == res / 10。

當x是偶數長度的話,我們比較 x == res; 當x時奇數長度的時候,比如 x = 1,這時我們比較的是 x == res / 10。合起來用一個或運算就可以了。

public class Solution {public boolean isPalindrome(int x) {if(x < 0 || (x != 0 && x % 10 == 0)) {return false;}int res = 0;while (x > res) {res = res * 10 + x % 10;x /= 10;}return (x == res || x == res / 10) ;} }

?

?

?

Reference:

https://leetcode.com/discuss/33500/an-easy-lines-code-only-reversing-till-half-and-then-compare

https://leetcode.com/discuss/23563/line-accepted-java-code-without-the-need-handling-overflow

https://leetcode.com/discuss/12693/neat-ac-java-code-o-n-time-complexity?

https://leetcode.com/discuss/65915/python-solution-with-other-variable-introduced-besides-input

?

轉載于:https://www.cnblogs.com/yrbbest/p/4430410.html

總結

以上是生活随笔為你收集整理的9. Palindrome Number的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。