本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41598031

Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space.

思路:

(1)题意中限制不可申请额外的空间,所以不能将整数转为字符串进行处理。如果没有限制,则可将其转为字符串,前后分别进

行遍历即可进行判断。

(2)需要注意的是,需要对负数进行判断,负数显然不是回文串。

(3)首先,需要设置标志位来记录该整数最高位所对最小整数,例如1221,包含所有位数最小整数位1000,则标志位为1000。

(4)其次,判断该整数"/1000"(即对标志位取整)的结果是否和其“%10”(取余数)得到的结果相同,即判断最高位和最低位所

对的数字是否相同,不相同则返回false;

(5)最后,如果相同,首先去除该整数位“最高位”,即该整数减去其 “最高位 * 标志位”(得到结果为221),然后去除其“最低位”,

将所得结果再“/10”(取整,得到结果为22),这样就去掉了最高位和最低位。由于去掉了“两位”,所以标志位对应也需要去

掉“两位”,即标志位需要再“/100”(10的2次方)。循环上述操作,直到该整数小于0,最后返回对应结果。

算法代码实现如下所示:(希望对你有所帮助,谢谢)

public static boolean isPalindrome(int x) {
	if (x < 0)
		return false;
	int flag = 1;
	//确定为10的多少幂次方
	while (x / flag > 9) {
		flag = flag * 10;
	}

	while (x > 0) {
		//判断最高位和最低位是否相同
		if (x/flag != x % 10) {
			return false;
		} else {
			//得到最高位
			int left = x / flag;
			//去掉最高位
			x = x - left * flag;
			//去掉最低位
			x = x / 10;
			//位数少了两位,标志位减少10的2次方100
			flag = flag / 100;
		}
	}
	return true;
}

Leetcode_9_Palindrome Number的更多相关文章

  1. JavaScript Math和Number对象

    目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...

  2. Harmonic Number(调和级数+欧拉常数)

    题意:求f(n)=1/1+1/2+1/3+1/4-1/n   (1 ≤ n ≤ 108).,精确到10-8    (原题在文末) 知识点:      调和级数(即f(n))至今没有一个完全正确的公式, ...

  3. Java 特定规则排序-LeetCode 179 Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  4. Eclipse "Unable to install breakpoint due to missing line number attributes..."

    Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...

  5. 移除HTML5 input在type="number"时的上下小箭头

    /*移除HTML5 input在type="number"时的上下小箭头*/ input::-webkit-outer-spin-button, input::-webkit-in ...

  6. iOS---The maximum number of apps for free development profiles has been reached.

    真机调试免费App ID出现的问题The maximum number of apps for free development profiles has been reached.免费应用程序调试最 ...

  7. 有理数的稠密性(The rational points are dense on the number axis.)

    每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.

  8. [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  9. [LeetCode] Number of Boomerangs 回旋镖的数量

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

随机推荐

  1. virtualenvwrapper 的安装和使用

    virtualenvwrapper是用来管理virtualenv的扩展包,用着很方便. 1. 安装: #安装virtualenv (sudo) pip install virtualenv #安装vi ...

  2. OpenCv error :unresolved external symbol(链接库没有加上)

    Error 如下:Linking...: error LNK2001: unresolved external symbol _cvDestroyWindow: error LNK2001: unre ...

  3. Go 语言条件语句

    条件语句需要开发者通过指定一个或多个条件,并通过测试条件是否为 true 来决定是否执行指定语句,并在条件为 false 的情况在执行另外的语句. 下图展示了程序语言中条件语句的结构: Go 语言提供 ...

  4. MySQL where 子句

    MySQL where 子句 我们知道从MySQL表中使用SQL SELECT 语句来读取数据. 如需有条件地从表中选取数据,可将 WHERE 子句添加到 SELECT 语句中. 语法 以下是SQL ...

  5. 分别用face++和百度获取人脸属性(python单机版)

    称之为单机版,主要是相对于调用摄像头实时识别而言.本篇主要py2下利用face++和百度接口获取本地图片中的人脸属性,并按照一定格式保存数据. face++版 face++是刚注册的,只能用一个试用的 ...

  6. Swift基础之如何使用iOS 9的Core Spotlight框架

    本文由CocoaChina译者KingOfOnePiece(博客)翻译 作者:GABRIEL THEODOROPOULOS?校对:hyhSuper 原文:How To Use Core Spotlig ...

  7. Linux文件编辑命令详细整理

    刚接触Linux,前几天申请了个免费体验的阿里云服务器,选择的是Ubuntu系统,配置jdk环境变量的时候需要编辑文件. vi命令编辑文件,百度了一下,很多回答不是很全面,因此编辑文件话了一些时间. ...

  8. linux 防火墙操作

    root/12345 (只能用ROOT操作)iptables -I INPUT -s x.x.x.x -p tcp --dport 8091 -j ACCEPT   #允许x.x.x.x访问本机的80 ...

  9. SuperVideo,一款直播,点播,投屏并有的app

    应用名称:SuperVideo应用简介: 1.聚合海量视频,视频源来源于搜狐,乐视,优酷, 腾讯等主流视频网站的丰富视频内容,最新院线大片,热播剧随时看 2.基于百度云解码,享受云解码支持RMVB,M ...

  10. ROS_Kinetic_28 turtlebot gazebo demo例子

    ROS_Kinetic_28 turtlebot gazebo demo例子 官方教程:http://wiki.ros.org/turtlebot_gazebo/Tutorials/indigo/Ma ...