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 ...
随机推荐
- 119 Pascal's Triangle II 帕斯卡三角形 II Pascal's Triangle II
给定一个索引 k,返回帕斯卡三角形(杨辉三角)的第 k 行.例如,给定 k = 3,则返回 [1, 3, 3, 1].注:你可以优化你的算法到 O(k) 的空间复杂度吗?详见:https://leet ...
- python学习之调试:
编写的代码不会都能完好运行,所以需要调试,解决错误和异常,常有几种方法: 1 通过printf()来打印信息.但在发布时无法删除: 2 通过assert 条件表达式,‘提示信息’:启动解释器时通过 - ...
- 我喜欢的两个js类实现方式 现在再加上一个 极简主义法
闭包实现 变量是不会变的:) var myApplication = function(){ var name = 'Yuri'; var age = '34'; var status = 'sing ...
- CSS Secrets 翻译笔记 01: CSS coding tips
.firDemoButton{ padding: 6px 16px; border: 1px solid #446d88; background: #58a linear-gradient(#77a0 ...
- Nacos部署中的一些常见问题汇总
开个帖子,汇总一下读者经常提到的一些问题 问题一:Ubuntu下启动Nacos报错 问题描述 使用命令sh startup.sh -m standalone启动报错: ./startup.sh: 78 ...
- 【踩坑】Safari不兼容webpack封装的vue项目
刚完成 Iblog 博客项目,在 chrome 浏览器调试完后,用 Safari 打开网站,页面一直停留在加载状态. 后来网上说这是 Safari 不支持 ES6 所致. 经过搜索,在 github ...
- poj3046
dp,可以再优化. #include <iostream> #include <cstdio> #include <cstring> using namespace ...
- Eclipse优化工具Optimizer for Eclipse
第一次看到是Optimizer for Eclipse是在InfoQ 然后使用了一下,发现不错啊,我的好几年的破本都能比较快的启动Eclipse了 好了,废话不说了,来介绍一下Optimizer fo ...
- 显示大图Activity(支持手势放大)
xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:andro ...
- IOS生成UUID
/** * 生成GUID */ + (NSString *)generateUuidString{ // create a new UUID which you own CFUUIDRef uuid ...