Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

Have you thought about this?

Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!

If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

Update (2014-11-10):
Test cases had been added to test the overflow behavior.

题意是反转十进制int并返回,需要注意的是,int表示范围为 -2147483648 ~ 2147483647,翻转之后的数值可能会溢出,而题意为:如果溢出则返回0。

long long int dmod(int n)
{
long long int i = ;
while (n--)
i *= ;
return i;
} int reverse(int x) {
int y = abs(x), bit, tmp = , len = , sign;
long long int res = ;
if (x== || x==-) return ;//int最小值取绝对值的话会溢出
sign = x/y;
while (len> && y / dmod(len-) == )
{
len --;
}
for (bit=; bit<=len; bit++)
{
tmp = y / dmod(bit-) % ;
res += tmp * (dmod(len-bit));
}
//printf("sign = %d, res = %d\n", sign, res);
if (res > ) return ;
return sign * (int)res;
}

LeetCode第七题的更多相关文章

  1. leetcode第七题--Reverse Integer

    Problem: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...

  2. leetcode第七题Reverse Integer (java)

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...

  3. LeetCode 第七题--整数反转

    1. 题目 2.思路 1. 题目 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123输出: 321 示例 2: 输入: -123输出: -321示例 ...

  4. C#版 - Leetcode 504. 七进制数 - 题解

    C#版 - Leetcode 504. 七进制数 - 题解 Leetcode 504. Base 7 在线提交: https://leetcode.com/problems/base-7/ 题目描述 ...

  5. Leetcode第1题至第10题 思路分析及C++实现

    笔者按照目录刷题,对于每一道题,力争使用效率最高(时间复杂度最低)的算法,并全部通过C++代码实现AC.(文中计算的复杂度都是最坏情况复杂度) 因为考虑到大部分读者已经在Leetcode浏览过题目了, ...

  6. leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  7. 《学习OpenCV》练习题第四章第七题abc

    题外话:一直是打算把这本书的全部课后编程题写完的,中间断了几个月,一直忙于其他事.现在开始补上. 这道题我不清楚我理解的题意是不是正确的,这道题可以练习用OpenCV实现透视变换(可以用于矫正在3维环 ...

  8. 经典算法题每日演练——第七题 KMP算法

    原文:经典算法题每日演练--第七题 KMP算法 在大学的时候,应该在数据结构里面都看过kmp算法吧,不知道有多少老师对该算法是一笔带过的,至少我们以前是的, 确实kmp算法还是有点饶人的,如果说红黑树 ...

  9. leetcode第37题--Count and Say

    题目:(据说是facebook的面试题哦) The count-and-say sequence is the sequence of integers beginning as follows:1, ...

随机推荐

  1. intellij idea与github整合管理代码

    各位看官大家好,博主每每在公司学习新知识写代码时都需要通过U盘带回家来继续每天的学习,觉得这样实在麻烦,于是今天就整合了一下github来完成代码的管理. 开始之前我们需要准备三样东西:1.intel ...

  2. Git简易使用教程

    1.Git 安装 2.设置git登录信息 3.git操作命令 4.提交代码的过程中几个命令的顺序 5.git 学习资料. 1.Git 安装 Git 下载地址:https://git-scm.com/d ...

  3. 学习Canvas这一篇文章就够了

    一.canvas简介 ​ <canvas> 是 HTML5 新增的,一个可以使用脚本(通常为JavaScript)在其中绘制图像的 HTML 元素.它可以用来制作照片集或者制作简单(也不是 ...

  4. 开发规范 小白进阶 python代码规范化

    开发规范 软件开发,规范项目的目录结构,代码规范,遵循 PeP8规范等等,让你更加清晰的,合理开发 一功能分类(文件名) settings.py配置文件 配置文件放一些静态参数, 划归固定的路径,文件 ...

  5. Mybatis mapper动态代理的原理详解

    在开始动态代理的原理讲解以前,我们先看一下集成mybatis以后dao层不使用动态代理以及使用动态代理的两种实现方式,通过对比我们自己实现dao层接口以及mybatis动态代理可以更加直观的展现出my ...

  6. 项目启动会(project initiating meeting)与项目开工会(kick-off meeting)区别

    一.项目启动会initiating meeting 召开时间:是启动阶段结束时召开的会议:主要任务:发布项目章程,并任命项目经理,赋予项目经理动用组织资源的权力:注意事项:(1)会议召开前已经对干系人 ...

  7. 微服务API通过ip可访问,域名不可访问问题分析

    摘要 经常会有同学遇到api通过ip可以访问,但是通过域名却不可以访问.本篇文章总结了造成这种情况可能的原因. 因为与具体技术的选型.规则配置有关,所以没有深入讨论,只是列出可能性,仅供参考. 分析 ...

  8. Nginx总结(三)基于端口的虚拟主机配置

    前面讲了如何配置基于IP的虚拟主机,大家可以去这里看看nginx系列文章:https://www.cnblogs.com/zhangweizhong/category/1529997.html 今天就 ...

  9. zookeeper学习之原理

    一.zookeeper 是什么 Zookeeper是一个分布式协调服务,可用于服务发现,分布式锁,分布式领导选举,配置管理等.这一切的基础,都是Zookeeper提供了一个类似于Linux文件系统的树 ...

  10. Windows下Python 3.6 + VS2017 + Anaconda 解决Unable to find vcvarsall.bat问题

    Python 3.6 + VS2017 + Anaconda 解决Unable to find vcvarsall.bat问题 已经安装了VS2017,需要将项目中的C代码翻译为Python代码,在编 ...