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. JAVA学习5:用Maven创建第一个web项目(2)servlet演示

    上一章用Maven新建了web项目成功后,本文演示在此基础上应用servlet. 1.首先修改pom.xml文件,添加servlet依赖   <project xmlns="http: ...

  2. how to run windows programs on a MAC?

    How to run windows programs on a MAC? We could use wine or Wine Bottler which is based on wine and p ...

  3. ADT安装Genymotion的eclipse插件安装及错误解决办法

    接触安卓开发也有很长一段时间了,但是一直使用的真机测试程序,因为感觉android模拟器实在是太不方便,运行慢,而且经常出错.最近听人介绍说Genymotion这款Android模拟器相当不错,于是打 ...

  4. Netflix Hystrix笔记

    maven引入 <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hyst ...

  5. C语言入门语法

    一.数据类型 常量 1.通过预处理声明常量 #include <stdio.h> #define PRICE 100 int main() { printf("价格:%d\n&q ...

  6. Scrum 冲刺博客第二篇

    一.当天站立式会议照片一张 二.每个人的工作 (有work item 的ID),并将其记录在码云项目管理中 昨天已完成的工作 配置和连接微信小程序服务器 个人界面设计 部主页界面设计 答题界面设计 今 ...

  7. SP16580 QTREE7 - Query on a tree VII

    Description 一棵树,每个点初始有个点权和颜色(0/1) 0 u :询问所有u,v 路径上的最大点权,要满足u,v 路径上所有点的颜色都相同 1 u :反转u 的颜色 2 u w :把u 的 ...

  8. Windows开启telnet命令

    1.点击开始 → 运行 → 输入telnet,回车. 2.点击启用或关闭Windows功能 3.找到Telnet客户端,勾选,点击确认 4.搞定,测试一下 打开CMD,在出来的DOS界面里,输入tel ...

  9. Tomcat源码(二):tomcat启动之前的初始化

    当tomcat启动的时候 首先会加载 org.apache.ctalina.startup.BootStrap类. 使用eclipse或idea启动tomcat其实就是在启动这个类的main方法 根据 ...

  10. zookeeper【2】集群管理

    Zookeeper 的核心是广播,这个机制保证了各个Server之间的同步.实现这个机制的协议叫做Zab协议. Zab协议有两种模式,它们分别是恢复模式(选主)和广播 模式(同步).当服务启动或者在领 ...