【LeetCode】9、Palindrome Number(回文数)
题目等级:Easy
题目描述:
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?
题意:判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
解题思路:
本题比较简单,一个很直观的做法是我们很熟悉回文字符串的判断,因此可以将整数转为字符串,从而利用回文字符串的判断来判断回文数。当然,题目也提示我们能不能采用别的办法。
解法一:整数反转后比较
整数反转后如果和原来的数相同,那么它就一定是回文数,而如何反转一个整数,在上一题中已经解决,可以参考:【LeetCode】7、Reverse Integer(整数反转)。
解法二:只反转一半
实际上,从解法一也可以看出,么有必要都进行反转,只需要反转整数的后一半,与前一半进行比较即可。
这里主要需要注意的问题有三个:一是如何判断已经反转了一半,判断条件是:剩下的数字小于已经反转完成的数字。二是需要判断奇数位和偶数位不同的情况,若一共有奇数位,那么反转之后比前一半多一位,需要%10,若一共是奇数位,那么说明反转的后一半与前一半位数相同。三是注意特殊情况:负数都不可能是回文数,最后一位如果是0的数,除0之外的数也不可能是回文数。
class Solution {
public boolean isPalindrome(int x) {
//将后一半反转,与前一半比较
//重点:如何知道我们已经反转到了一半,剩下的数字小于已经反转完成的数字
if(x<0 || (x%10==0 && x!=0)) //负数都不可能是回文数,最后一位是0的除0之外都不可能是回文数
return false;
int reverse=0;
while(x>reverse){
reverse=reverse*10+x%10;
x=x/10;
}
if(x==reverse||x==reverse/10)
return true;
return false;
}
}
时间复杂度:有多少位就循环多少次的一半,所以是1/2 * lg(n),即以10为底,时间复杂度O(lgn)
空间复杂度:O(1)
【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】9. Palindrome Number 回文数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...
- [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 ...
随机推荐
- 【bzoj3676】[Apio2014]回文串 —— 回文自动机的学习
写题遇上一棘手的题,[Apio2014]回文串,一眼看过后缀数组+Manacher.然后就码码码...过是过了,然后看一下[Status],怎么慢这么多,不服..然后就搜了一下,发现一种新东西——回文 ...
- HTTP要点概述:五,HTTP的无状态性,持久连接,Cookie
一,HTTP的无状态性: HTTP 是一种不保存状态,无状态(stateless)协议.HTTP 协议自身不对请求和响应之间的通信状态进行保存.也就是说在 HTTP 这个级别,协议对于发送过的请求或响 ...
- ios21--xib例子
故事板控制器: // // XMGViewController.m // 03-综合练习 // // Created by xiaomage on 15/12/28. // Copyright © 2 ...
- luence全文检索(简介)
刚开始做全文检索也是找了很多资料但是网上的都不是很齐全luence是个很不多的工具 Lucene4.0的官网文档:http://lucene.apache.org/core/4_0_0/core/ov ...
- [Codeforces Round511C] Enlarge GCD
[题目链接] https://codeforces.com/contest/1047/problem/C [算法] 首先求出n个数的最大公约数g , 将每个数除以g , 那么 , 问题就转化为在n个数 ...
- Spark 2.2.0 分布式集群环境搭建
集群机器: 1台 装了 ubuntu 14.04的 台式机 1台 装了ubuntu 16.04 的 笔记本 (机器更多时同样适用) 1.需要安装好Hadoop分布式环境 参照:Hadoop分类 ...
- bzoj 1684: [Usaco2005 Oct]Close Encounter【数学(?)】
枚举分母,然后离他最近的分子只有两个,分别判断一下能不能用来更新答案即可 #include<iostream> #include<cstdio> #include<cma ...
- 测试神器Swagger的相关使用
1.Swagger简介 swagger官网地址: https://swagger.io/ swagger官网文档介绍地址: https://swagger.io/about/ swagge是一个易 ...
- svg image 图片无法铺满 circle 的问题解决
引子 使用d3.js绘制了力布图后,需要在circle中绘制图片,方法如下: // 绘制图片 drawPattern(gContainer) { let that = this; let gPatte ...
- c++ rand && srand 函数
RAND: 结构:rand() 注:rand()不需要参数,它会根据种子返回一个从0到最大随机数的任意整数. 作用:生成一个随机数. 头文件:#include <cstdlib> ...