LeetCode(9)Palindrome Number
题目:
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.
分析:
AC代码:
class Solution {
public:
bool isPalindrome(int x) {
if(x < 0)
return false;
int size = 0 , tmp = x;
while(tmp != 0 )
{
tmp /= 10;
size++;
}//while
int p = x , q = x , i , j;
for(i=1 ,j=size ; i<j ; i++ , j--)
{
int a = pow(10 , j-1);
if(p/a != q%10)
{
return false;
}else{
p %= a;
q /= 10;
}//else
}//for
return true;
}
};
LeetCode(9)Palindrome Number的更多相关文章
- LeetCode(137) Single Number II
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- LeetCode(234) Palindrome Linked List
题目 Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) t ...
- LeetCode(202) Happy Number
题目 Write an algorithm to determine if a number is "happy". A happy number is a number defi ...
- LeetCode(131)Palindrome Partitioning
题目 Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- LeetCode(306) Additive Number
题目 Additive number is a string whose digits can form additive sequence. A valid additive sequence sh ...
- LeetCode(65) Valid Number
题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...
- LeetCode(260) Single Number III
题目 Given an array of numbers nums, in which exactly two elements appear only once and all the other ...
- LeetCode(268) Missing Number
题目 Given an array containing n distinct numbers taken from 0, 1, 2, -, n, find the one that is missi ...
- LeetCode(136) Single Number
题目 Given an array of integers, every element appears twice except for one. Find that single one. Not ...
随机推荐
- A.走方格
链接:https://ac.nowcoder.com/acm/contest/368/A 题意: 在一个n*n的方格中,你只能斜着走. 你还有一次上下左右走的机会 给你一个起点(sx,sy),和终点( ...
- 洛谷P1896||bzoj1087 [SCOI2005]互不侵犯
bzoj1087 洛谷P1896 想了很久,太久没做状压都已经不会了... 状压每一行就好了 #include<cstdio> #include<algorithm> #inc ...
- Jasper_crosstab_group_dataset_Error incrementing crosstab dataset
Error detail: net.sf.jasperreports.engine.JRException: net.sf.jasperreports.engine.JRRuntimeExceptio ...
- jquery offsetParent()源码解读
offsetParent: function() { return this.map(function() { var offsetParent = this.offsetParent || docE ...
- Windows7环境下Apache连接MySQL提示“连接已重置”的解决办法
win7下手动搭建wamp环境,碰到的几个坑总结下, 1.能正常访问php和html类型文件,但是访问项目文件时老是连接被重置,后来总结是数据库的问题,就写测试用例测试php能否成功调用数据库, &l ...
- Django的学习需要掌握的一些基础和初步搭建自己的框架
一.Django的学习需要掌握的一些基础 第一个需要注意的点:客户端发送过来的数据结构组成: 第二个需要注意的点:动态网页和静态网页 静态网页:用户发送请求,服务端找到对应的静态文件返回给浏览器,静态 ...
- 用CSS3和伪元素绘制三角形
具体怎样的写法,参照右边链接:https://segmentfault.com/a/1190000002783179 加以改良,不想多一个标签,可以直接利用伪元素,以下面代码为例所示: html代码: ...
- list control 【转】
1. CListCtrl 风格 LVS_ICON: 为每个item显示大图标 LVS_SMALLICON: 为每个item显示小图标 LVS_LIST: 显示一列带有小图标的ite ...
- css布局两边固定中间自适应的四种方法
第一种:左右侧采用浮动 中间采用margin-left 和 margin-right 方法. 代码如下: <div style="width:100%; margin:0 auto;& ...
- 图像处理框架 Core Image 介绍
这篇文章会为初学者介绍一下 Core Image,一个 OS X 和 iOS 的图像处理框架. 如果你想跟着本文中的代码学习,你可以在 GitHub 上下载示例工程.示例工程是一个 iOS 应用程序, ...