此题实现比较简单,但是边界处理比较麻烦。题目要求是以32位考虑,所以可表达的数的范围是-2147483648~2147483648。

我们需要判断当前的数翻转之后是否在这个范围中,我的思路是首先对当前数的绝对值进行判断,如果它不是一个10位数就可以正常的执行;

反之,进入判断边界的部分。将边界的最大值的每一位分别存入数组中。逐位进行比较,若翻转后越界,则返回0;

但是这样还是无法通过,会在边界处出错,即输入为-2147483648和2147483648就会得出错误的结果,正确的情况应该返回0;

对于上述的情况没有想明白原因,所以就取巧加了个判断,没想到竟然通过了。而且运行时间竟然是最短的,真是吓死我了!!

有大神看出哪里有毛病请一定告诉我,要不然我就瞎嘚瑟了~~

代码如下:

 class Solution {
public:
int reverse(int x) {
int result = ;
if(x == || x == -)
return ;
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 / ;
}
return result;
}
flag /= ;
i++;
}
}
return result;
}
};

leetcode 7的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [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 ...

  4. 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 ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  10. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. Linux中变量$#,$@,$0,$1,$2,$*,$$,$?的含义(转载)

    From:http://dadekey.blog.51cto.com/107327/119938 我们先写一个简单的脚本,执行以后再解释各个变量的意义   # touch variable # vi ...

  2. map遍历测试结果

    结论:一般情况下推荐使用enterSet的for循环(即以下的方法2),如果只是取key值可以使用keySet性能会更好. 因为keySet只取key,enterSet即取了key又取了value. ...

  3. 播放wav聲音格式

    1. #import <AudioToolbox/AudioToolbox.h> 2.聲明 成員变量 SystemSoundID soundID; 3.播放 - (void)playSou ...

  4. 92、App Permissions(权限管理)实例

    •Manifest权限声明 •Permission Groups-权限组 •权限的区分-安装时授权于运行时授权 •撤销权限 •检查.请求权限 •在应用中如何合理的处理权限请求逻辑   在Android ...

  5. Android 上的代码阅读器 CoderBrowserHD 修改支持 go 语言代码

    我在Android上的代码阅读器用的是 https://github.com/zerob13/CoderBrowserHD 改造的版本,改造后的版本我放在 https://github.com/ghj ...

  6. 安装配置opensips

    opensips提供了一个视频教程(这个页面有下载链接,90M),参考教程 wget http://opensips.org/pub/opensips/1.9.1/src/opensips-1.9.1 ...

  7. 使用jaxp对比xml进行SAX解析

    package cn.itcast.sax; import java.io.IOException; import javax.xml.parsers.ParserConfigurationExcep ...

  8. 求链表中倒数第k个节点

    注意鲁棒性和算法效率的典型例题:(头文件省略) typedef struct node { int data; struct node* next; }ListNode; ListNode* Find ...

  9. Chapter Querying Data

    Chapter Querying Data XF获取数据的三种方法: 其中参数schema,参见 Chapter Schema. 下面列出一个后面将会用到的schema片段,称之为片段A: <U ...

  10. git小操作之checkout、stash

    git checkout会带上当前changed但没有commit的内容到目标分支 git stash用来暂存当前改动,并且会退代码到上一个commit:git stash pop则取出所stash的 ...