[leetcode]29. Divide Two Integers 两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator.
Return the quotient after dividing dividend by divisor.
The integer division should truncate toward zero.
Example 1:
Input: dividend = 10, divisor = 3
Output: 3
Example 2:
Input: dividend = 7, divisor = -3
Output: -2
Note:
- Both dividend and divisor will be 32-bit signed integers.
- The divisor will never be 0.
- Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231− 1]. For the purpose of this problem, assume that your function returns 231 − 1 when the division result overflows.
题目
不准乘除,不准膜。
思路
那剩下的还能用加减、位运算
代码
// Divide Two Integers
// 时间复杂度O(logn),空间复杂度O(1)
public class Solution {
public int divide(int dividend, int divisor) {
if(dividend == 0) return 0;
if (divisor == 0) return Integer.MAX_VALUE; // 当 dividend = INT_MIN,divisor = -1时,结果会溢出
if (dividend == Integer.MIN_VALUE) {
if (divisor == -1) return Integer.MAX_VALUE;
else if (divisor < 0)
return 1 + divide(dividend - divisor, divisor);
else
return - 1 + divide((dividend + divisor), divisor);
}
if(divisor == Integer.MIN_VALUE){
return dividend == divisor ? 1 : 0;
} int a = dividend > 0 ? dividend : -dividend;
int b = divisor > 0 ? divisor : -divisor; int result = 0;
while (a >= b) {
int c = b;
for (int i = 0; a >= c;) {
a -= c;
result += 1 << i;
if (c < Integer.MAX_VALUE / 2) { // prevent overflow
++i;
c <<= 1;
}
}
} return ((dividend^divisor) >> 31) != 0 ? (-result) : (result);
}
}
[leetcode]29. Divide Two Integers 两整数相除的更多相关文章
- [leetcode]29. Divide Two Integers两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...
- [LeetCode] 29. Divide Two Integers 两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [LeetCode]29. Divide Two Integers两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- 【LeetCode每天一题】Divide Two Integers(两整数相除)
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆
转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- [LeetCode] 29. Divide Two Integers ☆☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 029 Divide Two Integers 两数相除
不使用乘号,除号和取模符号将两数相除.如果溢出返回 MAX_INT.详见:https://leetcode.com/problems/divide-two-integers/description/ ...
- [LintCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
随机推荐
- numpy的shuffle函数
import numpy as np from numpy.random import shuffle import pandas as pd df = pd.DataFrame([[1,2,3],[ ...
- PHP mysqli_autocommit() 函数
定义和用法 mysqli_autocommit() 函数开启或关闭自动提交数据库修改. 提示:请查看 mysqli_commit() 函数,用于提交指定数据库连接的当前事务.请查看 mysqli_ro ...
- php中的释放语句unset和释放函数mysql_free_result()
首先要强调的一点是unset在php中已经不再是一个函数了,既然不是函数,那么就没有了返回值,所以用的时候不能够用unset的返回值来做判断. 其次,在函数中,unset只能销毁局部变量,并不能销毁全 ...
- Spark Streaming 例子
NetworkWordCount.scala /* * Licensed to the Apache Software Foundation (ASF) under one or more * con ...
- 编译器错误消息: CS0016: 未能写入输出文件“c:/Windows/Microsoft.NET/Framework/v4.0.50727/Temporary ASP.NET Files/root .... 拒绝访问。
此问题困扰良久,得终极解决方案 环境:windows 2008 server r2 + iis7 + .net framework4.5 解决:1. 错误信息中包含的目录“c:/Windows/Mic ...
- selenium+python自动化84-chrome手机wap模式(登录淘宝页面)
前言 chrome手机wap模式登录淘宝页面,点击验证码无效问题解决. 切换到wap模式,使用TouchActions模块用tap方法触摸 我的环境 chrome 62 chromedriver 2. ...
- linux实时流量监控
在类Unix系统中可以使用top查看系统资源.进程.内存占用等信息.查看网络状态可以使用netstat.nmap等工具.若要查看实时的网络流量,监控TCP/IP连接等,则可以使用iftop. 一.if ...
- 0_Simple__vectorAdd + 0_Simple__vectorAdd_nvrtc + 0_Simple__vectorAddDrv
▶ 使用 CUDA Runtime API,运行时编译,Driver API 三种接口计算向量加法 ▶ 源代码,CUDA Runtime API #include <stdio.h> #i ...
- Java的Synchronized
原理,注意看输入输出,不了解原理是想不到会这样输出的. http://www.cnblogs.com/paddix/p/5367116.html 还有一个要注意的是一个对象一个monitor类
- leetcode342
public class Solution { public bool IsPowerOfFour(int num) { ) && ((num & (num - )) == ) ...