题目

Divide two integers without using multiplication, division and mod operator.

If it is overflow, return MAX_INT.

分析

题目要求不用 * / %三种运算符的条件下,求得两个int类型整数的商。

方法一:

很明显的,我们可以用求和累计的方法,求得商,但是该方法测试会出现TLE;参考博客提出解决办法:每次将被除数增加1倍,同时将count也增加一倍,如果超过了被除数,那么用被除数减去当前和再继续本操作,但是我测试结果依然是TLE。所以这道题的目的在于考察逻辑运算。

方法二:

该方法来源于参考博客但是该实现忽略了结果溢出的问题,需要加上结果是否溢出判断。

TLE(方法一)代码

//方法一,翻倍累和  结果是:Time Limit Exceeded
class Solution {
public:
int divide(int dividend, int divisor) { //如果被除数或者除数有一者为0 或者绝对值除数大于被除数则返回0
if (dividend == 0 || divisor == 0 || abs(divisor) > abs(dividend))
return 0; int sign = ((dividend > 0 && divisor > 0) || (dividend < 0 && divisor < 0)) ? 1 : -1; long long Dividend = abs(dividend), Divisor = abs(divisor); long long sum = 0;
int count = 0, ret = 0; while (Divisor <= Dividend)
{
count = 1;
sum = Divisor;
while ((sum + sum) < Dividend)
{
sum += sum;
count += count;
}
Dividend -= sum;
ret += count;
} if (sign == -1)
return 0 - ret;
else
return ret;
}
};

AC代码

//方法二:位运算
class Solution {
public:
int divide(int dividend, int divisor) { //如果被除数或者除数有一者为0 或者绝对值除数大于被除数则返回0
if (dividend == 0 || divisor == 0)
return 0; // without using * / mod
// using add
auto sign = [=](long long x) {
return x < 0 ? -1 : 1;
}; int d1 = sign(dividend);
int d2 = sign(divisor); long long n1 = abs(static_cast<long long>(dividend));
long long n2 = abs(static_cast<long long>(divisor)); long long ans = 0; while (n1 >= n2) {
long long base = n2;
for (int i = 0; n1 >= base; ++i) {
n1 -= base;
base <<= 1;
ans += 1LL << i;
}
}
//如果转换为int类型,结果溢出,返回INT_MAX ,int类型表示范围[-2147483648 , 2147483648)
if (ans > INT_MAX && d1 == d2)
return INT_MAX; int res = static_cast<int>(ans);
if (d1 != d2)
return -res;
else
return res;
}
};

GitHub测试程序源码

LeetCode(29)Divide Two Integers的更多相关文章

  1. LeetCode(29): 两数相除

    Medium! 题目描述: 给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符. 返回被除数 dividend 除以除数 divisor ...

  2. Leetcode(29)-两数相除

    给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符. 返回被除数 dividend 除以除数 divisor 得到的商. 示例 1: 输 ...

  3. LeetCode(29)-Plus One

    题目: Given a non-negative number represented as an array of digits, plus one to the number. The digit ...

  4. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  5. LeetCode(4)Median of Two Sorted Arrays

    题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  6. JMS生产者+单线程发送-我们到底能走多远系列(29)

    我们到底能走多远系列(29) 扯淡: “然后我俩各自一端/望着大河弯弯/终于敢放胆/嘻皮笑脸/面对/人生的难”      --- <山丘> “迎着风/迎向远方的天空/路上也有艰难/也有那解 ...

  7. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(29)-T4模版

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(29)-T4模版 这讲适合所有的MVC程序 很荣幸,我们的系统有了体验的地址了.演示地址 之前我们发布了一 ...

  8. Windows Phone开发(29):隔离存储C

    原文:Windows Phone开发(29):隔离存储C 本文是隔离存储的第三节,大家先喝杯咖啡放松,今天的内容也是非常简单,我们就聊一件东东--用户设置. 当然了,可能翻译为应用程序设置合适一些,不 ...

  9. Qt 学习之路 2(29):绘制设备

    Qt 学习之路 2(29):绘制设备 豆子 2012年12月3日 Qt 学习之路 2 28条评论 绘图设备是继承QPainterDevice的类.QPaintDevice就是能够进行绘制的类,也就是说 ...

随机推荐

  1. 【POJ - 1661】Help Jimmy (动态规划)

    Help Jimmy Descriptions: "Help Jimmy" 是在下图所示的场景上完成的游戏. 场景中包括多个长度和高度各不相同的平台.地面是最低的平台,高度为零,长 ...

  2. ————————C语言中快速排序方法——————————————

    在对浮点型排序是一定要用三木运算符(三目运算符内容下去自己看),因为如果也是用整形那样的减法的时候如果是两个十分相近的数字 可能返回一个小数(自己一会去试试),冉冉他cmp返回值是int(试试别的)因 ...

  3. Go 连接PostgreSQL数据库

    先在PostgreSQL数据库中建一个表,可以使用PostgreSQL官方的pgAdmin来完成: CREATE TABLE userinfo ( uid serial NOT NULL, usern ...

  4. Java中关键字continue、break和return的区别

    Java中关键字continue.break和return的区别: continue:跳出本次循环继续下一次循环 break:   跳出循环体,继续执行循环外的函数体 return:   跳出整个函数 ...

  5. django templates模板

    Django templates模板 HTML代码可以被直接硬编码在views视图代码中,虽然这样很容易看出视图是怎么工作的,但直接将HTML硬编码到视图里却并不是一个好主意. 让我们来看一下为什么: ...

  6. 积分图像的应用(一):局部标准差 分类: 图像处理 Matlab 2015-06-06 13:31 137人阅读 评论(0) 收藏

    局部标准差在图像处理邻域具有广泛的应用,但是直接计算非常耗时,本文利用积分图像对局部标准差的计算进行加速. 局部标准差: 标准差定义如下(采用统计学中的定义,分母为): 其中. 为了计算图像的局部标准 ...

  7. 使用Dotfuscator保护.NET DLL加密DLL,防止DLL反编译

    1.下载地址 https://pan.baidu.com/s/1ztWlBxw1Qf462AE7hQJQRg 2.操作步骤 2.1安装后打开DotfuscatorPro软件,如下图所示: 2.2 选择 ...

  8. mysql 字段包含某个字符的函数

    通过sql查询语句,查询某个字段中包含特定字符串: 例子:查询e_book表的types字段包含字符串"3",有下面4种方式 select * from e_book where ...

  9. Java语法基础-final关键字

    final关键字主要用在三个地方:变量.方法.类. 对于一个final变量,如果是基本数据类型的变量,则其数值一旦在初始化之后便不能更改: 如果是引用类型的变量,则在对其初始化之后便不能再让其指向另一 ...

  10. 使用Jenkins进行android项目的自动构建(2)

    Maven and POM 1. 什么是Maven? 官方的解释是: http://maven.apache.org/guides/getting-started/index.html#What_is ...