国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

[LeetCode] Palindrome Number 解題報(bào)告

2019-11-08 01:45:22
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

[題目] 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.

[中文翻譯] 確定整數(shù)是否是回文。 不使用額外的空間。 提示: 負(fù)整數(shù)是回文嗎? (如,-1) 如果你正在考慮將整數(shù)轉(zhuǎn)換為字符串,請(qǐng)注意不使用額外空間的限制。 您也可以嘗試反轉(zhuǎn)整數(shù)。 但是,如果已解決了“Reverse Integer”的問(wèn)題,則知道反向整數(shù)可能溢出。 你將如何處理這種情況? 有一個(gè)更通用的方法來(lái)解決這個(gè)問(wèn)題。

[解題思路] 先反轉(zhuǎn)整數(shù),然后比較反轉(zhuǎn)的整數(shù)是否與原整數(shù)相同。對(duì)于溢出的情況,使用long long即可解決。不是很確定,這種方法是否違反了不適用額外空間的限制。

看Discuss的時(shí)候,發(fā)現(xiàn)其實(shí)整數(shù)只需要反轉(zhuǎn)一半即可。

[C++代碼]

class Solution {public: bool isPalindrome(int x) { if (x < 0) return false; long long y = 0; int tmp = x; while (x > 0) { y = y * 10 + x % 10; x = x / 10; } return tmp == y; }};
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 福海县| 余姚市| 柘荣县| 华宁县| 始兴县| 仪征市| 土默特右旗| 普格县| 乌鲁木齐县| 柳州市| 盐津县| 岳西县| 丹东市| 德令哈市| 萨迦县| 黄冈市| 彩票| 石嘴山市| 厦门市| 称多县| 河源市| 广河县| 乌兰浩特市| 洱源县| 股票| 玉门市| 乐亭县| 诸暨市| 襄垣县| 乐清市| 大厂| 祁门县| 达州市| 道真| 白朗县| 岢岚县| 吉安县| 社旗县| 南充市| 苗栗县| 和平县|