【Palindrome Number】cpp
题目:
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.
代码:
class Solution {
public:
bool isPalindrome(int x)
{
if (x<) return false;
return x==Solution::reverse(x);
}
static int reverse(int x)
{
int ret = ;
while (x)
{
if ( ret>INT_MAX/ || ret<INT_MIN/ ) return ;
ret = ret* + x%;
x = x/;
}
return ret;
}
};
tips:
1. 如果是负数,认为不是回文数
2. 如果是非负数,则求这个数的reverse,如果等于其本身就是回文数,否则就不是。
利用的就是reverse integer这道题(http://www.cnblogs.com/xbf9xbf/p/4554684.html)。
====================================
还有一种解法可以不用reverse,有点儿类似求字符串是否是回文的题目(头尾两个指针往中间夹逼)。
class Solution {
public:
bool isPalindrome(int x)
{
if (x<) return false;
int d = ;
while ( x/d>= ) d = d*;
while ( x> )
{
int high = x/d;
int low = x%;
if (high!=low) return false;
x = x%d/;
d = d/;
}
return true;
}
};
对于这道题的整数来说,就是找到给定整数的最大位数-1,即d。
每次从高位取一个数,从低位取一个数;然后,再把高低位都去掉获得新数(x=x%d/10);这样照比上一次的数少了两位,因此还有d=d/100。
===========================================
第二次过这道题,根据题意提示,用reverse的结果判断是否是palindrome number。
class Solution {
public:
bool isPalindrome(int x)
{
if ( x< ) return false;
int rev = Solution::reverse(x);
return rev==x;
}
static int reverse(int x)
{
vector<int> digits;
int sign = x> ? : -;
int ret = ;
while ( true )
{
int digit = fabs(x%);
if ( INT_MAX/<ret || (INT_MAX/==ret && INT_MAX%<digit) )
{
return ;
}
ret = ret* + digit;
x = x/;
if ( fabs(x)== ) break;
}
return ret*sign;
}
};
【Palindrome Number】cpp的更多相关文章
- 【Palindrome Partitioning】cpp
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- 【Valid Number】cpp
题目: Validate if a given string is numeric. Some examples:"0" => true" 0.1 " = ...
- 【Single Number】cpp
题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...
- 【Letter Combinations of a Phone Number】cpp
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- 【Sort Colors】cpp
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【Stirling Number】
两类Stirling Number的简介与区别(参考自ACdreamer的CSDN) Stirling Number I --- s(n,k):将n个物体排成k个非空循环排列(环)的方法数. 递推式: ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- Hdu 4734 【数位DP】.cpp
题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...
- 【system.number】使用说明
对象:system.number 说明:提供一系列针对数值类型的操作 目录: 方法 返回 说明 system.number.isNumber( number ) [True | False] 检测是否 ...
随机推荐
- JS浏览器获取宽高
screen.availHeight is the height the browser's window can have if it is maximized. (including all th ...
- linux 命令——40 wc (转)
Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式: wc [选项]文件... 2.命令功能: 统计指定文件中的字节数. ...
- Windows 服务快捷启动命令,可以直接在运行处运行电脑的功能。
gpedit.msc-----组策略 sndrec32-----录音机 nslookup----- ip地址侦测器 explorer------ 打开资源管理器 logoff-------注销命令 t ...
- 新建framework的bundle资源 图片资源被编译成了ttf后缀 解決
设置combine_hidpi_images为no
- Opentsdb简介
1.OpenTSDB介绍 1.1.OpenTSDB是什么?主要用途是什么? 官方文档这样描述:OpenTSDB is a distributed, scalable Time Series Datab ...
- SSH框架快速搭建(Maven)
1.新建Maven项目ssh 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=& ...
- Sql优化器究竟帮你做了哪些工作?
关系型数据库的一大优势之一,用户无需关心数据的访问方式,因为这些优化器都帮我们处理好了,但sql查询优化的时候,我不得不要对此进行关注,因为这牵扯到查询性能问题. 有经验的程序员都会对一些sql优化了 ...
- C/C++程序基础 (二)常用知识点
使用宏实现max 注意括号在宏内的使用 #define MAX(x, y) ( ( (x) > (y) ) ? (x) : (y) ) 宏参数连接 a##e##b 转化为字符串 #a const ...
- ASP.NET Core模块化前后端分离快速开发框架介绍之3、数据访问模块介绍
源码 GitHub:https://github.com/iamoldli/NetModular 演示地址 地址:https://nm.iamoldli.com 账户:admin 密码:admin 前 ...
- mysql四:数据操作
一 介绍 MySQL数据操作: DML ======================================================== 在MySQL管理软件中,可以通过SQL语句中的 ...