乘风破浪: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. JavaScript数据结构-2.排序算法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Thread -- Request

    Servlet容器应该绝大部分(有可能全部)是Thread per Request,每个请求一个线程.此外有Thread per Connection,应该不是用于Servlet容器.请见 How a ...

  3. linux安装教程以及使用时遇到的问题和解决方法

    以后开发都是要用linux,所以就安装了ubuntu,也是第一次用linux的系统.装的是win7+Ubuntu16.04的双系统. 安装过程如下:我用的是U盘安装,参看http://www.jian ...

  4. winform httplicent调用API

    绑定datagriview Uri uri = new Uri("http://localhost:49423");//地址            HttpClient clien ...

  5. jQuery选择器,外加例子讲解

    jQuery选择器的优势: (1) 代码更简单(2) 支持CSS1到CSS3选择器(3) 完善的处理机制 jQuery选择器的分类 (1) 基本选择器 基本选择器是jQuery中使用最多的选择器,它又 ...

  6. linux的环境变量与文件查找

    1. 环境变量 1.1 变量 shell 中的变量有不同类型,可参与运算,有作用域限定 变量的作用域即变量的有效范围(比如一个函数中.一个源文件中或者全局范围),在该范围内只能有一个同名变量.一旦离开 ...

  7. 全面了解HTTP和HTTPS

    序言 Http和Https属于计算机网络范畴,但作为开发人员,不管是后台开发或是前台开发,都很有必要掌握它们. 在学习Http和Https的过程中,主要是参考了阮一峰老师的博客,讲的很全面,并且通俗易 ...

  8. Spring与MyBatis整合上_Mapper动态代理方式

         将MyBatis与Spring进行整合,主要解决的问题就是将SqlSessionFactory对象交由Spring来管理..所以该整合,只需将SQLSessionFactory的对象生成器S ...

  9. MySQL数据库的备份与恢复命令

    1.数据库导出SQL脚本 启动MySQL服务器 输入:mysqldump -u root -p  数据库名>生成脚本文件路径 输入登录密码,回车键 例如: $ mysql.server star ...

  10. protobuf版本冲突

    在编译chromium代码的过程中发现,官方推荐使用的版本是ubuntu16.04,但是这个版本的ubuntu比较老旧,一些库都比较老了,但是google自己用的部分却是挺新的,protobuf就是一 ...