转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180

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

乘除取模都不让用。。那只有加减

思路解析:

判空,返回0
使用long类型的变量存储division和divisor的绝对值
如果除数小于被除数,返回0
使用加法完成除法,注意保存加了多少倍,使用嵌套循环,外层控制division大于divisor,里层控制倍数增加一倍是否比division要小
最后返回值的时候不要忘记注意正负号

public int divide(int dividend, int divisor) {
if (dividend == 0 || divisor == 0)
return 0;
boolean flag = (dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0);
long a = Math.abs((long) dividend);// 注意要转换成long型,防止溢出
long b = Math.abs((long) divisor);
if (a < b)
return 0;
int result = 0;
while (a >= b) {
long tempSum = b;
long divSum = 1;
while (tempSum + tempSum <= a) {
tempSum += tempSum;
divSum += divSum;
}
a -= tempSum;
result += divSum;
}
return flag == true ? -result : result;
}

[LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. Java [leetcode 29]Divide Two Integers

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

  7. LeetCode 29 Divide Two Integers (不使用乘法,除法,求模计算两个数的除法)

    题目链接: https://leetcode.com/problems/divide-two-integers/?tab=Description   Problem :不使用乘法,除法,求模计算两个数 ...

  8. LeetCode: 29. Divide Two Integers (Medium)

    1. 原题链接 https://leetcode.com/problems/divide-two-integers/description/ 2. 题目要求 给出被除数dividend和除数divis ...

  9. [leetcode] 29. divide two integers

    这道题目一直不会做,因为要考虑的corner case 太多. 1. divisor equals 0. 2. dividend equals 0. 3. Is the result negative ...

随机推荐

  1. P4555 [国家集训队]最长双回文串

    P4555 [国家集训队]最长双回文串 manacher 用manacher在处理时顺便把以某点开头/结尾的最长回文串的长度也处理掉. 然后枚举. #include<iostream> # ...

  2. 20145206邹京儒MSF基础应用

    20145206邹京儒MSF基础应用 一.MS08_067漏洞渗透攻击实践 实验前准备 1.两台虚拟机,其中一台为kali,一台为windows xp sp3(英文版). 2.在VMware中设置两台 ...

  3. TensorFlow入门(三)多层 CNNs 实现 mnist分类

    欢迎转载,但请务必注明原文出处及作者信息. 深入MNIST refer: http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/mni ...

  4. JAVA I/O(二)文件NIO

    一.Unix五种I/O模型 读取和写入文件I/O操作都是调用操作系统提高的接口,对磁盘I/O来说,一般是将数据从磁盘拷贝到内核空间,然后从内核空间拷贝到用户空间.为了减小I/O时间,一般内核空间存在高 ...

  5. HTTP If-Modified-Since引发的浏览器缓存汇总

    在看Spring中HttpServlet的Service方法时,对于GET请求,代码逻辑如下: if (method.equals(METHOD_GET)) { long lastModified = ...

  6. Linux进程间通信--使用信号量【转】

    本文转载自:http://blog.csdn.net/ljianhui/article/details/10243617 这篇文章将讲述别一种进程间通信的机制——信号量.注意请不要把它与之前所说的信号 ...

  7. dp专题训练

    ****************************************************************************************** 动态规划 专题训练 ...

  8. MVC ---- EF三层代码

    1.DAL层 using Night.Models; using System; using System.Collections.Generic; using System.Data.Entity. ...

  9. go 语言字典元素删除

    package main import "fmt" func main() { /* 创建map */ countryCapitalMap := map[string]string ...

  10. Ubuntu 关闭防火墙

    关闭防火墙:service iptables stop