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 ...
随机推荐
- django_mysql_配置
配置 1. 安装Pymysql pip install PyMySQL 然后在项目同名_init__添加 from pymysql import install_as_MySQLdb install_ ...
- lua table长度解析
先来看lua table源码长度获取部分(ltable.c) j是数组部分的长度.首先判断数组长度大于0,并且数组最后一个是nil,就用二分法查找,返回长度. 如果t->node是 table的 ...
- HDU 5657 CA Loves Math 状压DP + 枚举
题意: 给出\(A(2 \leq A \leq 11), n(0 \leq n \leq 10^9), k(1 \leq k \leq 10^9)\). 求区间\([1, A^n]\)中各个数字互不相 ...
- 50、转自知乎上android开发相见恨晚的接口
原文链接:http://www.zhihu.com/question/33636939 程序员软件开发Android 开发JavaAndroid修改 Android开发中,有哪些让你觉得相 ...
- MFC定时关机程序的实现3-最小化到托盘栏
这个定时关机运行过后默认最小化到托盘栏最好了,不用每次都去点了. 现在来看看如何将程序显示在托盘栏. 首先在头文件里声明一个变量和一个消息响应函数 //最小化到托盘栏 //第一步,生成一个成员变量,或 ...
- PostgreSQL查看索引的使用情况
查看某个表的索引使用情况 select relname, indexrelname, idx_scan, idx_tup_read, idx_tup_fetch from pg_stat_user_i ...
- python IDLE简介及使用技巧
前言:本人环境windows 7 64位,python2.7 IDLE简介: 是python 的可视化GUI编辑器 可以逐行输入命令 可方便的进行复制.粘贴等操作 常用命令行命令: import mo ...
- DNS(转载)
最近帮朋友注册域名配置主机,碰到一些DNS上的一些概念,惭愧于有一些东西已经忘记是啥意思,于是决定重新学习一下DNS方面的基本概念. 常用概念: TTL: TTL为Time to live的缩写,网络 ...
- HTML5 新增绘图功能
<!DOCTYPE html> <html> <head lang="en"> <title></title> < ...
- BZOJ2806 [Ctsc2012]Cheat 【后缀自动机 + 二分 + 单调队列优化DP】
题目 输入格式 第一行两个整数N,M表示待检查的作文数量,和小强的标准作文库 的行数 接下来M行的01串,表示标准作文库 接下来N行的01串,表示N篇作文 输出格式 N行,每行一个整数,表示这篇作文的 ...