题目

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. Unexpected EOF 远程主机强迫关闭了一个现有的连接 如何处理

    由于数据量的增大,调用接口的次数会增加. 当连续向目标网站发送多次request后,目标网站可能会认为是,恶意攻击. 于是会抛出requests异常. 测试代码: for i in range(200 ...

  2. JAVA值传递和引用传递 以及 实参是否改变

    八大数据类型和String 在进行传递的时候  不会改变. 八大数据类型 public class parameterValue { //值传递 public static void main(Str ...

  3. Android SQLite(1)简单示例-增,删,改,查

    1.主要核心类,Sqlite编程要继承SQLiteOpenHelper import android.content.Context; import android.database.sqlite.S ...

  4. hdu 1430 魔板 康托展开 + 很好的映射

    http://acm.hdu.edu.cn/showproblem.php?pid=1430 如果从start ---> end,每一次都bfs进行,那么就肯定会超时. 考虑到先把start映射 ...

  5. 如何通过SecureCRT作为客户端连接Linux服务器

    主机cmd ping虚拟机失败 打开计算机-管理-服务,找到所有以VMare开头的服务,右键点击启动即可,此时主机即可ping通虚拟机 可ping通之后,在主机cmd窗口输入 ssh root@192 ...

  6. poj3662 Telephone Lines

    思路: 二分+最短路.最短路也可以用来计算从a到达b所需的边权不超过x的边的数量. 实现: #include <cstdio> #include <cmath> #includ ...

  7. hihocoder offer收割编程练习赛12 D 寻找最大值

    思路: 可能数据太水了,随便乱搞就过了. 实现: #include <iostream> #include <cstdio> #include <algorithm> ...

  8. 'NSUnknownKeyException' … setValue:forUndefinedKey:]: …not key value coding compliant

    解决一个问题: 当我添加一个IBout, 报了如下错误 NSUnknownKeyException' … setValue:forUndefinedKey:]: …not key value codi ...

  9. 解剖嵌入式设备开发时以SD卡启动时SD卡的存储结构(以三星exynos4412为例)

    目前面对高性能产品的嵌入式开发中,用SD卡来代替以往的JLINK显得备受大家喜欢,而且MCU厂家也对以SD卡启动的支持度越来越大,反而对JLINK不在那么重视(不过依旧保留着).一些以开发开发板的公司 ...

  10. IT实习总结-上海的实习的日子

    在上海实习两个多月,从找工作,到面试,最后到创业公司工作,体会了一个人在外工作的那种工作时的压力与双休时的寂寞,有时甚至有点迷惘,可能这就是生活,每个人都会经历,在经历的过程中会有不同的味道. 说说我 ...