[LeetCode] 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 integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
There is a more generic way of solving this problem.
解法:
这道验证回文数字的题不能使用额外空间,意味着不能把整数变成字符,然后来验证回文字符串。而是直接对整数进行操作,我们可以利用取整和取余来获得我们想要的数字,比如 1221 这个数字,如果 计算 1221 / 1000, 则可得首位1, 如果 1221 % 10, 则可得到末尾1,进行比较,然后把中间的22取出继续比较。代码如下:
public class Solution {
public boolean isPalindrome(int x) {
if (x < 0) return false; int div = 1;
while (x / div >= 10) div *= 10; // 不能写成while(div*10<=x),因为乘法可能溢出
while (x > 0) {
int left = x / div;
int right = x % 10;
if (left != right) return false;
x = (x % div) / 10;
div /= 100;
}
return true;
}
}
[LeetCode] 9. Palindrome Number ☆的更多相关文章
- Leetcode练习题 Palindrome Number
9. Palindrome Number Question: Determine whether an integer is a palindrome. An integer is a palindr ...
- Leetcode 9. Palindrome Number(水)
9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome w ...
- leetcode 9 Palindrome Number 回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- LeetCode 9. Palindrome Number (回文数字)
Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, ...
- Leetcode 9. Palindrome Number(判断回文数字)
Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...
- [LeetCode][Python]Palindrome Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/palindr ...
- 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]
题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- [LeetCode] 9.Palindrome Number - Swift
Determine whether an integer is a palindrome. Do this without extra space. 题目意思:判断一个整数是否是回文数 例如:1232 ...
- [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
题目简述: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could n ...
随机推荐
- array.some() 方法兼容ie8
在第 5 版时,some 被添加进 ECMA-262 标准:这样导致某些实现环境可能不支持它.你可以把下面的代码插入到脚本的开头来解决此问题,从而允许在那些没有原生支持它的实现环境中使用它.该算法是 ...
- android课程第一节(TextView控件使用)
TextView控件使用 一.TextView基本使用(创建方式) 1.在程序中创建TextView对象 如下代码: @Override protected void onCreate(Bundle ...
- OSI七层协议模型及OSI参考模型中的数据封装过程
转载自:http://blog.csdn.net/qq_14935437/article/details/71081546 OSI模型,即开放式通信系统互联参考模型(Open System Inter ...
- 20172333 2017-2018-2 《Java程序设计》第10周学习总结
20172333 2017-2018-2 <Java程序设计>第10周学习总结 教材学习内容 第十三章 集合是一个对象,一个保存其他对象的数据库. 集合可以保存不同种类的对象也可以保存同种 ...
- 计算器软件实现系列(六)windowform窗体+SQL+策略模式
一 整体概述 这个计算器软件的功能和以前的功能基本上一样,只不过是数据的保存形式发生了变化,,以前用的是txt文件保存,现在更正用SQL数据库,现在更改了以前的文件保存形式,是三层架构中数据层的更换, ...
- Uncaught Error: Syntax error, unrecognized expression: |117的js错误
117指的是js代码在浏览器运行时的出错的行号 var a="117|117" 前面的错误是由于有特殊符号“|”,用$("txtId"+a).val();去取 ...
- 【Linux】- 不可不知的小技巧
1.Tab键:输入文件或目录名的前几个字符,然后按TAB键,如无相重的,完整的文件名立即自动在命令行出现:如有相重的,再按一下TAB键,系统会列出当前目录下所有以这几个字符开头的名字. 在命令行下,只 ...
- navicat for mysql 10.1.7 注册码
NAVN-LNXG-XHHX-5NOO名:组织:注册码:均为NAVN-LNXG-XHHX-5NOO 下载地址:http://www.cr173.com/soft/38153.html
- Java SE1.6中的Synchronized
1 引言 在多线程并发编程中Synchronized一直是元老级角色,很多人都会称呼它为重量级锁,但是随着Java SE1.6对Synchronized进行了各种优化之后,有些情况下它并不那么重了,本 ...
- asp.netMVC中实现分页方法
方法一:使用传统的sql语句实现分页, public class UserprintDao如下 /// <summary> /// 取得用户申请记录列表(按分页) /// </ ...