【Leetcode】【Medium】Divide Two Integers
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的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- 【LeetCode每天一题】Divide Two Integers(两整数相除)
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- 【leetcode刷题笔记】Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 题解:要求不用乘除和取模运算实现两个数的除法. ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【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 ...
- 【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 ...
- 【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 ...
- 【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 ...
随机推荐
- SuperMap iClient for JavaScript 之关联查询
人们常说,计划赶不上变化.同样的,在项目中,使用的数据也是在不断变化的,尤其是属性信息的改变.就比如说,地图上的地物,它的空间信息在比较长的时间内,都不会发生变化,他的属性信息在初期不完整或者与后来的 ...
- 带有权重的服务器SLB的实现
1)参考了网络上的算法,但是那个算法仅仅是用于展示“权重轮循”的意图,在真正的网络下,因为是并行的,所以不可能单纯一个简单的循环可以解决问题. 2)用lock的话性能显然有损失. 3)想了一阵,结合C ...
- unity对敏感词库处理的记录
string txt = Resources.Load<TextAsset>("minganci").ToString(); //string txt = " ...
- D3介绍
D3介绍 D3是用来做web页面可视化的组件,其官方网址为http://d3js.org 安装 D3类库的文件只有一个d3.js.下载后直接在html的<script>标签中引用此js就可 ...
- vi 中插入当前时间
声明 笔者最近意外的发现 笔者的个人网站http://tiankonguse.com/ 的很多文章被其它网站转载,但是转载时未声明文章来源或参考自 http://tiankonguse.com/ 网站 ...
- JavaScript设计模式系列学习笔记目录
说明 本系列笔记参考书籍<JavaScript设计模式>.<JavaScript高级程序设计3> 参考博客:汤姆大叔博客:http://www.cnblogs.com/TomX ...
- SSIS教程:创建简单的ETL包 -- 2. 添加循环(Adding Looping)
在第 1 课:创建项目和基本包中,创建了从单个平面文件源中提取数据的包,然后使用查找转换功能对数据进行了转换,最后将数据加载到AdventureWorksDW2012 示例数据库的 FactCurre ...
- ASP.NET MVC4 新手入门教程之三 ---3.添加视图
在这一节你要修改HelloWorldController类要使用的视图模板文件来干净封装生成 HTML 响应到客户端的过程. 您将创建一个使用Razor 视图引擎介绍 ASP.NET MVC 3 的视 ...
- GitHub Desktop使用
1创建仓库 创建文件并提交2~3次 上传到github上,并创建仓库 就可以看到本地记录同步到github上了 回退上一版本(目前只懂一次一次回退) 回退到最原始 文件都删除 暂时记录
- 2017年11月27日 C#MDI窗体创建&记事本打印&记事本查找、自动换行
MDI窗体第一个父窗体 把属性里的IsMdiContainer设置为true就可以了 父窗体连接子窗体 //创建一个新的类,用来连接别的窗体,并且别的窗体为唯一窗体 List<Form> ...