寫(xiě)在前面:
程序=數(shù)據(jù)結(jié)構(gòu)+算法,數(shù)據(jù)結(jié)構(gòu)與算法的重要性就不多說(shuō)了。幾乎沒(méi)有一個(gè)一線互聯(lián)網(wǎng)公司招聘任何類(lèi)別的技術(shù)人員是不考算法的,程序猿們都懂的,現(xiàn)在最權(quán)威流行的刷題平臺(tái)就是 LeetCode。
Question(Easy):
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example:
Input: 121
Output: true
Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Follow up:
Coud you solve it without converting the integer to a string?
Solution
以下代碼皆是本人用 C++寫(xiě)的,覺(jué)得還不錯(cuò)的話別忘了點(diǎn)個(gè)贊哦。各位同學(xué)們?nèi)绻衅渌咝Ы忸}思路還請(qǐng)不吝賜教,多多學(xué)習(xí)。
A1、反轉(zhuǎn)數(shù)字判斷回文數(shù)
回文數(shù):1.負(fù)數(shù)排除 ;2.先反轉(zhuǎn)數(shù)字,再比較是否與原數(shù)相等
參考LeetCode刷算法題 - 7. Reverse Integer
算法時(shí)間復(fù)雜度 O(logn),Runtime: 136 ms,代碼如下
static int x=[](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();
class Solution {
public:
bool isPalindrome(int x) {
if (x<0) {
return false;
}
long res = 0;
long temp = x;
while (temp) {
res = res*10 + temp%10;
temp /= 10;
}
return res == x;
}
};