Divide Two Integers leetcode java
题目:
Divide two integers without using multiplication, division and mod operator.
题解:
这道题我自己没想出来。。。乘除取模都不让用。。那只有加减了。。。我参考的http://blog.csdn.net/perfect8886/article/details/23040143
代码如下:
1 public int divide(int dividend, int divisor) {
2 if (dividend == 0 || divisor == 0) {
3 return 0;
4 }
5 boolean isNeg = (dividend > 0 && divisor < 0)
6 || (dividend < 0 && divisor > 0);
7 long a = Math.abs((long) dividend);
8 long b = Math.abs((long) divisor);
9 if (b > a) {
return 0;
}
long sum = 0;
long pow = 0;
int result = 0;
while (a >= b) {
pow = 1;
sum = b;
while (sum + sum <= a) {
sum += sum;
pow += pow;
}
a -= sum;
result += pow;
}
return isNeg ? -result : result;
}
Reference:
http://blog.csdn.net/perfect8886/article/details/23040143
Divide Two Integers leetcode java的更多相关文章
- 29. Divide Two Integers - LeetCode
Question 29. Divide Two Integers Solution 题目大意:给定两个数字,求出它们的商,要求不能使用乘法.除法以及求余操作. 思路:说下用移位实现的方法 7/3=2, ...
- Divide Two Integers leetcode
题目:Divide Two Integers Divide two integers without using multiplication, division and mod operator. ...
- Divide Two Integers —— LeetCode
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- Java for LeetCode 029 Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- LeetCode第[29]题(Java):Divide Two Integers
题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...
- Java [leetcode 29]Divide Two Integers
题目描述: Divide two integers without using multiplication, division and mod operator. If it is overflow ...
- LeetCode: Divide Two Integers 解题报告
Divide Two Integers Divide two integers without using multiplication, division and mod operator. SOL ...
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- leetcode面试准备:Divide Two Integers
leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...
随机推荐
- leetcode easy problem set
*勿以浮沙筑高台* 持续更新........ 题目网址:https://leetcode.com/problemset/all/?difficulty=Easy 1. Two Sum [4m ...
- 用UltraEdit转换大小写
alt+F5转大写: ctrl+F5转小写: F5每个单词的首字母大写: Shift+F5大小写互换.
- [leetcode sort]75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- PHP 快速排序算法详解
备注:下面转载的快速排序算法有bug,数组中重复值会被删除,修改后如下: function quickSort($arr){ //递归出口 if(!isset($arr[1])){ return $a ...
- 2017 ACM Amman Collegiate Programming Contest
A - Watching TV /* 题意:求出出现次数最多的数字 */ #include <cstdio> #include <algorithm> #include < ...
- 【数论】Codeforces Round #483 (Div. 2) [Thanks, Botan Investments and Victor Shaburov!] C. Finite or not?
题意:给你一个分数,问你在b进制下能否化成有限小数. 条件:p/q假如已是既约分数,那么如果q的质因数分解集合是b的子集,就可以化成有限小数,否则不能. 参见代码:反复从q中除去b和q的公因子部分,并 ...
- java集合之一(框架介绍)
本文转载自:http://www.cnblogs.com/skywang12345/p/3308498.html Java集合主要可以划分为4个部分:List列表.Set集合.Map映射.工具类(It ...
- bzoj 3209 数位DP+欧拉定理
枚举1的个数,统计有那么多1的数的个数 /************************************************************** Problem: 3209 Us ...
- PYQT设计无边框窗体
#UI.py,通过UI设计师制作后直接转换为UI.py脚本 # -*- coding: utf-8 -*-from PyQt4 import QtCore, QtGui try: _fromUt ...
- poj 3630 Phone List 贪心
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23722 Accepted: 7289 Descr ...