乘风破浪:LeetCode真题_009_Palindrome Number
乘风破浪:LeetCode真题_009_Palindrome Number
一、前言
如何判断一个整型数字是回文呢,我们可能会转换成String来做,但是还有更简单的方法。
二、Palindrome Number
2.1 问题理解


2.2 问题分析和解答
通过题意我们知道不使用String来作答,因此我们想到可不可以采用取整和取余的运算来解决呢,如果将整数倒过来,余数乘以10加上更高位的数字,这样得到的数字如果和原来的数字相等,并且不是负数,那么就是回文的。这是我们的想法,但是是否有更好的方法呢,那就是如果是回文的,那么一半倒过来和另一半也是相等的,并且考虑到奇偶问题,这样就能减少一半的运算了。
首先看官网上的解答:
public class Solution {
public bool IsPalindrome(int x) {
// Special cases:
// As discussed above, when x < 0, x is not a palindrome.
// Also if the last digit of the number is 0, in order to be a palindrome,
// the first digit of the number also needs to be 0.
// Only 0 satisfy this property.
if(x < 0 || (x % 10 == 0 && x != 0)) {
return false;
}
int revertedNumber = 0;
while(x > revertedNumber) {
revertedNumber = revertedNumber * 10 + x % 10;
x /= 10;
}
// When the length is an odd number, we can get rid of the middle digit by revertedNumber/10
// For example when the input is 12321, at the end of the while loop we get x = 12, revertedNumber = 123,
// since the middle digit doesn't matter in palidrome(it will always equal to itself), we can simply get rid of it.
return x == revertedNumber || x == revertedNumber/10;
}
}
下面是我们自己的解答:
public class Solution {
/**
* 原题
* Determine whether an integer is a palindrome. Do this without extra space.
*
* 题目大意
* 判断一个数字是否是回文数字,不要使用String转换。
*
* 解题思路
* 首先,负数不是回文数字,其次对数字进行逆转,123变成321这样,如果变换后的数字相等说明是回文数字。
*/
public boolean isPalindrome(int x) {
// 负数不是回文数字
if (x < 0) {
return false;
}
// 数字逆转后的值,为了不使用溢出采用long
long reverse = 0;
int tmp = x;
// 求逆转后的值
while (tmp != 0) {
reverse = reverse * 10 + tmp % 10;
tmp /= 10;
}
// 判断是否是回文数字
return x == reverse;
}
}
因为这样做浪费了一半的计算资源,所以效率比较低。

三、总结
通过一件事情,我们可以发现自己想到的并不是最优的,这个时候就要仔细想想是不是可以继续优化,优化的时候一定是利用了问题的某种特性,比如回文字符串的对称性。
乘风破浪:LeetCode真题_009_Palindrome Number的更多相关文章
- 乘风破浪:LeetCode真题_017_Letter Combinations of a Phone Number
乘风破浪:LeetCode真题_017_Letter Combinations of a Phone Number 一.前言 如何让两个或者多个集合中的随机挑选的元素结合到一起,并且得到所有的可能呢? ...
- 乘风破浪:LeetCode真题_036_Valid Sudoku
乘风破浪:LeetCode真题_036_Valid Sudoku 一.前言 有的时候对于一些基础知识的掌握,对我们是至关重要的,比如ASCII重要字符的表示,比如一些基本类型的长度. 二.Valid ...
- 乘风破浪:LeetCode真题_041_First Missing Positive
乘风破浪:LeetCode真题_041_First Missing Positive 一.前言 这次的题目之所以说是难,其实还是在于对于某些空间和时间的限制. 二.First Missing Posi ...
- 乘风破浪:LeetCode真题_040_Combination Sum II
乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...
- 乘风破浪:LeetCode真题_039_Combination Sum
乘风破浪:LeetCode真题_039_Combination Sum 一.前言 这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...
- 乘风破浪:LeetCode真题_038_Count and Say
乘风破浪:LeetCode真题_038_Count and Say 一.前言 这一道题目,很类似于小学的问题,但是如果硬是要将输入和结果产生数值上的联系就会产生混乱了,因此我们要打破思维定势. ...
- 乘风破浪:LeetCode真题_037_Sudoku Solver
乘风破浪:LeetCode真题_037_Sudoku Solver 一.前言 这次我们对于上次的模型做一个扩展并求解. 二.Sudoku Solver 2.1 问题 2.2 分析与解决 这道题 ...
- 乘风破浪:LeetCode真题_035_Search Insert Position
乘风破浪:LeetCode真题_035_Search Insert Position 一.前言 这次的问题比较简单,也没有限制时间复杂度,但是要注意一些细节上的问题. 二.Search Insert ...
- 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array
乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...
随机推荐
- Cocos文档案例游戏设计的梳理与分析
导语:这是一篇新手教程,适用于已看完Cocos官方文档,但还对游戏设计.运行流程不熟悉的新人.这篇教程是对文档[快速上手]里那款名叫"摘星星"的坑爹小游戏(文档原话)流程的梳理,以 ...
- multi-threads JavaEE 容器
Thread -- Request What is recommended way for spawning threads from a servlet in Tomcat [duplicate] ...
- linux安装QQ截图
本人(壮壮熊)现用系统是ubuntu 12.04 相信用过linux系统的朋友都知道,linux下的截图软件是在不咋的.虽然系统本身有带截图工具,但是却苦于没有办法在截下来的图片上作画圈.写文字说明等 ...
- VM虚拟机安装后的网络设置
-------------------------------------------- VM的win7系统 网络设置: 1,启动前,VM中的 网络适配器:NAT 2,关闭主机和虚拟机的所有防火墙先. ...
- js原型及原型链解析
js原型.原型链 这几天闲了看了下js的原型,以下内容为个人理解,如有错误,尽请指正. 首先,明确一点:js中的对象分为普通对象和函数对象,一般我们自定义的可以被new的函数称作函数对象,另外js内置 ...
- C# 批量 json 读取
// 方法一 //string test = "[{ 'CreateUser': 'CN=koujirou nishikawaOMHBK','CreateUserJ': '西川 公二郎'}, ...
- array(1) { [0]=> int(5) }和array(1) { [0]=> string(1) "5" }
php array数组: $arrayValue = array(5); $arrayValue = array('5'); 的不同之处 一个是整型一个是字符串型 array(1) { [0]=> ...
- jquery的事件绑定on()动态绑定
常用 这里有个文章列表, 通过on() 点击标题获取标题内容 <div class="article"> <div class="title" ...
- 撩课-Web大前端每天5道面试题-Day8
1. 说说你对作用域链的理解? 作用域链的作用是保证执行环境里 有权访问的变量和函数是有序的, 作用域链的变量只能向上访问, 变量访问到window对象即被终止, 作用域链向下访问变量是不被允许的; ...
- 理解webpack4.splitChunks之chunks
上回说到按照默认的splitChunks配置,入口里面的第三方依赖没有打包出来,这个是因为chunks属性的原因,下面我们就介绍chunks属性的意义和用法. chunks的含义是拆分模块的范围,它有 ...