【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 ...
随机推荐
- 【算法笔记】B1030 完美数列(三种方法)
1030 完美数列 (25 分) 给定一个正整数数列,和正整数 p,设这个数列中的最大值是 M,最小值是 m,如果 M≤mp,则称这个数列是完美数列. 现在给定参数 p 和一些正整数,请你从中选择尽可 ...
- laravel的一些语法
5.1的一些语法 检索表中的所有行 $users = DB::table('users')->get();因为查询出来的是对象,所以可以直接选择DB::table('users')->ge ...
- Yii2 执行Save()方法失败,却没有错误信息
一般用$model->errors 就能查看到更新失败的原因,但是这次却什么错误信息都没有,最后发现是因为在模型类中定义了一个方法 public function beforeSave($ins ...
- Oracle RAC集群删除节点
一,节点环境 [root@node1 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost ...
- (转)同步异步,阻塞非阻塞 和nginx的IO模型
同步异步,阻塞非阻塞 和nginx的IO模型 原文:https://www.cnblogs.com/wxl-dede/p/5134636.html 同步与异步 同步和异步关注的是消息通信机制 (sy ...
- ife task0001页面实现细节问题总结
好久没写css了,突然对重构页面陌生了许多.不过也没什么,前面几个月一直扩充知识面,偏重了理论技术学习,结果还不算遗憾.昨天重拾css,针对问题做点总结: 一.语义化方面 1.HTML5新标签使用 标 ...
- temp脚本
!/bin/bash source ${HOME_DIR}/script/ideploy_dm.inc source ${HOME_DIR}/script/comm_lib home_dir=$(cd ...
- 网站加入QQ聊天链接
有时候我们的网站需要加入客服聊天功能,实现方式各不相同同,对于流量不大的网站,可以加入qq聊天的链接,点击链接,会打开本地qq的聊天窗口, 和指定的人会话.实现方式很简单,就是一个<a>标 ...
- Coursera 机器学习 第6章(上) Advice for Applying Machine Learning 学习笔记
这章的内容对于设计分析假设性能有很大的帮助,如果运用的好,将会节省实验者大量时间. Machine Learning System Design6.1 Evaluating a Learning Al ...
- 腾讯蔡晨:十年沉淀,腾讯iOA为企业安全保驾护航
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 背景:5月23-24日,以"焕启"为主题的腾讯"云+未来"峰会再广州召开,广东省各级政府机构领导.海 ...