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 ...
随机推荐
- ZJOI2017 day2 T2 线段树 想法题
考完D2发现自己简直zz了...花式扔基本分 首先这道题有个显然的套路:树上一些点到一个定点的距离和=这些点深度和+点数*定点深度和-2*lca深度和 ——上一次见这个套路是LNOI2014,上次做的 ...
- siege官方文档(译)(二)
WHY DO I NEED IT? Siege was written for both web developers and web systems administrators. siege是为了 ...
- centos设置ssh免密码登陆
准备工作: 1.确认本机sshd的配置文件(需要root权限) $ gedit /etc/ssh/sshd_config 找到以下内容,并去掉注释符”#“ RSAAuthentication y ...
- C++使用ADO连接数据库及其实例
读写数据库的技术很多,现在多用ADO.ADO以COM方式提供,所以它的很多行为遵循COM规范.首先,要引入ADO的COM文件,它的位置一般在"C:/Program Files/Common ...
- C. Hamburgers
Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own han ...
- iOS UDP 广播 AsyncSocket 用法
因为业务需要,需要用广播发送一个字段,在iOS开发中,用到了AsynSocket. 1.定义一个属性,负责发送和接受数据 #define YX_Local_Host @"255.255.25 ...
- setuid
-r-s--x--x #s就是setuid,仅可用在二进制文件,对目录设置无效
- UVA 11324 The Largest Clique (强连通分量,dp)
给出一个有向图,求一个最大的结点集合,任意两个点u,v.u可到达v或v可到达u. 一个强连通分量肯定一起选的.而且只能在一条路径上. 所以先找出所有scc,然后缩点找一条最大权的路径,按拓扑序跑DAG ...
- map最基本操作
#include<iostream> #include<map> using namespace std; int main() { /*map<int,char> ...
- canvas 在视频中的用法
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...