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

If it is overflow, return MAX_INT.

解题思路:

模拟除法运算,但是不能试用 * / % 操作。

先思考边界条件,再开始做题:

0、除数为0无意义,需要和出题人沟通此边界是否存在(leetcode中并未考察除数为0的情况,默认除数不为0);

1、传入被除数/除数两个int做除法,什么情况返回值overflow:被除数为-2147483648,除数为-1,结果2147483648越界,此时按题目要求,应该返回INT_MAX。

2、在计算被除数包含多少个除数的过程中,需要不停的累加除数直到累加值超过被除数,此时累加值不可控,可能越界。因此应该使用long long 类型进行运算(32位/64位系统都适用)。

想好以上需要注意的边界条件后,使用二分查找的思路:

例如100 ÷ 7:

1、可先对除数7进行左移操作,7->14->28->56->112,112大于100,停止左移,左移倍数为8;

2、100 - 56 = 44,则继续计算44 ÷ 7,对7左移,7->14->28->56,56大于44,停止左移,左移倍数为4;

3、44 - 28 = 16,计算16 ÷ 7,左移倍数2;

4、16 - 14 = 2, 此时2 < 7, 左移倍数0,停止运算;

结果为 8 + 4 + 2 = 14.

代码编写过程中,注意被除数和除数的符号。

代码:

 class Solution {
public:
int divide(int dividend, int divisor) {
if (dividend == INT_MIN && divisor == -)
return INT_MAX; int sign = (dividend > ^ divisor > ) ? - : ;
long long end = abs((long long)(dividend));
long long sor = abs((long long)(divisor)); int ans = ;
while (end >= sor) {
long long cur_sor = sor;
int count = ;
while (cur_sor + cur_sor <= end) {
cur_sor <<= ;
count <<= ;
}
end -= cur_sor;
ans += count;
} if (sign > )
return ans;
else
return - ans;
}
};

【Leetcode】【Medium】Divide Two Integers的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. 【LeetCode每天一题】Divide Two Integers(两整数相除)

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  5. 【leetcode刷题笔记】Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 题解:要求不用乘除和取模运算实现两个数的除法. ...

  6. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  7. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  8. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  9. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

  10. 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters

    [Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...

随机推荐

  1. 750的设计图以rem为单位的移动设备响应的设置大小

    750的设计图,设置font-size: 125%;  1rem =20px;  大部分浏览器默认的字体大小为16px,谷歌默认字体大小为12px; 其他设备的fon-size的比列: 320/720 ...

  2. OnClick和OnClientClick

    OnClientClick是客户端事件处理方法,一般采用JavaScript来进行处理,也就是直接在IE端运行,一点击就运行 OnClick是服务器端事件处理方法,在服务器端也就是IIS中运行,点击后 ...

  3. eclipse相关问题汇总

    安装subclipse svn工具: 配置JavaHL:http://subclipse.tigris.org/wiki/JavaHL#head-5bf26515097c3231c1b04dfdb22 ...

  4. 使用Chrome console提取页面数据

    使用Chrome console提取页面数据 1.需求介绍 在做课题研究的过程中,遇到这样一个问题,有一个页面中包含很多IP地址,需要把这些IP地址提取出来保存到文件中.如下图所示: 一开始的做法是一 ...

  5. Learning Linux Commands: awk--reference

    http://how-to.linuxcareer.com/learning-linux-commands-awk 1. Introduction In this case, the title mi ...

  6. Windows加密API的层次

    Windows平台下的应用程序可以分为托管的.NET程序和本机的Win32(以及Win64)两大类..NET有着类似于JAVA的虚拟机和二进制码托管运行环境,提供了在不同Windows平台上的代码可携 ...

  7. golang基础---Slice切片

    切片Slice在go语言中是单独的类型(指向底层的数组),不同于python(对可迭代对象操作的工具),注意区分数组和slice的区别 定义一个空slice,格式var s []int,这种既没有长度 ...

  8. windows 下配置ndk环境,无需cygwin

    时隔好久要用ndk编译jni库,本以为配制安装cygwin环境,便按部就班的下载安装,但是公司的网速真的不给力,三天安装了三四次都没有安装成功(我选择的是在线安装),于是我便开始查ndk的官网看看,发 ...

  9. Windows Server 2008 R2系统上安装SQLServer2012集群(简略)

    4台服务器(1台AD.2台SQL服务器.1台iSCSI存储服务器) 9个IP(1个AD的IP.2个SQL服务器的IP.2个心跳IP.1个iSCSI存储服务器的IP.1个集群IP.1个DTC的IP.1个 ...

  10. access 2010,语文

    access 2010*(报表) 使用报表创建:打开需要创建图形的报表----创建----报表----完成. 使用报表向导创建:创建----报表向导----选择表/查询----选择字段----设置分布 ...