LeetCode OJ:Divide Two Integers(两数相除)
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
两数相除,不能用*,/,% 那么只能用位移运算了,注意边界条件防止溢出,代码如下:
 class Solution {
 public:
     int divide(int dividend, int divisor) {
         int sign1 = dividend >  ?  : -;
         int sign2 = divisor >  ?  : -;
         long long d1 = abs(static_cast<long long>(dividend));
         long long d2 = abs(static_cast<long long>(divisor));
         int res = ;
         while(d1 >= d2){
             long long tmp = d2;
             for(int i = ; d1 >= tmp; ++i){
                 d1 -= tmp;
                 tmp <<= ;
                 res += 1LL << i;
             }
         }
         if(sign1 == sign2){
             if(res == INT_MIN)
                 return INT_MAX;
             return res;
         }else
             return res * -;
     }
 };
PS:这题溢出情况相当恶心。
LeetCode OJ:Divide Two Integers(两数相除)的更多相关文章
- [LeetCode] 29. Divide Two Integers 两数相除
		Given two integers dividend and divisor, divide two integers without using multiplication, division ... 
- [LeetCode]29. Divide Two Integers两数相除
		Given two integers dividend and divisor, divide two integers without using multiplication, division ... 
- [LeetCode] Divide Two Integers 两数相除
		Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ... 
- 029 Divide Two Integers 两数相除
		不使用乘号,除号和取模符号将两数相除.如果溢出返回 MAX_INT.详见:https://leetcode.com/problems/divide-two-integers/description/ ... 
- [LintCode] Divide Two Integers 两数相除
		Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ... 
- [leetcode]29. Divide Two Integers两整数相除
		Given two integers dividend and divisor, divide two integers without using multiplication, divisio ... 
- [leetcode]29. Divide Two Integers 两整数相除
		Given two integers dividend and divisor, divide two integers without using multiplication, division ... 
- LeetCode(29):  两数相除
		Medium! 题目描述: 给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符. 返回被除数 dividend 除以除数 divisor ... 
- 【LeetCode每天一题】Divide Two Integers(两整数相除)
		Given two integers dividend and divisor, divide two integers without using multiplication, division ... 
- [LeetCode] Sum of Two Integers 两数之和
		Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ... 
随机推荐
- Android图片加载框架Picasso最全使用教程2
			前言 前面我们已经介绍了Picasso的基本用法及如何将一张图片加载到ImageView中,下面我们就利用Picasso在ListView中加载图片;Let’s Go! 一个ListView的简单应用 ... 
- 《Redis官方文档》用Redis构建分布式锁
			用Redis构建分布式锁 在不同进程需要互斥地访问共享资源时,分布式锁是一种非常有用的技术手段. 有很多三方库和文章描述如何用Redis实现一个分布式锁管理器,但是这些库实现的方式差别很大,而且很多简 ... 
- [TensorFlow] tf.nn.softmax_cross_entropy_with_logits的用法
			在计算loss的时候,最常见的一句话就是tf.nn.softmax_cross_entropy_with_logits,那么它到底是怎么做的呢? 首先明确一点,loss是代价值,也就是我们要最小化的值 ... 
- web前端编码规范
			简要介绍 本文通过参考百度腾讯等前端编码规范(链接建文末),得出个人习惯的编码规范.个人编码规范采用在不影响可读性的情况下能省就省,尽量简洁,不需要就直接去掉. 最佳原则不管是个人编码规范还是团队编码 ... 
- MySQL数据库Date型数据插入问题
			MySQL数据库中,Date型数据插入问题,总是提示如下错误: “java.util.Date cannot be cast to java.sql.Date” 解决办法: 1.首先,获取Date型数 ... 
- nginx缓存原理
			一.HTTP字段理解 1.Expires: 该字段的http1.0时的规范,值为一个绝对时间的GMT格式的时间字符串,代表缓存资源的过期时间,在这个时点之前即命中缓存. 缺点:服务器返回的时间,可能与 ... 
- crm--业务点详细概述
			一.CRM简介:(为什么开发CRM) 此项目主要供自己的公司使用,原来因为公司人员较少,人员管理考勤等都用excel保存.但是现在因为公司人员,以及部门增多,为了方便管理 ,供销售人员使用内部系统更方 ... 
- Linux Shell编程第3章——正则表达式
			目录 正则表达式基础 正则表达式的扩展 通配 grep命令 正则表达式基础 Linux Shell以字符串作为表达式向系统传达意思.元字符(Metacharacters)是用来阐述字符表达式意义的字符 ... 
- SpringMVC学习大纲
			PartA: 1.SpringMVC介绍 2.入门程序 3.SpringMVC架构讲解 a) 框架结构 b) 组件说明 4.SpringMVC整合MyBatis 5.参数绑定 a) SpringMVC ... 
- 用python实现一个计算器
			import re def atom_cal(exp): # 计算乘除法 if '*' in exp: a,b = exp.split('*') return str(float(a) * float ... 
