Divide two integers without using multiplication, division and mod operator.

If it is overflow, return MAX_INT.

解题思路:

既然不呢个用乘除和取模运算,只好采用移位运算,可以通过设置一个length代表divisor向左的做大移位数,直到大于dividend,然后对length做一个循环递减,dividend如果大于divisor即进行减法运算,同时result加上对应的值,注意边界条件,JAVA实现如下:

static public int divide(int dividend, int divisor) {
if (divisor == 0)
return Integer.MAX_VALUE;
boolean isNeg = dividend < 0;
if (divisor < 0)
isNeg = !isNeg;
int length = 0;
long longdividend = Math.abs((long) dividend);
long longdivisor = Math.abs((long) divisor), result = 0;
while (longdividend >= longdivisor) {
longdivisor = longdivisor << 1;
length++;
}
while (length >= 0) {
if (longdividend >= longdivisor) {
result += Math.pow(2, length);
longdividend -= longdivisor;
}
longdivisor = longdivisor >> 1;
length--;
}
if (result > Integer.MAX_VALUE && !isNeg)
result = (long) Integer.MAX_VALUE;
return isNeg ? -(int) result : (int) result;
}

Java for LeetCode 029 Divide Two Integers的更多相关文章

  1. LeetCode 029 Divide Two Integers

    题目要求:Divide Two Integers Divide two integers without using multiplication, division and mod operator ...

  2. Java [leetcode 29]Divide Two Integers

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

  3. 【LeetCode】029. Divide Two Integers

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

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

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

  5. 【leetcode】Divide Two Integers (middle)☆

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

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

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

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

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

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

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

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

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

随机推荐

  1. 【Matplotlib】图例分开显示

    作图时图例往往都会出现一个图例框内,如果需要不同类型的图例分别显示,比如显示两个图例. 基本上,出现两个图例的话,需要调用两次 legend .第一次调用,你需要将图例保存到一个变量中,然后保存下来. ...

  2. Hive 正则匹配函数 regexp_extract

    regexp_extract 语法:    regexp_extract(string subject,  string pattern,  int index) 返回值: string 说明:  将 ...

  3. 常用sql,在做项目时用mysqlWorkBeach里面自动生成的

    -- 修改表中的字段的长度ALTER TABLE `sfkbbs`.`sfk_father_module` CHANGE ) NULL DEFAULT NULL COMMENT '父板块名字' ; 在 ...

  4. bzoj1069 SCOI2007 最大土地面积

    1069: [SCOI2007]最大土地面积 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 2560  Solved: 983 Description ...

  5. 安装hadoop2.4.0遇到的问题

    一.执行start-dfs.sh后,datenode没有启动 查看日志如下: 2014-06-18 20:34:59,622 FATAL org.apache.hadoop.hdfs.server.d ...

  6. php 获取后缀的几种方法

    1: function get_extension($file){ substr(strrchr($file, '.'), 1); } 2: function get_extension($file) ...

  7. Struts2几种传值

    1.url向action传值 url为 http://localhost/txyl/teacher_info?method:teacher_info&teacher_seq=dedafdsf3 ...

  8. 详细解析Java中抽象类和接口的区别

    在Java语言中, abstract class 和interface 是支持抽象类定 义的两种机制.正是由于这两种机制的存在,才赋予了Java强大的 面向对象能力.abstract class和in ...

  9. B/S C/S架构的界面测试

    网站是B/S架构的典型,从做网站的有限经验来整理一下B/S测试的基本要点,并把它与C/S进行区分. 与C/S相比,以下4个测试是除了常用测试外还要注意的: (1)链接测试 (2)表单测试 (3)脚本测 ...

  10. linux 的终端字体色和背景色的修改方法(一)

    更改Linux系统终端的颜色主题 随着Linux系统在服务器端的崛起,Linux也在慢慢进军个人桌面系统领域.如果在使用Linux系统的终端时,对其颜色主题不是很满意,该怎么修改颜色的主题呢?今天笔者 ...