【LeetCode】9. Palindrome Number 回文数
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
公众号:负雪明烛
本文关键词:回文数,回文,题解,Leetcode, 力扣,Python, C++, Java
题目地址:https://leetcode.com/problems/palindrome-number/#/description
题目描述
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?
题目大意
判断一个数字是不是回文数字。
解题方法
判断回文字符串
可以先转化成字符串,然后判断这个字符串是不是回文串。
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
if x < 0: return False
x = str(x)
N = len(x)
for i in range(N / 2):
if x[i] != x[N - 1 - i]:
return False
return True
翻转数字
这个题的意思很简单就是判断一个数字是不是回文数,这个和一个字符串是不是回文的方法不一样。可以通过重新构建一个数字,和之前的数字的顺序是反着的,最后看两个数字是不是相等即可。
注意,常量空间不算附加空间。
public class Solution {
public boolean isPalindrome(int x) {
if (x < 0) return false;
int y = x;
int res = 0;
while(y > 0){
res = res * 10 + y % 10;
y /= 10;
}
return x == res;
}
}
日期
2017 年 5 月 7 日
2018 年 11 月 22 日 —— 感恩节快乐~
【LeetCode】9. Palindrome Number 回文数的更多相关文章
- leetcode 9 Palindrome Number 回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- Leetcode 3——Palindrome Number(回文数)
Problem: Determine whether an integer is a palindrome. Do this without extra space. 简单的回文数,大一肯定有要求写过 ...
- [LeetCode]9. Palindrome Number回文数
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
- 【LeetCode】Palindrome Number(回文数)
这道题是LeetCode里的第9道题. 题目说的: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: ...
- LeetCode Problem 9:Palindrome Number回文数
描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- [LeetCode] Prime Palindrome 质数回文数
Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...
- Palindrome Number 回文数
判断一个数字是否是回文数,尝试不用其他额外空间. 注意: 负数也有可能成为回文数吗? 如果你想让int转为string,注意不用其他空间这个约束. 你也可以翻转一个int,但是有可能会溢出. ...
- 【LeetCode】9 Palindrome Number 回文数判定
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...
- 9. Palindrome Number 回文数的判断
[抄题]: Determine whether an integer is a palindrome. An integer is a palindrome when it reads the sam ...
随机推荐
- R语言与医学统计图形-【28】ggplot2扩展包ggrepel、ggsci、gganimate、ggpubr
ggplot2绘图系统--扩展包ggrepel.ggsci.gganimate.ggpubr等 部分扩展包可在CRAN直接下载,有些需借助devtools包从Github下载. 1. ggrepel包 ...
- Docker将容器制作成镜像并提交到远程仓库
Docker将容器制作成镜像并提交到远程仓库 步骤如下 先在dockerhub上创建一个自己的用户https://hub.docker.com/.或者在阿里云也可以. 2. 然后先创建一个空的镜像名. ...
- k8s集群中部署Rook-Ceph高可用集群
先决条件 为确保您有一个准备就绪的 Kubernetes 集群Rook,您可以按照这些说明进行操作. 为了配置 Ceph 存储集群,至少需要以下本地存储选项之一: 原始设备(无分区或格式化文件系统) ...
- 【Python机器学习实战】聚类算法(1)——K-Means聚类
实战部分主要针对某一具体算法对其原理进行较为详细的介绍,然后进行简单地实现(可能对算法性能考虑欠缺),这一部分主要介绍一些常见的一些聚类算法. K-means聚类算法 0.聚类算法算法简介 聚类算法算 ...
- 【2021赣网杯web(一)】gwb-web-easypop
源码分析 <?php error_reporting(0); highlight_file(__FILE__); $pwd=getcwd(); class func { public $mod1 ...
- android studio 生成aar和引用aar
以android studio 2.0正式版为例 1.aar包是Android studio下打包android工程中src.res.lib后生成的aar文件,aar包导入其他android stud ...
- mybatis-plus解析
mybatis-plus当用lambda时bean属性不要以is/get/set开头,解析根据字段而不是get/set方法映射
- 【分布式】Zookeeper客户端基本的使用
与mysql.redis等软件一样,zookeeper的软件包中也提供了客户端程序用于对服务器上的数据进行操作.本节我们就来学习zookeeper客户端的使用方法.不过在详细讲解zk客户端的使用方法之 ...
- Output of C++ Program | Set 17
Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...
- OC中的结构体
一.结构体 结构体只能在定义的时候进行初始化 给结构体属性赋值 + 强制转换: 系统并不清楚是数组还是结构体,需要在值前面加上(结构体名称) +定义一个新的结构体,进行直接赋值 + ...