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. SPRING中的线程池ThreadPoolTaskExecutor(转)

    转自:https://blog.csdn.net/zhanglongfei_test/article/details/51888433 一.初始化 1,直接调用 ThreadPoolTaskExecu ...

  2. (转)mysql帮助命令使用说明

    https://www.ilanni.com/?p=8157------- 烂泥:mysql帮助命令使用说明

  3. SQL Cookbook—查询、排序

    涉及到的问题1.在select语句中使用条件逻辑2.限制返回的行数3.从表中随机返回n条记录4.将空值转换为实际值5.对字母和数字混合的数据排序6.处理排序空值7.根据数据项的键排序–8.从一个表中查 ...

  4. NMAP - A Stealth Port Scanner--reference

    http://nmap.org/bennieston-tutorial/ 实例:nmap -sP 192.168.21.* Contents 1  Introduction Nmap is a fre ...

  5. [PY3]——创建多值映射字典?/defaultdict默认字典/setdefault()

    Defaultdict 默认字典 collections 模块中的 defaultdict(默认字典),可以用来构造“一个键映射多个值”这样的字典 如果你想保持元素的插入顺序就应该使用list, 如果 ...

  6. Codeforces 639B——Bear and Forgotten Tree 3——————【构造、树】

    Bear and Forgotten Tree 3 time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  7. HZAU 21——Arithmetic Sequence——————【暴力 or dp】

    Arithmetic Sequence Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1810  Solved: 311[Submit][Status] ...

  8. 【idea快捷键】

    IntelliJ Idea 常用快捷键列表 idea也是可以切换到eclipse风格的快捷键方式的  在keymap中切换即可 Ctrl+Shift + Enter,语句完成 “!”,否定完成,输入表 ...

  9. 分布式事务-Sharding 数据库分库分表

      Sharding (转)大型互联网站解决海量数据的常见策略 - - ITeye技术网站 阿里巴巴Cobar架构设计与实践 - 机械机电 - 道客巴巴 阿里分布式数据库服务原理与实践:沈询_文档下载 ...

  10. SQL 工具系列一

    1.误删除数据恢复篇 ApexSQL Recover   可以恢复Delete Truncate  drop,恢复 二进制大型对象 测试版本  每10行才会恢复 评估版本下载地址:只能用14天 所以基 ...