【Leetcode】【Easy】Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.
判断一个整数是不是回文整数(例12321)。不能使用多余的空间。
Some hints:
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
There is a more generic way of solving this problem.
一些提示:
复数不是回文;如果你希望将整数转为字符串,注意不要使用多余的空间;
如果你希望反转整数,请注意“反转”可能导致溢出。你如何解决这个问题?
思路1:
可将此数按位从低到高依次取出,再从高到低反向排列,得到的新数字和原数字比较,一致则是回文数,否则不是。
至于提示中提到反转整数可能会出现int越界的问题,在此题中不会出现。因为如果一个int是回文整数,那么它和自身的反转相等,因此反转后不会越界。如果一个int不是回文数,反转后出现越界的情况,也不会影响结果的判定。
class Solution {
public:
bool isPalindrome(int x) {
int res = ;
int tmp_x = x;
if (x<)
return false;
while (tmp_x != ) {
res = res * + tmp_x % ;
tmp_x = tmp_x / ;
}
if (res == x) {
return true;
} else {
return false;
}
}
};
思路2:
可以将整数中,需要进行比较的首尾数字,不断取出并比较。
class Solution {
public:
bool isPalindrome(int x) {
if (x < )
return false;
int sig = ;
while (x / sig >= ) {
sig *= ;
}
while (x!=) {
if (x / sig == x % ) {
x = x % sig / ;
sig /= ;
} else {
return false;
}
}
return true;
}
};
上面代码在函数中对x进行了修改,为了避免x被修改:
class Solution {
public:
bool isPalindrome(int x) {
if (x < )
return false;
int high = , low = ;
while (x / high >= ) {
high *= ;
}
while (high > low) {
if (x / high % == x / low % ) {
high /= ;
low *= ;
} else {
return false;
}
}
return true;
}
};
附录:
算法中“不使用多余空间”的含义
【Leetcode】【Easy】Palindrome Number的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List
9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- C# 写 LeetCode easy #9 Palindrome Number
9.Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number
[Q7] 把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...
- 【LeetCode每天一题】Palindrome Number( 回文数字)
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
- 【leetcode刷题笔记】Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
随机推荐
- C. Enlarge GCD Codeforces Round #511 (Div. 2)【数学】
题目: Mr. F has nn positive integers, a1,a2,…,an. He thinks the greatest common divisor of these integ ...
- UESTC - 1610 递推方程+矩阵快速幂
感觉像是HDU Keyboard的加强版,先推出3张牌时的所有组合,然后递推出n张牌 看到n=1e18时吓尿了 最后24那里还是推错了.. (5行1列 dp[1][n],dp[2][n],dp[3][ ...
- HDU1286
欧拉函数测试题 期末考试完了做下水题 更好的方法是phi[i]==i时就直接筛选,不改了 #include<bits/stdc++.h> using namespace std; cons ...
- Linux设备驱动开发基础--内核定时器
1. Linux内核定时器是内核用来控制在未来某个时间点(基于jiffies)调度执行某个函数的一种机制,其实现位于 <Linux/timer.h> 和 kernel/timer.c 文件 ...
- PIE SDK栅格分级渲染
1. 功能简介 栅格数据分级渲染是根据不同的分级规则,对像元值进行等级划分:并通过对每一级设置不同的显示符号和标注信息,从而达到分级显示的效果. 2.功能实现说明 2.1. 实现思路及原理说明 第一 ...
- svn checkout时连接不到服务器,重装也没有弹出用户名和密码输入框的问题
用公司的电脑,是win7 64位的系统,可以checkout出东西.现在用自己的电脑上,系统是win7 64位的,却再也连不上SVN. 1.不提示输入用户名和密码,不管重装多少次都一样. 2.Tort ...
- opensuse12.3 桌面设置备忘录
工作空间外观 窗口装饰 ghost deco2.2 光标主题 ringG 桌面主题 ghost 欢迎屏幕 login-scan-splash-cg 应用程序外观 风格 部件样式 Oxygen 颜色 g ...
- (三)TestNG
1.testNG的部分注解 test测试方法都是执行顺序:并不是从上往下执行的,而是根据方法名ASCII码进行执行的,小的先执行 比如a比b先执行,1比2先执行,不管代码放的顺序是怎么样. impor ...
- 项目搭建系列之三:SpringMVC框架下使用Ehcache对象、数据缓存
注明:该文章为以前写的文章,这里只更改了标题,无GitHub源码下载. 一.准备工作 如果已经成功搭建SpringMVC环境,那么就可以进入Ehcache的准备工作了.1.下载jar包 Ehca ...
- 在 Linux 上创建第一个 Service Fabric Java 应用程序
先决条件 开始之前,请安装 Service Fabric SDK.Azure CLI,并在 Linux 开发环境中设置开发群集. 如果使用 Mac OS X,则可使用 Vagrant 在虚拟机中设置 ...