[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 ...
随机推荐
- [UE4]C++ getter and setter
问:以前面向对象没学好.... 最近老是在想,既然要设为private为什么还要写个setter来改变它的值呢? 为什么不直接把它直接设成public倒省事? 呵呵,谢啦 答:用setter来改变数据 ...
- Open Live writer 远程博客管理客户端
1. 官网地址:http://openlivewriter.org/ 点击download下载:https://openlivewriter.azureedge.net/stable/Release ...
- 自己写的jQuery颜色插件
界面效果: 插件js代码: ;(function ($) { //122种颜色 var aColors = [ "ff0000", "ffff00", &quo ...
- 腾讯优图联手Science发布主题报告:计算机视觉的研发和应用
近日,腾讯优图与<科学>(Science)杂志共同发布<Seeing is believing: R&D applications of computer vision> ...
- Solr学习之二-Solr基础知识
一 基本说明 简单来说Solr是基于Lucene的高性能的,开源的Java企业搜索服务器.Solr可以看作一个Web app,运行在tomcat或Jetty这类HTTP服务器上, 底层是一个基于Luc ...
- push(),pop(),unshift(),shift()
1.push() 往数组末尾添加一个或多个元素,返回新的长度 2.pop() 删除数组末尾元素,数组长度减1,返回被删除的值 3.unshift() 往数组开头添加一个或多个元素,返回新的长度 4.s ...
- mysql 更新(-)初始mysql
01-MySql的前戏 MySql的前戏 在学习Mysql之前,我们先来想一下一开始做的登录注册案例,当时我们把用户的信息保存到一个文件中: #用户名 |密码root|123321 alex|12 ...
- Wechall 部分WP
前言: 开始打CTF,掌握一些新的姿势与知识. 这里我选择的平台是Wechall.这里从简单到难 WP部分: Training: Get SourcedAnswer: 查看网页源代码 Training ...
- 1.urlencoder和urldecoder的使用
今天传url的时候乱码了.先说情形,url中有searchText=中文的情形,后台new String(searchText.getBytes(ISO-8859-1),"gbk" ...
- IIS 更新EXE文件
IIS 更新EXE文件 MIME,add,文件扩展名带不带.都可以,会自动加上.的 文件扩展名:.exe MIME类型:application/octet-stream .ini文件