leetcode 回文数
判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
示例 1:
输入: 121
输出: true
示例 2:
输入: -121
输出: false
解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
示例 3:
输入: 10
输出: false
解释: 从右向左读, 为 01 。因此它不是一个回文数。
进阶:
你能不将整数转为字符串来解决这个问题吗?
/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function (x) {
if (x < 0) {
return false;
}
let num = x;
let len = 0;
//求位数
while (num) {
num = ~~(num / 10);
len++;
}
//判断对应位是否相等
for (let i = ~~(len / 2); i !== 0; i--) {
let n = (~~(x / (10 ** (len - i)))) % 10;
let m = ~~((~~(x % (10 ** (i)))) / (10 ** (i - 1)));
if (n !== m) {
return false;
}
}
return true;
};
对应位的数字就是一手死凑凑出来的
leetcode 回文数的更多相关文章
- leetcode 9 Palindrome Number 回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- [LeetCode] Prime Palindrome 质数回文数
Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...
- LeetCode 5回文数
判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...
- leetcode isPalindrome (回文数判断)
回文很简单,就是正着读和反着读一样,要判断一个数是否为回文数只需要判断正反两个是不是相等即可. 再往深了想一下,只需要判断从中间分开的两个数一个正读,一个反读相等即可. 代码: class Solut ...
- 【LeetCode】Palindrome Number(回文数)
这道题是LeetCode里的第9道题. 题目说的: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: ...
- 【LeetCode】9、Palindrome Number(回文数)
题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...
- Leetcode(9)回文数
Leetcode(9)回文数 [题目表述]: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 第一次:直接全部转 执行用时:148 ms: 内存消耗:13.4 ...
- [LeetCode] 906. Super Palindromes 超级回文数
Let's say a positive integer is a superpalindrome if it is a palindrome, and it is also the square o ...
- 【LeetCode】 #9:回文数 C语言
目录 题目 思路 初步想法 进一步想法 最后想法 总结 最近打算练习写代码的能力,所以从简单题开始做. 大部分还是用C语言来解决. @(解法) 题目 判断一个整数是否是回文数.回文数是指正序(从左向右 ...
随机推荐
- sorl 6.6.0 定时更新索引
solr 定时更新索引 – solr 6.6.0 – dataimport.scheduler 这里先重点说下,定时更新引用的org.apache.solr.handler.dataimport.sc ...
- 将Hive统计分析结果导入到MySQL数据库表中(一)——Sqoop导入方式
https://blog.csdn.net/niityzu/article/details/45190787 交通流的数据分析,需求是对于海量的城市交通数据,需要使用MapReduce清洗后导入到HB ...
- 设置GO环境变量
linux的设置方法:有4个环境变量需要设置:GOROOT.GOPATH.GOBIN以及PATH.需要设置到某一个profile文件中(~/.bash_profile(单一用户)或/etc/profi ...
- Direcshow之视频捕捉<转>
关于视频捕捉(About Video Capture in Dshow) 1. 视频捕捉Graph的构建 一个能够捕捉音频或者视频的graph图都称之为捕捉graph图.捕捉graph图比一般的文件回 ...
- hive 建表导入数据
1. hive> create table wyp > (id int, name string, > age int, tel string) > ROW FORMAT DE ...
- LevelDB 写入与删除记录
[LevelDB 写入与删除记录] levelDb的记录更新操作,即插入一条KV记录或者删除一条KV记录.levelDb的更新操作速度是非常快的,源于其内部机制决定了这种更新操作的简单性. 图6.1是 ...
- Python divmod() 函数
Python divmod() 函数 Python 内置函数 python divmod() 函数把除数和余数运算结果结合起来,返回一个包含商和余数的元组(a // b, a % b). 在 pyt ...
- Git学习笔记——从一台电脑上传文件到Github上
目标:从一台电脑上传文件到Github上 前提: 1.这里假定已在Github上创建了仓库,建立了仓库 2.已在这台电脑上安装了Git客户端 实验环境: 1.Windows 10 64位,已安装了Gi ...
- 通过Jenkins自动构建dubbo服务时的问题汇总
最近接触新的dubbo项目,项目初始时,测试环境的提供者服务发布较频繁,奈何公司又没有自动发布工具,遂自己在测试环境中搭建了Jenkins用于dubbo服务的发布.由于第一次使用,过程中也遇到了一些问 ...
- 洛谷 P2899 [USACO08JAN]手机网络Cell Phone Network(树形动规)
题目描述 Farmer John has decided to give each of his cows a cell phone in hopes to encourage their socia ...