LeetCode(29)Divide Two Integers
题目
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
分析
题目要求不用 * / %三种运算符的条件下,求得两个int类型整数的商。
方法一:
很明显的,我们可以用求和累计的方法,求得商,但是该方法测试会出现TLE;参考博客提出解决办法:每次将被除数增加1倍,同时将count也增加一倍,如果超过了被除数,那么用被除数减去当前和再继续本操作,但是我测试结果依然是TLE。所以这道题的目的在于考察逻辑运算。
方法二:
该方法来源于参考博客但是该实现忽略了结果溢出的问题,需要加上结果是否溢出判断。
TLE(方法一)代码
//方法一,翻倍累和 结果是:Time Limit Exceeded
class Solution {
public:
int divide(int dividend, int divisor) {
//如果被除数或者除数有一者为0 或者绝对值除数大于被除数则返回0
if (dividend == 0 || divisor == 0 || abs(divisor) > abs(dividend))
return 0;
int sign = ((dividend > 0 && divisor > 0) || (dividend < 0 && divisor < 0)) ? 1 : -1;
long long Dividend = abs(dividend), Divisor = abs(divisor);
long long sum = 0;
int count = 0, ret = 0;
while (Divisor <= Dividend)
{
count = 1;
sum = Divisor;
while ((sum + sum) < Dividend)
{
sum += sum;
count += count;
}
Dividend -= sum;
ret += count;
}
if (sign == -1)
return 0 - ret;
else
return ret;
}
};
AC代码
//方法二:位运算
class Solution {
public:
int divide(int dividend, int divisor) {
//如果被除数或者除数有一者为0 或者绝对值除数大于被除数则返回0
if (dividend == 0 || divisor == 0)
return 0;
// without using * / mod
// using add
auto sign = [=](long long x) {
return x < 0 ? -1 : 1;
};
int d1 = sign(dividend);
int d2 = sign(divisor);
long long n1 = abs(static_cast<long long>(dividend));
long long n2 = abs(static_cast<long long>(divisor));
long long ans = 0;
while (n1 >= n2) {
long long base = n2;
for (int i = 0; n1 >= base; ++i) {
n1 -= base;
base <<= 1;
ans += 1LL << i;
}
}
//如果转换为int类型,结果溢出,返回INT_MAX ,int类型表示范围[-2147483648 , 2147483648)
if (ans > INT_MAX && d1 == d2)
return INT_MAX;
int res = static_cast<int>(ans);
if (d1 != d2)
return -res;
else
return res;
}
};
LeetCode(29)Divide Two Integers的更多相关文章
- LeetCode(29): 两数相除
Medium! 题目描述: 给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符. 返回被除数 dividend 除以除数 divisor ...
- Leetcode(29)-两数相除
给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符. 返回被除数 dividend 除以除数 divisor 得到的商. 示例 1: 输 ...
- LeetCode(29)-Plus One
题目: Given a non-negative number represented as an array of digits, plus one to the number. The digit ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(4)Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- JMS生产者+单线程发送-我们到底能走多远系列(29)
我们到底能走多远系列(29) 扯淡: “然后我俩各自一端/望着大河弯弯/终于敢放胆/嘻皮笑脸/面对/人生的难” --- <山丘> “迎着风/迎向远方的天空/路上也有艰难/也有那解 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(29)-T4模版
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(29)-T4模版 这讲适合所有的MVC程序 很荣幸,我们的系统有了体验的地址了.演示地址 之前我们发布了一 ...
- Windows Phone开发(29):隔离存储C
原文:Windows Phone开发(29):隔离存储C 本文是隔离存储的第三节,大家先喝杯咖啡放松,今天的内容也是非常简单,我们就聊一件东东--用户设置. 当然了,可能翻译为应用程序设置合适一些,不 ...
- Qt 学习之路 2(29):绘制设备
Qt 学习之路 2(29):绘制设备 豆子 2012年12月3日 Qt 学习之路 2 28条评论 绘图设备是继承QPainterDevice的类.QPaintDevice就是能够进行绘制的类,也就是说 ...
随机推荐
- html 解决空格显示问题
前端开发者都知道,在html中手动输入多个空格或者是回车,在页面解析的时候都被解析成一个空白显示,但有时候的需求要求显示多个空格,这个问题怎么解决呢?根绝我个人的经验,目前找到了以下集中解决办法: 1 ...
- PostgreSQL 9.6.2版本在centOS下的安装和配置
1.如果有用yum安装过旧版,卸载掉: yum remove postgresql* 2.更新一下yum: sudo yum update 3.去 官网 找到 适合你系统 的资源的下载地址,然后使用w ...
- IDEA远程调试Tomcat程序
如何使用 Idea 远程调试 Java 代码 IDEA远程调试的 基本就是在服务端先设置Tomcat服务器启动脚本catalina.bat,然后在客户端IDEA上进行参数配置,最后二者可以通过Sock ...
- _bzoj3223 Tyvj 1729 文艺平衡树【Splay】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3223 裸的,打个标记. #include <cstdio> #include & ...
- [hdu4089] Activation【概率dp 数学期望】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4089 本来可以一遍过的,结果mle了一发...注意要用滚动数组. 令f(i, j)表示队列剩余i个人,这 ...
- (二)python高级特性
一.切片 >>> L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack'] 对这种经常取指定索引范围的操作,用循环十分繁琐,因此,Python ...
- Service官方教程(3)Bound Services
Bound Services 1.In this document The Basics Creating a Bound Service Extending the Binder class Usi ...
- Access2010 - 数据类型[转]
原文链接 Access允许十种数据类型:文本.备注.数值.日期/时间.货币.自动编号.是/否.OLE对象.超级链接.附件.查询向导 . 文本(Text):这种类型允许最大255个字符或数字,Acces ...
- html下的图片链接有边框的解决方法
使用dreamweaver创建网页后,上传到网站发现网页的图片链接有非常难看的蓝色边框,而在dw下是没有的 后来查看了一下网上的资料,发现加一个border="0"即可,默认是有边 ...
- zojDakar Rally(01背包)
01背包 加上每次更新解题数目最多 总用时最少 因为要保证用时最少,要先把时长由小到大排序. 没排序 WA了几小时..链接 #include <iostream> #include< ...