代码先锋网 代码片段及技术文章聚合

Palindrome Number

技术标签: leetcode  算法

一 问题描述

Palindrome Number

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:

Input: 121
Output: true

Example 2:

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.

Example 3:

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?
翻译:
判定一个整数是不是回文串。一个是否是回文串取决于正着读或反着读是否一样。

二 解法

1. 第一解法(个人,转换字符串)

分析:
将数字转换成字符串数组之后,从两端往中间比较,很容易得出结论。
代码:

var isPalindrome = function(x) {
    
    let str = x.toString().split("");
    
    let len = str.length - 1;
    let mid = str.length / 2;
    
    for (let i = 0; i <= mid; i++) {
        if (str[i] !== str[len - i])
            return false
    }
    return true;
};

2. 第二解法(个人,非转字符串)

分析:
将数字通过数学计算生成一个相反排列的数字,与原数字比较即可。这时候,需要先判断是否是负数。并且,js中整数除法可以除出来小数,因此需要使用Math.floor()方法向下取整。
代码:

var isPalindrome = function(x) {
    
    let num = x;
    let arc = 0;
    
    if (x < 0)
        return false;
    
    while (num >= 10) {
        arc *= 10;
        arc += num % 10;
        num = Math.floor(num /10);
    }
    
    arc = arc * 10 + num;
    return arc === x;
    
};

3. 第三解法(个人,最优,非转字符串)

分析:
上述的通过数学计算,从头到尾完全反转,但实际上,是不需要这样的。回文串判断只需要前后半段比较。因此,通过新设置一个arc变量存储后半段数字,然后比较x是否大于arc,只要大于arc,就和前面的逻辑一样,一直生成后半段反转数字。
之后,判断有两个标准:

  1. 是否相等,如果相等, 必定是回文串
  2. 前半段此时已经小于后半段,说明,如果前半段乘10加上后半段的个位数如果和后半段相等,也可以判定为回文串。

注:这种判断方法不能正确处理0的问题。例如x = 10,x = 100。因为0 * 10 = 0。会干扰判断。因此在一开始需要排除掉这种情况。这种情况有两个特征,首先是x % 10 === 0,其次是x !== 0。

代码:

    var isPalindrome = function(x) {
        
        let num = x;
        let arc = 0;
        
        if (x < 0 || x % 10 === 0 && x !== 0)
            return false;
        
        while (num > arc) {
            arc *= 10;
            arc += num % 10;
            num = Math.floor(num /10);
        }
    
        if(arc === num || num * 10 + arc % 10 === arc)
            return true;
    
        return false;  
    };

结果:

11509 / 11509 test cases passed.
Status: Accepted
Runtime: 164 ms
Memory Usage: 45.3 MB
Runtime:faster than 98%

By DoubleJan
2019.7.7

版权声明:本文为DoubleJan原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/DoubleJan/article/details/94989220

智能推荐

Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space.    ...

Palindrome Number

题目:Determine whether an integer is a palindrome. Do this without extra space. 思路: 一个技巧就是先计算出一共几位数,对应后面几个零; 接下来就是循环,首先除以div,在%10,就是最左边最右边的数字,判断两者是否相等。 不等,直接返回。 代码:...

Palindrome number

一开始采用了reverse integer的方法,就是那个while a!=0: n=n*10+a%10; a=a//10这个循环,也是可以提交成功的。 但是,事情应该不是这么简单的。比如我们能不能只loop一半的时间呢。 还真的可以。 在reverse一半之后,就可以检查是不是和另一半一样。也可以避免reverse过程中,超过INT.MAX的问题。PYTHON没有这个问题。 检查是否超过一半,就...

Palindrome Number

目录 Palindrome Number 题目描述 题目解析 代码 Palindrome Number 题目描述 判断一个整数是否是回文数。回文数表示正序和倒序读都是一样的整数。 题目解析 负数一定不是回文数,因为负数的标识符-没有对称的位置。 一一将整数最高位和最低位互换,第二高位和第二低位互换等,然后看下互换后的整数是否和先前的整数相等即可。 代码...

Palindrome Number

问题定义 Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Example 2: Example 3: Follow up: Coud you solve...

猜你喜欢

Palindrome Number

第一版...

Palindrome Number

题目:判断是否是回文数,并且不占用不能使用额外空间 思路:第一想法是使用切片,核心代码一步解决,一分钟解决,太爽了~ 但是仔细看看,不占用额外空间是什么鬼,(em…) 也就是不能通过将数字转为字符串来判断回文,因为使用了额外的空间(即只能使用空间复杂度 O(1) 的方法) 只能反转了~(待修改)...

Palindrome Number

Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. 要看x是否是回数,应该初始化一个int类型z,让x的最高位数=z的最低位数,x的第二高位数=z的第二低位数,并以此类推。 代码块 代码块语法遵循标准markdown代码,例如:...

Palindrome Number

9. Palindrome Number 描述: 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 i...

剑指offer | 数组中出现次数超过一半的数字

题目: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。 思路: ①作一个判断条件。 ②作俩个for循环,用每一个值依次和后面的值作比较。 ③当这俩个值相等时,计数加一。 ④当计数值大于数组长度的一半时,输出这个值就好了。 这里要注意in...