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

If it is overflow, return MAX_INT.

题解:

  思路就是被除数减去除数,减尽为止。优化的方法是尽量少的做减法。由于不能用乘法,可以利用位操作,左移一位即为该数乘上2

Solution 1

 class Solution {
public:
int divide(int dividend, int divisor) {
if (divisor == || (dividend == INT_MIN && divisor == -)) return INT_MAX;
long long m = abs((long long)dividend);
long long n = abs((long long)divisor); int sign = (dividend < ) ^ (divisor < ) ? - : ;
int res = ;
while (m >= n) {
long long tmp = n, p = ;
while (m >= (tmp << )) {
tmp <<= ;
p <<= ;
}
m -= tmp;
res += p;
}
return sign == ? res : -res;
}
};

Solution 2

 class Solution {
public:
int divide(int dividend, int divisor) {
if (!divisor || dividend == INT_MIN && divisor == -)
return INT_MAX;
int sign = ((dividend < ) ^ (divisor < )) ? - : ;
unsigned dvd = abs(dividend);
unsigned dvs = abs(divisor);
int res = ;
while (dvd >= dvs) {
unsigned temp = dvs, multiple = ;
// 不可写作 dvd >= (tmp << 1),因为有可能溢出
while (dvd - temp >= temp) {
temp <<= ;
multiple <<= ;
}
dvd -= temp;
res += multiple;
}
return sign * res;
}
};

【LeetCode】029. Divide Two Integers的更多相关文章

  1. 【LeetCode】29. Divide Two Integers

    题意:不用乘除求余运算,计算除法,溢出返回INT_MAX. 首先考虑边界条件,什么条件下会产生溢出?只有一种情况,即返回值为INT_MAX+1的时候. 不用乘除求余怎么做? 一.利用减法. 耗时太长, ...

  2. 【一天一道LeetCode】#29. Divide Two Integers

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

  3. 【leetcode】1296. Divide Array in Sets of K Consecutive Numbers

    题目如下: Given an array of integers nums and a positive integer k, find whether it's possible to divide ...

  4. 【LeetCode】Sum of Two Integers

    问题描述: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and - ...

  5. 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)

    [LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...

  6. 【leetcode】1291. Sequential Digits

    题目如下: An integer has sequential digits if and only if each digit in the number is one more than the ...

  7. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

  8. 【LeetCode】18、四数之和

    题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...

  9. 【LeetCode】15、三数之和为0

    题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...

随机推荐

  1. 一步步讲解如何开源自己的项目到GitHub上,Mac机示例

    如果你有自己的优秀项目,想要分享给大家,那GitHub会是你正确的选择.如何才能将自己的项目上传到GitHub上呢?接下来请一步一步跟着走. 需要准备的资源: 1.一台Mac机 2.安装git客户端( ...

  2. Capslock and Esc

    将Caps Lock转换成Esc(windows and linux) 1. linux 下将Caps Lock 转换成Esc 作为一个vimer,Caps Lock对我(还有其他很多人)来说根本就是 ...

  3. $《第一行代码:Android》读书笔记——第5章 Broadcast

    (一)广播机制简介 1.Android广播的分类: 如图所示: 2.发送广播:使用Intent:接收广播:Broadcast Receiver. (二)接收系统广播 1.动态注册监听网络变化 示例程序 ...

  4. java MD5Utils 加密工具类

    package com.sicdt.library.core.utils; import java.io.File; import java.io.FileInputStream; import ja ...

  5. Linux Shell基础 通配符

    通配符 在 Bash 中,如果需要模糊匹配文件名或目录名,就要用到通配符.下面为常用的通配符. 表 1 通配符 通配符 作 用 ? 匹配一个任意字符 * 匹配 0 个或任意多个任意字符,也就是可以匹配 ...

  6. NAS、SAN、DAS 说明

    NAS 说明 1.NAS(Network Attached Storage:网络附属存储) 2.NAS 是一种采用直接与网络介质相连的特殊设备实现数据存储的机制. 3.NAS本身能够支持多种协议(如N ...

  7. DNS 安装配置

    DNS 安装配置 实验环境 一台主机:Linux Centos 6.5 32位 安装包: DNS服务:bind.i686 DNS测试工具:bind-utils DNS 服务安装 1.yum安装DNS服 ...

  8. Python 列表List的定义及操作

    # 列表概念:有序的可变的元素集合 # 定义 # 直接定义 nums = [1,2,3,4,5] # 通过range函数构造,python2 和python3 版本之间的差异: # python3 用 ...

  9. gitLab 傻瓜式使用教程

    第一步,先去gitLab网上注册一下gitLab 进网站注册出来是这个界面: 2016082993103QQ20160829-1.png 然后进行人机验证(这个没啥困难的0.0) 2016082998 ...

  10. vs+mysql+ef配置方法

    这次的项目用的是MySQL数据库,但是ADO.NET实体数据模型默认是不支持MySQL数据库的,本文档将介绍如何让VS ADO.NET实体数据模型支持MySQL. 一.安装插件 1.VS插件 mysq ...