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:

  1. Input: dividend = 10, divisor = 3
  2. Output: 3

Example 2:

  1. Input: dividend = 7, divisor = -3
  2. 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.

题目

不准乘除,不准膜。

思路

那剩下的还能用加减、位运算

代码

  1. // Divide Two Integers
  2. // 时间复杂度O(logn),空间复杂度O(1)
  3. public class Solution {
  4. public int divide(int dividend, int divisor) {
  5. if(dividend == 0) return 0;
  6. if (divisor == 0) return Integer.MAX_VALUE;
  7.  
  8. // 当 dividend = INT_MIN,divisor = -1时,结果会溢出
  9. if (dividend == Integer.MIN_VALUE) {
  10. if (divisor == -1) return Integer.MAX_VALUE;
  11. else if (divisor < 0)
  12. return 1 + divide(dividend - divisor, divisor);
  13. else
  14. return - 1 + divide((dividend + divisor), divisor);
  15. }
  16. if(divisor == Integer.MIN_VALUE){
  17. return dividend == divisor ? 1 : 0;
  18. }
  19.  
  20. int a = dividend > 0 ? dividend : -dividend;
  21. int b = divisor > 0 ? divisor : -divisor;
  22.  
  23. int result = 0;
  24. while (a >= b) {
  25. int c = b;
  26. for (int i = 0; a >= c;) {
  27. a -= c;
  28. result += 1 << i;
  29. if (c < Integer.MAX_VALUE / 2) { // prevent overflow
  30. ++i;
  31. c <<= 1;
  32. }
  33. }
  34. }
  35.  
  36. return ((dividend^divisor) >> 31) != 0 ? (-result) : (result);
  37. }
  38. }

[leetcode]29. Divide Two Integers 两整数相除的更多相关文章

  1. [leetcode]29. Divide Two Integers两整数相除

      Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...

  2. [LeetCode] 29. Divide Two Integers 两数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  3. [LeetCode]29. Divide Two Integers两数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  4. 【LeetCode每天一题】Divide Two Integers(两整数相除)

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  5. [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆

    转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...

  6. [LeetCode] Divide Two Integers 两数相除

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  7. [LeetCode] 29. Divide Two Integers ☆☆

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  8. 029 Divide Two Integers 两数相除

    不使用乘号,除号和取模符号将两数相除.如果溢出返回 MAX_INT.详见:https://leetcode.com/problems/divide-two-integers/description/ ...

  9. [LintCode] Divide Two Integers 两数相除

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

随机推荐

  1. [UE4]C++ getter and setter

    问:以前面向对象没学好.... 最近老是在想,既然要设为private为什么还要写个setter来改变它的值呢? 为什么不直接把它直接设成public倒省事? 呵呵,谢啦 答:用setter来改变数据 ...

  2. Open Live writer 远程博客管理客户端

    1.  官网地址:http://openlivewriter.org/ 点击download下载:https://openlivewriter.azureedge.net/stable/Release ...

  3. 自己写的jQuery颜色插件

    界面效果: 插件js代码: ;(function ($) { //122种颜色 var aColors = [ "ff0000", "ffff00", &quo ...

  4. 腾讯优图联手Science发布主题报告:计算机视觉的研发和应用

    近日,腾讯优图与<科学>(Science)杂志共同发布<Seeing is believing: R&D applications of computer vision> ...

  5. Solr学习之二-Solr基础知识

    一 基本说明 简单来说Solr是基于Lucene的高性能的,开源的Java企业搜索服务器.Solr可以看作一个Web app,运行在tomcat或Jetty这类HTTP服务器上, 底层是一个基于Luc ...

  6. push(),pop(),unshift(),shift()

    1.push() 往数组末尾添加一个或多个元素,返回新的长度 2.pop() 删除数组末尾元素,数组长度减1,返回被删除的值 3.unshift() 往数组开头添加一个或多个元素,返回新的长度 4.s ...

  7. mysql 更新(-)初始mysql

    01-MySql的前戏   MySql的前戏 在学习Mysql之前,我们先来想一下一开始做的登录注册案例,当时我们把用户的信息保存到一个文件中: #用户名 |密码root|123321 alex|12 ...

  8. Wechall 部分WP

    前言: 开始打CTF,掌握一些新的姿势与知识. 这里我选择的平台是Wechall.这里从简单到难 WP部分: Training: Get SourcedAnswer: 查看网页源代码 Training ...

  9. 1.urlencoder和urldecoder的使用

    今天传url的时候乱码了.先说情形,url中有searchText=中文的情形,后台new String(searchText.getBytes(ISO-8859-1),"gbk" ...

  10. IIS 更新EXE文件

    IIS 更新EXE文件 MIME,add,文件扩展名带不带.都可以,会自动加上.的 文件扩展名:.exe MIME类型:application/octet-stream .ini文件