乘风破浪: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 一.前言 这次我们还是要改造二分搜索,但是想法却 ...
 
随机推荐
- js中一次性注册多个事件
			
在js中,如果想一次性给一个控件或者标签初测多个事件的方法: 假如有个<input>标签: <input id=”inputValue” value=”www.baidu.com”/ ...
 - Unix/Linux文件类型及访问权限
			
在Linux系统中,有7种文件类型. 普通文件 (regular file) 目录文件 (directory) 链接文件 (symbolic link) 管道文件 (FIFO) 套接字文件 (sock ...
 - hadoop开发环境
			
hadoop2.6伪分布式环境安装配置以及配置eclipse开发环境 Hadoop安装教程_单机/伪分布式配_Hadoop2.6.0/Ubuntu14.04 使用Eclipse编译运行MapReduc ...
 - [转]Hadoop集群_WordCount运行详解--MapReduce编程模型
			
Hadoop集群_WordCount运行详解--MapReduce编程模型 下面这篇文章写得非常好,有利于初学mapreduce的入门 http://www.nosqldb.cn/1369099810 ...
 - PgAdmin4连接数据库报错:Unable to connect to server:  ERROR: unrecognized configuration parameter "bytea_output"
			
原因: PgAdmin 4不再支持PostgreSQL 9.0 和更早的版本 我的版本是8.2.15 template1=# select version(); version ----------- ...
 - [PY3]——内置数据结构(6)——集合及其常用操作
			
集合及其常用操作Xmind图 集合的定义 # set( ) # {0,1,2} //注意不能用空的大括号来定义集合 # set(可迭代对象) In [1]: s=set();type ...
 - ubuntu下搭建ecshop
			
最近在看ecmobile的开源项目,可以从http://www.ecmobile.cn/agreement.html下载源码或者从github上下载源码https://github.com/G ...
 - jquery选择器【总结】
			
本文总结整理了jquery里和选择器相关的所有方法,通过这篇文章,可以让你学习到在jquery里使用选择器的所有方法. 一:基本选择器: $("#aijquery") 选择id值等 ...
 - rgbdslam 源代码的实现
			
经过一番努力,终于跑通了felix.endres的rgbd slam v2 源码,中间遇到挺多问题.总结如下: (1) 关于SiftGPU问题:ERROR: SiftGPU cannot be com ...
 - golang rpc 简单范例
			
RPC(Remote Procedure Call Protocol)--远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议. 它的工作流程如下图: go ...