国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 曲靖市| 汉川市| 内黄县| 左权县| 东丰县| 防城港市| 兰坪| 泊头市| 鄢陵县| 共和县| 东阳市| 晋江市| 龙江县| 贵德县| 昌黎县| 乌鲁木齐县| 宽甸| 安康市| 沂源县| 五大连池市| 游戏| 兴宁市| 富阳市| 广元市| 凤城市| 鹤壁市| 合江县| 伊吾县| 华蓥市| 延长县| 文安县| 兰西县| 洛阳市| 济阳县| 广安市| 开远市| 杨浦区| 什邡市| 赤城县| 土默特左旗| 双桥区|