【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 ...
随机推荐
- Myeclipse经常弹出Subversion Native Library Not Available
- poj 1964 City Game
Bob is a strategy game programming specialist. In his new city building game the gaming environment ...
- linux 配置环境变量
配置全局 环境变量 查看环境变量 #这个变量赋值操作,只是临时生效,需要写入到文件,永久生效 echo $PATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/u ...
- 【记录】adb连不上手机
1.\用户\.android文件夹下新建adb_usb.ini,内容为手机的VID值,如0x9BB5 2.重启adb adb kill-server adb start-server adb devi ...
- 使用Junit进行自动单元测试
软件工程第二次作业 选择开发工具 使用Eclipse进行java程序编写:安装过程如图: 练习自动单元测试技术 参考资料:[Junit入门使用教程][https://www.cnblogs.com/y ...
- PIE SDK过滤控制
1. 功能简介 栅格数据前置过滤是在渲染之前对内存中的数据根据特定的规则进行处理,然后再进行数据渲染.本示例以定标为例进行示例代码编写. 定标(校准)是将遥感器所得的测量值变换为绝对亮度或变换为与地 ...
- 理解 glibc malloc:主流用户态内存分配器实现原理
https://blog.csdn.net/maokelong95/article/details/51989081 Understanding glibc malloc 修订日志: 2017-03- ...
- (转)Nginx静态服务配置---详解root和alias指令
Nginx静态服务配置---详解root和alias指令 原文:https://www.jianshu.com/p/4be0d5882ec5 静态文件 Nginx以其高性能著称,常用与做前端反向代理服 ...
- (转)创建DB2实例时出错,请大家帮忙解决
创建DB2实例时出错,请大家帮忙解决 原文:http://bbs.chinaunix.net/thread-3601748-1-1.html 运行:$DB2DIR/instance/db2icrt ...
- (转)[Shell]tr命令详解
原文:http://blog.csdn.net/sunnyyoona/article/details/52986893 1. 用途 tr,translate的简写,主要用于压缩重复字符,删除文件中的控 ...