【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 ...
随机推荐
- 在OnActionExecuted 获取请求参数的值(包含类类型)
1.在OnActionExecuting里 获取请求参数的值 比较简单 /// <summary> /// 获取首参数的值 /// </summary> /// <par ...
- 小众软件:Windows 下优化软件推荐—— Dism++ | 强迫症晚期患者福音 - 少数派
Windows 下优化软件推荐—— Dism++ | 强迫症晚期患者福音 - 少数派 https://sspai.com/post/41992 官方网站免费下载
- Ba Gua Zhen
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5544 学习链接:https://www.cnblogs.com/qscqesze/p/4902518. ...
- 利用wireshark和python分析网络
- java中的各种修饰符作用范围
访问修饰符: private 缺省 protected public 作用范围: 访问修饰符\作用范围 所在类 同一包内其他类 其他包内子类 其他包内非子类 private 可以访问 不可以 不可以 ...
- pat1009. Product of Polynomials (25)
1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 虚拟机vmware 上不去 连不上网问题解决
开始---设置--控制面板---管理工具---服务 确保 VMware DHCP Service 和VMware NAT Service 服务已经启动
- zstu 4212 ——String Game ——————【字符串处理】
4212: String Game Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 337 Solved: 41 Description Alice a ...
- CheckBoxList
CheckBoxList 控件基本用法 定义和用法 CheckBoxList 控件用来建立一个多选的复选框组. CheckBoxList 控件中的每个可选项由一个 ListItem 元素来定义! 提示 ...
- jquery解析xml
更多的项目都是在解析json,今天临时让解析几个xml文件,其实都一样,总结一下吧. 例如我们有这样一个xml文件 <?xml version="1.0" encoding= ...