成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

[LeetCode] Palindrome Number

劉厚水 / 2742人閱讀

摘要:逐位看官,這兩種解法一看便知,小子便不多費(fèi)唇舌了。字符數(shù)組比較法原數(shù)翻轉(zhuǎn)比較法

Problem

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)
NO

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.

Note

逐位看官,這兩種解法一看便知,小子便不多費(fèi)唇舌了。

Solution 字符數(shù)組比較法
public class Solution {
    public boolean isPalindrome(int x) {
        if (x >= Integer.MAX_VALUE || x < 0) return false;
        int digit = 0, x2 = x;
        while (x2 != 0) {
            x2 /= 10;
            digit++;
        }
        int[] A = new int[digit];
        while (x != 0) {
            A[--digit] = x % 10;
            x /= 10;
        }
        int left = 0, right = A.length-1;
        while (left < right) {
            if (A[left++] != A[right--]) return false;
        }
        return true;
    }
}
原數(shù)翻轉(zhuǎn)比較法
public class Solution {
    public boolean isPalindrome(int x) {
        if (x < 0) return false;
        long rvs = 0;
        int i = 0, org = x;
        while (x != 0) {
            i = x % 10;
            rvs = rvs * 10 + i;
            x /= 10;
        }
        return rvs == org;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/64784.html

相關(guān)文章

  • [LeetCode] 9. Palindrome Number

    Problem Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121Output: trueExample 2: Input: -121Output: falseExplana...

    zhaochunqi 評(píng)論0 收藏0
  • LeetCode Easy】009 Palindrome Number

    摘要:比較簡(jiǎn)單的一道題目,用取模取余的方法求翻轉(zhuǎn)后的整數(shù)和原來(lái)的整數(shù)進(jìn)行比較就行不用完全翻轉(zhuǎn),翻轉(zhuǎn)一半就可這已經(jīng)是最快的方法了 Easy 009 Palindrome Number Description: Determine whether an integer is a palindrome. An integer is a palindrome when it reads the sa...

    big_cat 評(píng)論0 收藏0
  • [Leetcode] Palindrome Number 回文數(shù)

    摘要:首尾比較法復(fù)雜度時(shí)間空間,為所求的長(zhǎng)度思路先求記為的長(zhǎng)度根據(jù)長(zhǎng)度制造掩碼循環(huán)當(dāng)當(dāng)最高位等于最低位還有數(shù)字等待判斷最高位通過(guò)掩碼和整除取得,最低位通過(guò)取余取得判斷過(guò)后更新掩碼,刪掉最高位,刪掉最低位注意求長(zhǎng)度的如何取得一個(gè)的最高位假設(shè)答設(shè)置一 Palindrome Number Determine whether an integer is a palindrome. Do this w...

    lixiang 評(píng)論0 收藏0
  • [Leetcode] Palindrome Number 回文數(shù)

    摘要:反轉(zhuǎn)比較法復(fù)雜度時(shí)間空間思路回文數(shù)有一個(gè)特性,就是它反轉(zhuǎn)后值是一樣的。代碼逐位比較法復(fù)雜度時(shí)間空間思路反轉(zhuǎn)比較有可能會(huì)溢出,但我們遍歷每一位的時(shí)候其實(shí)并不用保存上一位的信息,只要和當(dāng)前對(duì)應(yīng)位相等就行了。首先,負(fù)數(shù)是否算回文。 Palindrome Number Determine whether an integer is a palindrome. Do this witho...

    _Suqin 評(píng)論0 收藏0
  • javascript初探LeetCode之9.Palindrome Number

    摘要:題目分析這是上的第題,難度為,判斷整型數(shù)字是否為回文串,需要注意兩點(diǎn)負(fù)數(shù)都不是回文小于的非負(fù)整數(shù)都是回文這一題與第題類似,也可以有兩種思路數(shù)組法和模十法。所以代碼可以如下改進(jìn)能被整除的非整數(shù)和負(fù)數(shù),返回 題目 Determine whether an integer is a palindrome. Do this without extra space. 分析 這是leetcode上...

    icyfire 評(píng)論0 收藏0
  • leetcode 9 Palindrome Number

    摘要:有一點(diǎn)需要注意的是,負(fù)數(shù)不算作回文數(shù)。而第題當(dāng)時(shí)的方法是,對(duì)整數(shù)取除的余數(shù),即是當(dāng)前整數(shù)的最后一位。那么它翻轉(zhuǎn)后一半的數(shù)字之后,應(yīng)該和前半段的數(shù)字相等,我們將采用這種思路進(jìn)行解題。 題目詳情 Determine whether an integer is a palindrome. Do this without extra space.題目要求我們?cè)诓徽加妙~外空間的前提下,判斷一個(gè)整...

    darkbug 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<