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 ...
随机推荐
- 查询集 QuerySet和管理器Manager
查询集 QuerySet 查询集,也称查询结果集.QuerySet,表示从数据库中获取的对象集合. 当调用如下过滤器方法时,Django会返回查询集(而不是简单的列表): all():返回所有数据. ...
- python3.7 倒计时
#!/usr/bin/env python __author__ = "lrtao2010" # python3.7 倒计时 import time for i in range( ...
- Linux命令之---cd
命令简介 Linux cd 命令是Linux中最基本的命令语句,其他的命令语句要进行操作,都是建立在使用 cd 命令上的. 命令格式 cd [目录名] 命令功能 切换当前目录至dirName 常用范例 ...
- 1022: [SHOI2008]小约翰的游戏John9(Auti_SG)
1022: [SHOI2008]小约翰的游戏John Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 3150 Solved: 2013[Submit] ...
- Redis实现之字典跳跃表
跳跃表 跳跃表是一种有序数据结构,它通过在每个节点中维持多个指向其他节点的指针,从而达到快速访问节点的目的.跳跃表支持平均O(logN).最坏O(N)的时间复杂度查找,还可以通过顺序性操作来批量处理节 ...
- cakephp中使用 find('count')方法
对于find('count',array('group'=>'user_id')); Model.php中这样描述: /** * Handles the before/after filter ...
- 二叉树遍历(Java实现)
二叉树遍历(Java实现) 主要是二叉树的遍历,包括递归遍历和非递归遍历 import java.util.ArrayDeque; import java.util.ArrayList; impo ...
- IOS开发---菜鸟学习之路--(十二)-利用ASIHTTPRequest进行异步获取数据
想要实现异步获取的话我这边了解过来有两个非常简单的方式 一个是利用ASIHTTPRequest来实现异步获取数据 另一个则是利用MBProgressHUD来实现异步获取数据 本章就先来讲解如何利用AS ...
- MD5碰撞
if ( $_POST['param1'] !==$_POST['param2'] && md5($_POST['param1']) === md5($_POST['param2']) ...
- apizza导出为html后,从中提取api_name/api_path/api_method,保存到本地,方便根据接口名称得到接口路径与请求方法
import re import os def open_file(file='c:/newcrm.html'): f=open(file,'r',encoding='utf-8') return f ...