LeetCode 9. 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?
public boolean isPalindrome(int x) {
int[] nums=new int[32];
int i=0;
if(x<0)//负数或 最后一位为0(后半句不严谨,单独0是对称的)
return false;
while(x/10>0){
nums[i]=x%10;
i++;
x=x/10;
}
nums[i]=x%10;
int len=0;
for(i=31;i>=0;i--){
if(nums[i]>0){
len=i;//数字位数
break;
}
}
for(i=0;i<=len/2;i++){
if(nums[i]!=nums[len-i])
return false;
}
return true;
}
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 ...
随机推荐
- linux通配符含义
linux通配符含义: . 当前目录**** .. 当前目录的上一级目录**** * 通配符,代表任意0个或多个字符***** ? 通配符,代表重复0个或一个0前面的字符 : ...
- python基础学习13----生成器&迭代器
生成器是属于迭代器,但迭代器不只是生成器 首先是一个简单的生成器 def gener(): print(1) yield 1 print(2) yield 2 print(3) yield 3 g=g ...
- C语言实现输出杨辉三角
1.倒推法实现输出杨辉三角右半部分,代码如下: #include<stdio.h> int main() { ]; printf("请输入行数n:"); scanf(& ...
- [A] 1046 Shortest Distance
The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...
- rhel7.6上安装Oracle 19.2.0.0 RAC
1. 软硬件检查 2. 安装前环境配置 3. 安装GI su - grid -- 解压软件包 unzip -q /ups/soft/V981627-01.zip -d ${ORACLE_HOME} - ...
- Docker技术入门与实战 第二版-学习笔记-5-容器-命令及限制内存与cpu资源
1.启动容器 启动容器有两种方式: 基于镜像新建一个容器并启动 将在终止状态(stopped)的容器重新启动 1)新建并启动——docker run 比如在启动ubuntu:14.04容器,并输出“H ...
- Rsyslog配置文件详解(转)
最近在搭建日志审计服务器,使用了rsyslog,发现这篇文章很有用,收藏一下. 原文链接:http://my.oschina.net/0757/blog/198329 具体内容: 非常详细的rsysl ...
- std::lexicographical_compare函数的使用
按照词典序比较前者是否小于后者. 当序列<first1, last1>按照字典序比较小于后者序列<first2, last2>,则返回true.否则,返回false. 所谓字典 ...
- shiro实战系列(九)之Web
一.Configuration(配置) 将 Shiro 集成到任何 Web 应用程序的最简单的方法是在 web.xml 中配置 ContextListener 和 Filter,理解如何读取 Shir ...
- Java继承访问权限
JAVA 子类重写继承的方法时,不可以降低方法的访问权限,子类继承父类的访问修饰符要比父类的更大,也就是更加开放,假如我父类是protected修饰的,其子类只能是protected或者public, ...