LeetCode OJ-- Divide Two Integers *
https://oj.leetcode.com/problems/divide-two-integers/
在不使用乘法、除法、求余的情况下计算除法。
使用减法计算,看看减几次。
刚开始寻思朴素的暴力做,然后超时了。
于是开始增大每次的被减数
但是溢出了。
2的32次方=4294967296(无符号),带符号再除以2,负数比正数多一个,-2147483648~+2147483647
所以int的范围就是 -2147483648~2147483648.
于是当输入中有 -2147483648的时候,对于 abs()函数返回结果就溢出了,求得后的值还是 -2147483648.
于是用 long long 来作为中间数据类型。并且不用abs()函数了。
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std; class Solution {
public:
int divide(int dividend, int divisor) { long long dividend_l = dividend;
long long divisor_l = divisor; bool positive = true;
if(dividend_l>&&divisor_l< || dividend_l< && divisor_l>)
positive = false; if(dividend_l<)
dividend_l = -dividend_l;
if(divisor_l<)
divisor_l = -divisor_l; if(dividend == || dividend_l< divisor_l)
return ;
if(dividend == divisor)
return ; int ans = ; while(dividend_l>=divisor_l)
{
ans += subDivide(dividend_l,divisor_l);
} if(positive == false)
ans = -ans; return ans;
}
int subDivide(long long ÷nd,long long divisor)
{
int timesSum = ;
long times = ;
long long _divisor = divisor;
while(_divisor> && dividend>=_divisor)
{
dividend = dividend - _divisor;
_divisor += _divisor;
timesSum += times;
times += times; //means _divisor is how much copies of divisor
}
return timesSum;
}
}; int main()
{
class Solution mys;
cout<<mys.divide(-,-);
}
LeetCode OJ-- Divide Two Integers *的更多相关文章
- [LeetCode] 29. Divide Two Integers 两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- Java for LeetCode 029 Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 【leetcode】Divide Two Integers (middle)☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- Java [leetcode 29]Divide Two Integers
题目描述: Divide two integers without using multiplication, division and mod operator. If it is overflow ...
- [leetcode]29. Divide Two Integers两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...
- [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆
转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...
- [leetcode]29. Divide Two Integers 两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [LeetCode] 29. Divide Two Integers ☆☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 【Leetcode】Divide Two Integers
Divide two integers without using multiplication, division and mod operator. class Solution { public ...
- [LeetCode]29. Divide Two Integers两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
随机推荐
- 并查集:HDU4496-D-City(倒用并查集)
D-City Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total Submis ...
- utf8和utf8mb4区别
原文链接 一.简介 MySQL在5.5.3之后增加了这个utf8mb4的编码,mb4就是most bytes 4的意思,专门用来兼容四字节的unicode.好在utf8mb4是utf8的超集,除了将编 ...
- 【java】实体类中 Set<对象> 按照对象的某个字段对set排序
Java利用hibernate进行一对多查询时,把另一张表作为一个属性存进这张表的字段中,返回的类型是set类型,要对返回的set类型进行排序 user表 package onlyfun.caterp ...
- Android Kotlin适用小函数
都是一些Android适用的Kotlin小函数. 1.点击空白隐藏键盘 //点击空白隐藏键盘 override fun onTouchEvent(event: MotionEvent): Boolea ...
- Sql日期时间格式转换(转 子夜.)
sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007-0 ...
- python基础——18(面向对象2+异常处理)
一.组合 自定义类的对象作为另一个类的属性. class Teacher: def __init__(self,name,age): self.name = name self.age = age t ...
- loj2051 「HNOI2016」序列
ref #include <algorithm> #include <iostream> #include <cstdio> #include <cmath& ...
- 【Combination Sum 】cpp
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...
- 【Combinations】cpp
题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...
- 几种常见的Android自动化测试框架及其应用
随着Android应用得越来越广,越来越多的公司推出了自己移动应用测试平台.例如,百度的MTC.东软易测云.Testin云测试平台…….由于自己所在项目组就是做终端测试工具的,故抽空了解了下几种常见的 ...