leetcode 7
此题实现比较简单,但是边界处理比较麻烦。题目要求是以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的更多相关文章
- 我为什么要写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 ...
随机推荐
- 转-Fragment+FragmentTabHost组件(实现新浪微博底部菜单)
http://www.cnblogs.com/lichenwei/p/3985121.html 记得之前写过2篇关于底部菜单的实现,由于使用的是过时的TabHost类,虽然一样可以实现我们想要的效果, ...
- HTTP 错误 500.19 - Internal Server Error
ylbtech-Error-IIS: HTTP 错误 500.19 - Internal Server Error 1.A,错误代码返回顶部 错误摘要 HTTP 错误 500.19 - Interna ...
- ASP 发送邮件
ASP发送邮件源码 ASP通过调用API接口发送邮件 <% ' '网吧数据 'www.zgw8.com '邮件发送接口调用demo ' ' '获取网页源代码函数 '=============== ...
- 【jQuery】关于选择器中的 :first 、 :first-child 、 :first-of-type
[:first] <!DOCTYPE html><html lang="zh-CN"><head> <title>test&l ...
- java io InputStream 转 byte
InputStream is ; ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] b = new byte[1024] ...
- 判断DataTable是否为空
DataTable: ) { //为空的操作 } DataRow: if(!DataRow.IsNull("列名")) { //不为空的操作 }
- CRM报表打印
删除路径下的文件 C:\Windows\Downloaded Program Files\rsclientprint.dll路径下的这个dll文件,重新登录crm选择一个面单点击打印按钮重新安装插件
- js 节流函数 throttle
/* * 频率控制 返回函数连续调用时,fn 执行频率限定为每多少时间执行一次 * @param fn {function} 需要调用的函数 * @param delay {number} 延迟时间, ...
- webstrom配置node环境一张图片说明问题
- ppm与毫克/立方米怎么换算
ppm是溶液浓度(溶质质量分数)的一种表示方法,1升水溶液中有1毫克的溶质,g/m3或mg/L. 对于气体:,一百万体积的空气中所含污染物的体积数. 而按我国规定,特别是环保部门,则要求气体浓度以质量 ...