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 ...
随机推荐
- unity c#
gameObject //获取当前脚本挂载到的游戏对象 在Unity中就算使用了C#进行编写脚本,要输出时不能使用Console类,应当使用print();或者Debug.log(); transfo ...
- Xcode7--坑无法运行iOS9以下的模拟器
Unable to open liblaunch_sim.dylib. Try reinstalling Xcode or the simulator 解决办法 一.找到目标文件 /Applicati ...
- jstatd命令
jstatd [options] 此命令是一个RMI Server应用程序,提供了对JVM的创建和结束监视,也为远程监视工具提供了一个可以attach的接口options -nr 当一个存在的RMI ...
- Singleton 单例模板
// singleton.h #ifndef SINGLETON_H #define SINGLETON_H // 单例基类模板 template <class T> class Sing ...
- java小程序 质数
package com.test; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; im ...
- java小程序 实例 二分法查找
使用二分法在一个数组中查找一个数: package com.test; public class BinaryFind { private final static int size = 500000 ...
- linux驱动学习之Input输入子系统
以前,看过国嵌关于input子系统的视频课程,说实话,我看完后脑子里很乱,给我的印象好像是input子系统驱动是一个全新的驱动架构,疑惑相当多.前几天在网上,看到有很多人介绍韦东山老师的linux驱动 ...
- wince6.0应用程序自启动
主要思想:将应用程序添加到image里,然后用应用程序代替桌面应用程序,从而使应用程序自启动. 主要步骤: 1.将应用程序MyApp.exe拷贝到wince600\OS ...
- ARM开发板挂载U盘
ARM开发板运行Linux系统后,插入U盘,通过串口打印系统识别到U盘如下: 系统识别U盘盘符为sda4 那么需要挂载后才能操作U盘:挂载命令 mount -t vfat /dev/sda4 /mnt ...
- 11gR2 Clusterware and Grid Home - What You Need to Know
11gR2 Clusterware Key Facts 11gR2 Clusterware is required to be up and running prior to installing a ...