leetcode题解||Palindrome Number问题
problem:
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. 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.
thinking:
(1)之前求过int型逆序反转。这里检验一个数是否为一个回文数,能够得到一点启示。
(2)逆序反转主要涉及溢出问题的处理,并且反转的空间复杂度能够忽略不计,满足题目要求。
code:
class Solution {
public:
bool isPalindrome(int x) {
int b=0;
int a=x;
bool flag=true;
if(a<0)
{
bool flag=false;
a=-x;
}
while(a)
{
b*=10;
b+=a%10;
a=a/10;
}
if(b>2147483647)
return false;
if(!flag)
b=-b;
return (b==x);
}
};
leetcode题解||Palindrome Number问题的更多相关文章
- LeetCode题解——Palindrome Number
题目: 判断一个数字是不是回文数字,即最高位与最低位相同,次高位与次低位相同,... 解法: 求出数字的位数,然后依次求商和求余判断是否相等. 代码: class Solution { public: ...
- Leetcode 9. Palindrome Number(水)
9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome w ...
- Leetcode练习题 Palindrome Number
9. Palindrome Number Question: Determine whether an integer is a palindrome. An integer is a palindr ...
- leetcode 9 Palindrome Number 回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- LeetCode 9. Palindrome Number (回文数字)
Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, ...
- Leetcode 9. Palindrome Number(判断回文数字)
Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...
- [LeetCode][Python]Palindrome Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/palindr ...
- LeetCode——9. Palindrome Number
一.题目链接:https://leetcode.com/problems/palindrome-number/ 二.题目大意: 给定一个整数,判断它是否为一个回文数.(例如-12,它就不是一个回文数: ...
- 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]
题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
随机推荐
- dubbo-monitor安装及配置过程
安装 1. 使用git下载(git clone https://github.com/alibaba/dubbo.git)或者从http://dubbo.io/下载源码 2. cd到dubbo的根目录 ...
- 云服务IaaS,PaaS,SaaS
IaaS:基础设施服务,Infrastructure-as-a-service PaaS:平台服务,Platform-as-a-service SaaS:软件服务,Software-as-a-serv ...
- Java多线程学习笔记(四)——Thread类中方法介绍
currentThread():返回代码正在被哪个线程调用. public class CurrentThreadWay { public static void main(String[] args ...
- Docker方式安装QIIME 2
# 下载最新版QIIME 2 docker pull qiime2/core:2017.7 # 测试是否安装成功 docker run -t -i -v $(pwd):/mnt/hgfs/2017 q ...
- Python基础之简介
参考原文 廖雪峰Python教程 什么是Python? Python是一种计算机程序设计语言,又被称为胶水语言,它是高级的编程语言. Python能干什么? 网站后端程序员.自动化运维.数据分析师.游 ...
- 19异常和file部分笔记
19异常和file部分笔记-2018/09/041.异常 1.1 throwable()几个常见方法 * getMessage()获取异常信息,返回字符串 * toString()获取异常类名和异常 ...
- x shell 连接不上本地虚拟机
登陆虚拟机服务器 输入ipconfig查看ip 地址(如果提示命令不存在,输入 ip addr) 输出结果中看标记处是否出现ip地址.我的打开后这里是没有地址的 然后输入 vi /etc/syscon ...
- type、object、class之间的关系
class Foo: pass print(type(int)) # <class 'type'> print(type(str)) # <class 'type'> prin ...
- Centos7二进制文件安装MySQL5.7.25
1.删除centos系统自带的mariadb数据库防止发生冲突 rpm -qa|grep mariadb rpm -e mariadb-libs --nodeps 2.安装libaio库 yum -y ...
- 移动端placeholder不能垂直居中解决方案
1.问题描述 问题如图:手机端placeholder文字偏上,垂直方向不居中,input光标显示偏上解决IE下不支持placeholder属性 2.解决方案 css .phoneNumber inpu ...