【Leetcode】Divide Two Integers
Divide two integers without using multiplication, division and mod operator.
class Solution {
public:
int divide(int dividend, int divisor) {
long long a = dividend < ? -(long long)dividend : dividend;
long long b = divisor < ? -(long long)divisor : divisor;
bool sign = (dividend >= && divisor >= ) ||
(dividend <= && divisor <= );
long long ans = ;
while (a >= b) {
long long c = b;
for (int i = ; c <= a; c = c << , ++i) {
a -= c;
ans += ( << i);
}
}
return sign ? ans : -ans;
}
};
不能用乘、除和取模,那么还可以用加、减和移位。
符号单独考虑。主要从减法出发,可以通过移位加倍被除数从而实现加速。有一点要特别注意的是可能发生溢出,需要用long long类型来进行中间的运算。
【Leetcode】Divide Two Integers的更多相关文章
- 【leetcode】Divide Two Integers (middle)☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 【Leetcode】 - Divide Two Integers 位运算实现整数除法
实现两个整数的除法,不许用乘法.除法和求模.题目被贴上了BinarySearch,但我没理解为什么会和BinarySearch有关系.我想的方法也和BS一点关系都没有. 很早以前我就猜想,整数的乘法是 ...
- 【Leetcode】【Medium】Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 【LeetCode】970. Powerful Integers 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力搜索 日期 题目地址:https://leetc ...
- 【leetcode】970. Powerful Integers
题目如下: Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j fo ...
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- 【LeetCode】二分 binary_search(共58题)
[4]Median of Two Sorted Arrays [29]Divide Two Integers [33]Search in Rotated Sorted Array [34]Find F ...
- 【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 ...
随机推荐
- Project Browser & Inspector
[Project Browser] You can drag items from the project structure list to the Favourites and also save ...
- solr java api 使用solrj操作zookeeper集群中的solrCloud中的数据
1 导入相关的pom依赖 <dependencies> <dependency> <groupId>org.apache.solr</groupId> ...
- JVM 对象状态判断01
1 引用计数法 给一个对象添加一个引用计数器,每当有一个地方引用时,计数器加1,当引用失效的时候,计数器减去1.当计数器为0的时候,表示对象不可能再被使用.此时表明该对象可以被回收. ...
- codeforce 457DIV2 C题
题意 你需要构造一个n个点m条边的无向有权图,要求这个图的MST中边权的和与从1到n的最短路长度都为素数 分析 可以想到这样一种贪心,在i到i+1直接连一条边,这样最短路和MST都会是同样的一些边.只 ...
- Stream01 定义、迭代、操作、惰性求值、创建流、并行流、收集器、stream运行机制
1 Stream Stream 是 Java 8 提供的一系列对可迭代元素处理的优化方案,使用 Stream 可以大大减少代码量,提高代码的可读性并且使代码更易并行. 2 迭代 2.1 需求 随机创建 ...
- 项目代码:js
1 //获取发文时间 function selectWriteTime(){ $("#writing_time_index").on("click"," ...
- 面试题:HTTP必知必会——常见面试题总结 背1
1.常用的HTTP方法有哪些?GET: 用于请求访问已经被URI(统一资源标识符)识别的资源,可以通过URL传参给服务器POST:用于传输信息给服务器,主要功能与GET方法类似,但一般推荐使用POST ...
- scala的futue和promise
异步操作的有两个经典接口:Future和Promise,其中的 Future 表示一个可能还没有实际完成的异步任务的结果,针对这个结果可以添加 Callback 以便在任务执行成功或失败后做出对应的操 ...
- App性能测试工具使用说明-MobilePerformance
一. 环境搭建 安装Android SDK 1.6或者1.7版本均可,建议1.7,环境变量的配置,Java SDK的安装很简单,不赘述了. 安装SDK 1.安装Android SDK: 2.安装完毕后 ...
- 很好的QSqlDatabase问题说明,关于连接错误(转)
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connect ...