【LeetCode】029. Divide Two Integers
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的更多相关文章
- 【LeetCode】29. Divide Two Integers
题意:不用乘除求余运算,计算除法,溢出返回INT_MAX. 首先考虑边界条件,什么条件下会产生溢出?只有一种情况,即返回值为INT_MAX+1的时候. 不用乘除求余怎么做? 一.利用减法. 耗时太长, ...
- 【一天一道LeetCode】#29. Divide Two Integers
一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...
- 【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 ...
- 【LeetCode】Sum of Two Integers
问题描述: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and - ...
- 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)
[LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...
- 【leetcode】1291. Sequential Digits
题目如下: An integer has sequential digits if and only if each digit in the number is one more than the ...
- 【LeetCode】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- 【LeetCode】18、四数之和
题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...
- 【LeetCode】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
随机推荐
- python tkinter组件学习
http://blog.csdn.net/pfm685757/article/details/50162567
- Android系统源代码的下载与编译
http://www.jianshu.com/p/aeaceda41798 目录 1.简介 2.官方同步源代码 3.镜像同步源代码 4.已有源代码更新 5.编译源代码 5.1编译Android 4.1 ...
- 解决:Requested 'libdrm_radeon >= 2.4.56' but version of libdrm_radeon is 2.4.52
checking for NOUVEAU... yes checking for RADEON... no configure: error: Package requirements (libdrm ...
- 嵌入式C函数优化
0. 引言 这是一个简单函数的优化,但却体现了代码易读性和效率的综合考虑. 如果问我如何写出优秀的代码,答曰:再写一版. 1. 版本1 从环形buffer中取出数据,然后放到一个结构体中.buffer ...
- Docker容器技术-第一个容器
一.第一个容器 1.Docker版本 A.community-edition社区版 Docker CE是免费的Docker产品的新名称,Docker CE包含了完整的Docker平台,非常适合开发人员 ...
- AngularJs 的一则错误 [$INJECTOR:MODULERR]
Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to: Error: [$injector:modu ...
- Python3.4 用 pip 安装lxml时出现 “Unable to find vcvarsall.bat ”
我的python版本是Python 3.5 该问题的产生是在windows环境中,python 的 Setup需要调用一个vcvarsall.bat的文件,该文件需要安装c++编程环境才会有.网上的方 ...
- iOS_多线程(二)
上篇中我们分享了NSThread.NSOperation&NSOperationQueue如何实现多线程,今天我们来看下第三种实现多线程的方式:GCD(Grand Central Dispat ...
- 1.linux源码安装nginx
从官网下载nginx.tar.gz源码包 拷贝至Linux系统下进行解压 tar -zxvf nginx.tar.gz 进入解压后的目录,需要./configure,此步骤会报多个错,比如没有安装gc ...
- 关于CKEDITOR的一些小问题
<textarea name="tMessage" ></textarea> <script type="text/javascript& ...