leetcode 9

判断一个数是否为回文数,不利用额外的空间。
思路:将数反转后进行比较。
注意:反转之后数越界的判断,若越界,则不是回文数;负数不是回文数;
代码如下:
class Solution {
public:
bool isPalindrome(int x) {
int result = ;
int y = x;
if(x == || x < )
{
return false;
}
if(abs(x) < )
{
while(x != )
{
result = result * + x % ;
x = x / ;
}
}
else
{
int a[] = {,,,,,,,,,};
int i = ;
int flag = abs(x);
while(flag != )
{
if((flag % ) > a[i])
{
return ;
}
else if((flag % ) < a[i])
{
while(x != )
{
result = result * + x % ;
x = x / ;
}
if(result == y)
{
return true;
}
}
flag /= ;
i++;
}
}
if(result == y)
{
return true;
}
return false;
}
};
leetcode 9的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- webView--总结
Anaroid WebView API详解--http://blog.csdn.net/zhangcanyan/article/details/51344090;Android5.1系统WebView ...
- JAVA 一个特殊的类 Object
一个特殊的类Object:它是java中所有对象的直接或间接父类,根父类(基类),它里面定义的功能是所有对象都应该具备的(所有的类,都是继承这个类的) 记住:当定义一个新类时,没有指明要继承某类,它默 ...
- tcpdump命令--实用篇
//查看本机与mysql的操作命令 注意 -i any表示监听所有网络接口,我们也根据自身情况选择网络接口 #tcpdump -i any -w - dst port 3306 |strings // ...
- 注意,ruby循环体定义的变量在结束时后,变量还存在
a = [1, 2, 3] for i in a b = 123 p i end p "b:#{b}" p i <ruby语言编程> 129页 倒数 第8行
- ASP isPostBack
源地址:http://blog.163.com/budong_weimin_zh/blog/static/129198524201061995455589/ ASP.NET中IsPostBack详解 ...
- [Java] java中的异常处理
Java中的异常类都继承自Throwable类.一个Throwable类的对象都可以抛出(throw). Throwable对象可以分为两组.一组是unchecked异常,异常处理机制往往不用于这组异 ...
- [技巧]把Excel里的数据插入到数据库里的方法
.如果先在6行数据的最后一列在插入一列数据,请先把列名写好,然后再第一行的该列下输入数字, 然后选中该单元格向下拖拽一个单元格然后就能看到黑色的小框, 双击右下角黑色的小点,6行数据就 会填上默认的第 ...
- [DataTable]控件排序事件中用DataView及DataTable排序
控件排序事件中用DataView及DataTable排序 文章分类:.net编程 在做ASP.NET页面开发时,经常要用到dataset(或者DataTable),绑定到DataGrid或GridVi ...
- 倍增法lca
][N],siz[N];//rt数组需要在dfs之前置-1. void dfs(int pos,int deep){ dep[pos]=deep; siz[pos]=; for(edge *it=ad ...
- HappyNum
/*Write an algorithm to determine if a number is "happy". A happy number is a number defin ...