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. 学习react心得及总结

    注意学习这个在D盘:小红书第一部分的案例react/new-my-app 小红书第二部分的案例react/make-redux 小红书第三部分的案例react/my-app-higher 并且里面有说 ...

  2. Google Map API抓取地图坐标信息小程序

    因为实验室需要全国城市乡镇的地理坐标,有Execl的地名信息,需要一一查找地方的经纬度.Google Map地图实验室提供自带的查找经纬度的方法,不过需要一个点一个点的手输入,过于繁琐,所以自己利用G ...

  3. JDBC(1)-连接数据库

    主要步骤包括: 加载驱动: 连接数据库: 使用语句操作数据库: 关闭数据库连接,释放资源. 1.需要导包: 2.加载数据驱动: mysql驱动名:com.mysql.jdbc.Driver 加载方式: ...

  4. TCP/IP协议簇分层详解---转

    http://blog.csdn.net/hankscpp/article/details/8611229 一. TCP/IP 和 ISO/OSI ISO/OSI模型,即开放式通信系统互联参考模型(O ...

  5. Scrapy框架学习(二)Scrapy入门

    接下来以爬取quote.toscrape.com为例完成一遍Scrapy的抓取流程. 首先创建一个Scrapy项目.打开命令行,输入以下命令: scrapy startproject projectn ...

  6. Environment.Exit(0) 、Application.Exit() 、this.Close() 、this.Dispose()的区别

    Application.Exit:通知winform消息循环退出.程序会等待所有的前台线程终止后才能真正退出.是一种强行退出方式,就像 Win32 的 PostQuitMessage().它意味着放弃 ...

  7. windows下查看 mysql二进制日志文件

    有时候需要将linux中的mysql从线上linux种down到windows查看,但是这种binlog日志是二进制的,应该怎么查看呢? 使用window上的mysqlbinlog.exe将其转码到另 ...

  8. MySQL触发器基本使用

    文章参考:这里 MySQL中,创建触发器的基本语法: CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EA ...

  9. JMS - 基于JMS的RPC

    现在试试通过JMS,在应用程序之间发送消息.先看看spring提供的RPC方案(其实还有其他方案,只是没见过谁用).需要使用到这两个类:·org.springframework.jms.remotin ...

  10. [android] 天气app布局练习(二)

    主要练习一下GridView MainActivity.java package com.example.weatherreport; import java.util.ArrayList; impo ...