9. Palindrome Number QuestionEditorial Solution
Determine whether an integer is a palindrome. Do this without extra space.
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.
每次区首尾两位进行比较,然后再去掉首尾两位循环。
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cmath>
using namespace std; class Solution {
public:
bool isPalindrome(int x) {
if (x < 0)
{
return false;
}
//数字长度
int len = 0;
int temp = x;
while(temp != 0)
{
temp = temp / 10;
len++;
}
while(x != 0)
{
int bottom = x % 10;
int top = x / (int)pow(10, len - 1);
if (bottom != top)
{
return false;
}
x = (x % (int)pow(10, len - 1)) / 10;
len -= 2;
}
return true;
}
}; int main()
{
Solution s;
cout << s.isPalindrome(123212) << endl;
return 0;
}
9. Palindrome Number QuestionEditorial Solution的更多相关文章
- 65. Reverse Integer && Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, re ...
- 9. Palindrome Number
/* Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers ...
- 【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 ...
- leetcode 第九题 Palindrome Number(java)
Palindrome Number time=434ms 负数不是回文数 public class Solution { public boolean isPalindrome(int x) { in ...
- Reverse Integer - Palindrome Number - 简单模拟
第一个题目是将整数进行反转,这个题实现反转并不难,主要关键点在于如何进行溢出判断.溢出判断再上一篇字符串转整数中已有介绍,本题采用其中的第三种方法,将数字转为字符串,使用字符串比较大小的方法进行比较. ...
- leetcode题解 9. Palindrome Number
9. Palindrome Number 题目: Determine whether an integer is a palindrome. Do this without extra space. ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- Palindrome Number - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Palindrome Number - LeetCode 注意点 负数肯定是要return false的 数字的位数要分奇数和偶数两种情况 解法 解法一: ...
- 【LeetCode】9. Palindrome Number (2 solutions)
Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. click t ...
随机推荐
- $Poj2956/AcWing116\ The\ Pilots\ Brothers'Refrigerator$ 二进制
AcWing $Sol$ 假设改变$[x1,y1]$和$[x2,y2]$的状态就可以达到目的.注意到先改变谁对结果是没有影响的!! 所以就可以直接枚举改变状态的结点而不需要注意顺序. $4*4$的矩阵 ...
- spring boot 中AOP的使用
一.AOP统一处理请求日志 也谈AOP 1.AOP是一种编程范式 2.与语言无关,是一种程序设计思想 面向切面(AOP)Aspect Oriented Programming 面向对象(OOP)Obj ...
- zabbix监控web应用日志报警并发送消息到钉钉
首先在钉钉上开启钉钉机器人功能 说明:自定义关键词是zabbix发送过来的消息内容必须含有你定义的ERROR或者error字段,否则消息无法发送过来 ip地址段:一般都是zabbix-server的I ...
- ubuntu下使用APT安装和卸载MySQL5.7
安装方式一: 向系统的软件仓库中列表中添加MySQL APT 仓库 去http://dev.mysql.com/downloads/repo/apt/.下载MySQL APT repository ...
- 使用docker增加部署速度的一次实践
问题: 公司给我们分配的服务器到期后不付费了,换成新服务商的服务器了.也就是说我们之前的环境需要重新搭建一次.光项目就50多个(微服务40+,其他服务不到10个),需要重新部署. 之前部署项目时,需要 ...
- C++类中的重载
函数重载回顾 函数重载的本质为相互独立的不同函数 C++中通过函数名和函数参数确定函数调用 无法直接通过函数名得到重载函数的入口地址 函数重载必然发生在同一个作用域 类中的成员函数可以进行重载 构造函 ...
- crawler碎碎念5 豆瓣爬取操作之登录练习
import requests import html5lib import re from bs4 import BeautifulSoup s = requests.Session() #这里要提 ...
- ERROR 1235 (42000): This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'问题的解决
.与limit相关的sql语句作为临时表 select * from 临时表 limit ) as B 缺点:只能查临时表的数据 .可以查原表的数据 select * from test where ...
- 【红外DDE算法】聊聊红外图像增强算法的历史进程(第一回)
宽动态红外图像增强算法综述回顾过去带你回顾宽动态红外图像增强算法的历史进程,历来学者的一步步革命(新的算法框架提出),一步步改革(改进优化),从简单粗暴到细致全面.正所谓是:改革没有完成时,只有进行时 ...
- 关于C++读入数字按位取出与进制转换问题
这一片博客我就不写具体的一个题了,只是总结一种典型问题——读入数字按位取出. 就拿数字12345举例吧. 是首先,我们要取出个位.这样取出: 12345/1=12345 12345%10=5. ...