Skip to content

Commit 6d34b83

Browse files
committed
Merge branch 'master' of github.com:hijiangtao/LeetCodeOJ
2 parents 514c55a + 2d53b0b commit 6d34b83

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# LeetCodeOJ
22

3-
This is the solution collection of my LeetCode problems, most of them are programmed in JavaScript. All JavaScript codes are wrote in ECMAScript 6 standard, each solution file will contain a problem description in the beginning.
3+
This is the solutions collection of my LeetCode submissions, most of them are programmed in JavaScript. All JavaScript codes are wrote in ECMAScript 6 standard, each solution file will contain a problem description in the beginning, and followed by some necessary explanation, some problems will provide more than one solution.
4+
5+
**ATTENTION**: If you also use JavaScript as your coding language, you should pay attention to some JavaScript INTERNAL issues, such as bitwise operators, so as to let you not drop into some trouble which may be caused by JavaScript itself.
46

57
**Progress: 33/**
68

@@ -16,6 +18,7 @@ This is the solution collection of my LeetCode problems, most of them are progra
1618
|19|[Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) | [JavaScript](./src/remove-nth-node-from-end-of-list/res.js)|Medium|
1719
|22|[Generate Parentheses](https://leetcode.com/problems/generate-parentheses/) | [JavaScript](./src/generate-parentheses/res.js)|Medium|
1820
|26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [JavaScript](./src/remove-duplicates-from-sorted-array/res.js)|Easy|
21+
|29|[Divide Two Integers](https://leetcode.com/problems/divide-two-integers/) | [JavaScript](./src/divide-two-integers/res.js)|Medium|
1922
|33|[Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) | [JavaScript](./src/search-in-rotated-sorted-array/res.js)|Medium|
2023
|34|[Search for a Range](https://leetcode.com/problems/search-for-a-range/) | [JavaScript](./src/search-for-a-range/res.js)|Medium|
2124
|48|[Rotate Image](https://leetcode.com/problems/rotate-image/) | [JavaScript](./src/rotate-image/res.js)|Medium|

src/divide-two-integers/res.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* res.js
3+
* @authors Joe Jiang ([email protected])
4+
* @date 2017-03-01 21:57:06
5+
* @version $Id$
6+
*/
7+
8+
/**
9+
* Divide two integers without using multiplication, division and mod operator.
10+
*
11+
* If it is overflow, return MAX_INT.
12+
*
13+
* Pay attention to Javascript Bitwise operators
14+
* http://www.ecma-international.org/ecma-262/5.1/#sec-11.10
15+
*
16+
* @param {number} dividend
17+
* @param {number} divisor
18+
* @return {number}
19+
*/
20+
let divide = function(dividend, divisor) {
21+
let MIN_INT = -2147483648,
22+
MAX_INT = 2147483647,
23+
positive = 0;
24+
25+
if(!divisor || (dividend === MIN_INT && divisor === -1)) {
26+
return MAX_INT;
27+
}
28+
29+
let res = 0;
30+
if (dividend < 0) {
31+
dividend = -dividend;
32+
positive += 1;
33+
}
34+
if (divisor < 0) {
35+
divisor = -divisor;
36+
positive += 1;
37+
}
38+
39+
while (dividend >= divisor) {
40+
let tmpres = 1, tmpdiv = divisor;
41+
42+
while ((dividend >> 1) >= tmpdiv) {
43+
tmpres <<= 1;
44+
tmpdiv <<= 1;
45+
}
46+
res += tmpres;
47+
dividend -= tmpdiv;
48+
}
49+
50+
if (positive === 1) {
51+
return -res;
52+
}
53+
54+
return res;
55+
};

0 commit comments

Comments
 (0)