LeetCode刷算法題 - 9. Palindrome Number

寫(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。

LeetCode原題鏈接

string - C++ Reference

C++中int與string的相互轉(zhuǎn)換

C++ Map常見(jiàn)用法說(shuō)明

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;
    }
};

引用相關(guān)

LeetCode 官網(wǎng)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,841評(píng)論 0 10
  • 最近正在找實(shí)習(xí),發(fā)現(xiàn)自己的算法實(shí)在是不能再渣渣,在網(wǎng)上查了一下,發(fā)現(xiàn)大家都在刷leetcode的題,于是乎本渣渣也...
    caoxian閱讀 1,014評(píng)論 0 2
  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問(wèn)題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,899評(píng)論 0 33
  • 1. 阿宇,在事業(yè)單位上班,早九晚五。楊楊,護(hù)士,三班倒。兩人是大學(xué)校友還是老鄉(xiāng),都是獨(dú)自飄在遠(yuǎn)方求學(xué)的孩子。各種...
    aries諾諾閱讀 868評(píng)論 1 6
  • 【day 52 菇醬】 《大家都有病》 作者是一個(gè)非常富有想法的人,用漫畫(huà)的形式介紹了現(xiàn)當(dāng)代社會(huì)存在人們心中的各...
    阿阿阿囡醬閱讀 173評(píng)論 0 0

友情鏈接更多精彩內(nèi)容