【Leet Code】Palindrome Number
Palindrome Number
Total Accepted: 19369 Total
Submissions: 66673My Submissions
Determine whether an integer is a palindrome. Do this without extra space.
推断一个数整数是不是回文?比如121,1221就是回文,好吧,直接利用前面写过的【Leet
Code】Reverse Integer——“%”你真的懂吗?
只是这里要考虑翻转后,数值溢出的问题,代码例如以下:
/*
//first method
class Solution {
public:
bool isPalindrome(int x)
{
long long temp = x;
long long ret = 0;
bool isNegative = false;
if (temp < 0)
{
return false;
}
while (temp)
{
ret = ret * 10 + temp % 10;
temp /= 10;
}
if(x == ret)
{
return true;
}
else
{
return false;
} }
};
*/
当然,我们还有更好的方法,事实上,初见这个题目,第一个想法是取头取尾进行比較,然后把头尾去掉,再循环,直到数值为个位数为止:
class Solution {
public:
bool isPalindrome(int x)
{
if (x < 0)
{
return false;
}
int divisor = 1;
while (x / divisor >= 10)
{
divisor *= 10;
}
while (x)
{
if (x / divisor != x % 10)
{
return false;
}
x = (x % divisor) / 10;
divisor /= 100;
}
return true;
}
};
【Leet Code】Palindrome Number的更多相关文章
- 【Leet Code】String to Integer (atoi) ——常考类型题
String to Integer (atoi) Total Accepted: 15482 Total Submissions: 106043My Submissions Implement ato ...
- 【POJ 1159】Palindrome
[POJ 1159]Palindrome 近期各种题各种奇葩思路已经司空见惯了...又新出个滚动数组= = 该题另一点须要知道 最少须要补充的字母数 = 原序列S的长度 - S和S'的最长公共子串长度 ...
- 【LeetCode算法-9】Palindrome Number
LeetCode第9题 Determine whether an integer is a palindrome. An integer is a palindrome when it reads t ...
- 【codeforces 805D】Minimum number of steps
[题目链接]:http://codeforces.com/contest/805/problem/D [题意] 给你一个字符串; 里面只包括a和b; 让你把里面的"ab"子串全都去 ...
- 【转】【VS Code】配置文件Launch及快捷键
Ctrl+shift+p,然后输入launch,点击第一个选项即可配置. 之后选择More即可 具体配置可修改为: { "version": "0.2.0", ...
- 【CF#303D】Rotatable Number
[题目描述] Bike是一位机智的少年,非常喜欢数学.他受到142857的启发,发明了一种叫做“循环数”的数. 如你所见,142857是一个神奇的数字,因为它的所有循环排列能由它乘以1,2,...,6 ...
- 【POJ 3974】 Palindrome
[题目链接] http://poj.org/problem?id=3974 [算法] 解法1 : 字符串哈希 我们可以分别考虑奇回文子串和偶回文子串,从前往后扫描字符串,然后二分答案,检验可以用哈希 ...
- 【leetcode】Palindrome Number
题目简述: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could n ...
- 【leetcode】Palindrome Number (easy)
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
随机推荐
- Nginx服务器的启动控制
写在前面的话:读书破万卷,编码如有神--------------------------------------------------------------------主要内容包括: nginx服 ...
- Spring---介绍
核心容器:Core.Beans.Context.EL模块 1. Core模块:封装了框架依赖的最底层部分,包括访问资源.类型转换及一些常用工具类 2. Beans模块:提供了框架的基础 ...
- python画激活函数图像
导入必要的库 import math import matplotlib.pyplot as plt import numpy as np import matplotlib as mpl mpl.r ...
- Centos 6/ 7下通过yum安装php7环境
本文转自:云溪社区 2015年12月初PHP7正式版发布,迎来自2004年以来最大的版本更新.PHP7最显著的变化就是性能的极大提升,已接近Facebook开发的PHP执行引擎HHVM.在WordPr ...
- perf 函数调用性能(函数流程图)
perf record -g -p pid perf record -g -t tid perf report -g fractal,0.2,caller perf report -g fractal ...
- ABC定制视图导航控制器
ABCustomUINavigationController ABC定制视图导航控制器 Subclass of UINavigationController for overwriting ...
- 安卓源码下载 windows
git clone https://android.googlesource.com/name Name Descriptionaccessories/manifest device/asus/deb ...
- Python学习(六)模块
Python 模块 模块定义 随着程序越来越庞大,需要分隔成几个文件:也需要能在不同文件中复用函数.类和变量,而不是拷贝代码.为了满足这些需要,Python提供了模块. 简单来说,模块就是一个保存了P ...
- Python学习(七)面向对象 ——类和实例
Python 面向对象 —— 类和实例 类 虽然 Python 是解释性语言,但是它是面向对象的,能够进行对象编程.至于何为面向对象,在此就不详说了.面向对象程序设计本身就很值得深入学习,如要了解,请 ...
- [转]MySQL Explain
Mysql Explain 详解 一.语法 explain < table_name > 例如: explain select * from t3 where id=3952602; 二. ...