Question

29. Divide Two Integers

Solution

题目大意:给定两个数字,求出它们的商,要求不能使用乘法、除法以及求余操作。

思路:说下用移位实现的方法

  1. 7/3=27是被除数,3是除数
  2. 除数左移,假设移动了n次后,移到最接近被除数,这时被除数=被除数-除数,商的一部分为2^n
  3. 如果被除数>除数,则继续循环
  4. 除数左移,又移动了m次后,移到最接近被除数,这时被除数=被除数-除数,商的一部分为2^m
  5. 最后商为2^n+2^m+...

Java实现:

法1:如果可以用除法,一步就可以了

  1. public int divide2(int dividend, int divisor) {
  2. // overflows
  3. if (dividend == Integer.MIN_VALUE && divisor == -1) return Integer.MAX_VALUE;
  4. // 给定两个数字,求出它们的商,要求不能使用乘法、除法以及求余操作。
  5. return dividend / divisor;
  6. }

法2:下面是用减法实现的,执行超时

  1. public int divide2(int dividend, int divisor) {
  2. // overflows
  3. if (dividend == Integer.MIN_VALUE && divisor == -1) return Integer.MAX_VALUE;
  4. int ans = 0;
  5. boolean negative = !((dividend > 0 && divisor > 0) || (dividend < 0 && divisor < 0));
  6. dividend = Math.abs(dividend);
  7. divisor = Math.abs(divisor);
  8. dividend -= divisor;
  9. while (dividend >= 0) {
  10. ans++;
  11. dividend -= divisor;
  12. }
  13. return negative ? -ans : ans;
  14. }

法3:用移位实现

  1. public int divide(int dividend, int divisor) {
  2. // 防止溢出
  3. if (dividend == Integer.MIN_VALUE && divisor == -1) return Integer.MAX_VALUE;
  4. // 获取最终结果的符号
  5. int sign = ((dividend < 0) ^ (divisor < 0)) ? -1 : 1;
  6. long dvd = Math.abs((long) dividend);
  7. long dvs = Math.abs((long) divisor);
  8. int ans = 0;
  9. while (dvd >= dvs) {
  10. long tmp = dvs, multiple = 1;
  11. while (dvd >= (tmp << 1)) {
  12. tmp <<= 1;
  13. multiple <<= 1;
  14. }
  15. dvd -= tmp;
  16. ans += multiple;
  17. }
  18. return sign == 1 ? ans : -ans;
  19. }

29. Divide Two Integers - LeetCode的更多相关文章

  1. [Leetcode][Python]29: Divide Two Integers

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...

  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

    一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...

  4. Java [leetcode 29]Divide Two Integers

    题目描述: Divide two integers without using multiplication, division and mod operator. If it is overflow ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 前馈控制+PID

    参考来源: 北京交通大学 硕士学位论文 基于脉冲串控制的含位置反馈和前馈补偿的位置控制算法的研究  赵旺升

  2. 《基于.NET Core构建微服务》系列文章(更新至第6篇,最新第7篇,已发布主页候选区)

    原文:Building Microservices On .NET Core – Part 1 The Plan 时间:2019年1月14日 作者:Wojciech Suwała, Head Arch ...

  3. 记离线缓存(manifest)一大坑,断定其只适用于静态网站或离线应用

    今天看了离线缓存(manifest)方面的资料,兴冲冲地就想给自己的网站用上.待我把代码都写好部署上服务器,并测试过OK的时候,在SegmentFault刷了一把manifest方面的问答,才发现这个 ...

  4. 用jq实现移动端滑动轮播以及定时轮播效果

    Html的代码: <div class="carousel_img"> <div class="car_img" style="ba ...

  5. 利用Charles做代理测试电脑上写的H5页面

    做H5页面的同学可能经常会遇到一个场景,就是电脑上调试好的页面怎么在手机上访问测试呢? 下面就介绍一种自己经常使用的方式,利用Charles代理软件来实现! 安装Charles 直接去官网下载对应的系 ...

  6. Unknown host mirrors.opencas.cn You may need to adjust the proxy settings in Gradle 报错及解决办法

    亲测Unknown host mirrors.opencas.cn You may need to adjust the proxy settings in Gradle 解决办法 - 程序员大本营 ...

  7. mysql find_in_set在oracle下的解决方案

    比如一张表: artile (id,type,content); type:1表示文艺类,2表示小说类,3表示传记,4表示传说,等等5,6,7,8 表数据: id type content 1 3,1 ...

  8. re模块补充与其他模块介绍

    注:昨日写了re单个模块几个重要的点需要补充 一.re模块补充 1.findall独有的优先级别展示 res = re.findall('abc', 'abcabcabcabc') print(res ...

  9. (更新中)"华为杯" 武汉大学21级新生程序设计竞赛 非官方题解

    "华为杯" 武汉大学21级新生程序设计竞赛 https://ac.nowcoder.com/acm/contest/31620#question D.和谐之树 思路:披着线段树外皮 ...

  10. 玩转LiteOS组件:玩转Librws

    摘要:Librws是一个跨平台的websocket客户端,使用C语言编写. 本文分享自华为云社区<LiteOS组件尝鲜-玩转Librws>,作者: W922 . 本期小编为大家带来Lite ...