乘风破浪: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的更多相关文章

  1. 乘风破浪:LeetCode真题_017_Letter Combinations of a Phone Number

    乘风破浪:LeetCode真题_017_Letter Combinations of a Phone Number 一.前言 如何让两个或者多个集合中的随机挑选的元素结合到一起,并且得到所有的可能呢? ...

  2. 乘风破浪:LeetCode真题_036_Valid Sudoku

    乘风破浪:LeetCode真题_036_Valid Sudoku 一.前言 有的时候对于一些基础知识的掌握,对我们是至关重要的,比如ASCII重要字符的表示,比如一些基本类型的长度. 二.Valid ...

  3. 乘风破浪:LeetCode真题_041_First Missing Positive

    乘风破浪:LeetCode真题_041_First Missing Positive 一.前言 这次的题目之所以说是难,其实还是在于对于某些空间和时间的限制. 二.First Missing Posi ...

  4. 乘风破浪:LeetCode真题_040_Combination Sum II

    乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...

  5. 乘风破浪:LeetCode真题_039_Combination Sum

    乘风破浪:LeetCode真题_039_Combination Sum 一.前言     这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...

  6. 乘风破浪:LeetCode真题_038_Count and Say

    乘风破浪:LeetCode真题_038_Count and Say 一.前言     这一道题目,很类似于小学的问题,但是如果硬是要将输入和结果产生数值上的联系就会产生混乱了,因此我们要打破思维定势. ...

  7. 乘风破浪:LeetCode真题_037_Sudoku Solver

    乘风破浪:LeetCode真题_037_Sudoku Solver 一.前言 这次我们对于上次的模型做一个扩展并求解. 二.Sudoku Solver 2.1 问题 2.2 分析与解决     这道题 ...

  8. 乘风破浪:LeetCode真题_035_Search Insert Position

    乘风破浪:LeetCode真题_035_Search Insert Position 一.前言 这次的问题比较简单,也没有限制时间复杂度,但是要注意一些细节上的问题. 二.Search Insert ...

  9. 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array

    乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...

随机推荐

  1. 设置spacevim字体显示乱码问题

    https://github.com/powerline/fonts clone powerline fonts 仓库 执行项目中的 install.sh 安装字体 修改终端配置中使用的字体为 xxx ...

  2. 很乱,临时保存,自定义v-model

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  3. Maven 打包的时候报 Failed to execute goal org.codehaus.mojo:native2ascii-maven-plugin

    错误信息: [ERROR] Failed to execute goal org.codehaus.mojo:native2ascii-maven-plugin:1.0-alpha-1:native2 ...

  4. 端口扫描工具nmap

    nmap 使用介绍 nmap是目前为止最广为使用的国外端口扫描工具之一.我们可以从[url]http://www.insecure.org/[/url]进行下载,可以很容易的安装到Windows和un ...

  5. 【LeetCode题解】349_两个数组的交集

    目录 [LeetCode题解]349_两个数组的交集 描述 方法一:两个哈希表 Java 实现 类似的 Java 实现 Python 实现 类似的 Python 实现 方法二:双指针 Java 实现 ...

  6. 使用FileSystemWatcher监视指定目录

    使用 FileSystemWatcher 监视指定目录中的更改.可监视指定目录中的文件或子目录的更改. 以下是一个简单的实例,用来监控指定目录下文件的新增.删除.重命名等情况(文件内容更改会触发多次, ...

  7. webpack工具、Vue、react模块化

    一.为什么要有webpack print('hello,world') fsdl fdsf title2 title3 引用 斜体字 加粗 有序列表1 有序列表2 无序列表1 无序列表2 行内code ...

  8. Skype坑爹报错:“旧版本无法删除,请联络您的技术支持小组 ”的解决办法

    真是恶心的让人想吐的报错.现在终于解决了,跟大家分享一下方法. 先给问题截个图,如下 首先当我去搜解决办法之前,我已经在[控制面板]的[卸载程序]里把Skype删除了,真是让我后悔不已的操作啊!!因为 ...

  9. 手把手教你写一个RPC

    1.1 RPC 是什么 定义:RPC(Remote Procedure Call Protocol)--远程过程调用协议 ,RPC协议假定某些传输协议的存在,如TCP或UDP,为通信程序之间携带信息数 ...

  10. k:特殊的线性表—队列

    队列的概念:  队列是另一种特殊的线性表,它的特殊性体现在其只允许在线性表的一端插入数据元素,在线性表的另一端删除数据元素(一般会采用在线性表的表尾那端(没被head指针所指的那端)插入数据元素,在线 ...