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 ...
随机推荐
- 使用shell脚本添加用户
该文演示如何使用shell脚本完成添加用户,首先进行一个判断,如果用户存在,提示该用户已经存在,否则进行添加新的用户. 示例代码如下: #!/bin/bash grep_user() { R=`gre ...
- 【转载】MQTT的学习之Mosquitto集群搭建
本文出自:http://www.cnblogs.com/yinyi521/p/6087215.html 文章钢要: 1.进行双服务器搭建 2.进行多服务器搭建 一.Mosquitto的分布式集群部署 ...
- Pychram基本操作
1. 更改pychram页面为黑色背景主题.更改主题: File ->Settings -> Editor -> Color Scheme -> Scheme -> Mo ...
- Essential C++ 3.1 节的代码练习——哨兵方式
#include "IncrementArray.hpp" template <typename element> element *find_address(elem ...
- TI C6000优化手册——让代码看起来像钉子
DSP芯片的出现,是为了解决大量的数字运算问题.通过集成专用的加法器.乘法器.地址产生器.复杂逻辑等硬件单元,DSP能实现比普通单片机更快速的数字运算,使处理器更适用于实时性高.复杂度强的处理场合.也 ...
- Maya
建立酒杯的方法(CV曲线) surface(曲面)-- creat cv curve tool-- control vertex(调整图形)[再次creat cv建立厚度,只需要建立酒杯的上口]--- ...
- 8 django 里面的API
1.什么是API? 2.在djang里面写API 3.API实战效果 1.移动端的网页 4.restframework :老师方法 (0)安装 Django REST framework 是一个强大且 ...
- Python中__str__和__repr__的区别
Python有一个内置的函数叫repr,它能把一个对象用字符串的形式表达出来以便辨认,这就是“字符串表示形式”.repr就是通过__repr__这个特殊方法来得到一个对象的字符串表示形式.如果没有实现 ...
- springboot(七):springboot+mybatis多数据源最简解决方案
说起多数据源,一般都来解决那些问题呢,主从模式或者业务比较复杂需要连接不同的分库来支持业务.我们项目是后者的模式,网上找了很多,大都是根据jpa来做多数据源解决方案,要不就是老的spring多数据源解 ...
- Azure Active Directory Connect是如何协助管理员工作的?
TechTarget中国原创] 应用基于云的Microsoft Azure Active Directory,管理员们可以将本地Active Directory整合到Windows Server中.通 ...