Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaa40721

Browse files
committed
add: Divide Two Integers
1 parent8c4399f commitaa40721

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This is the solution collection of my LeetCode problems, most of them are progra
1616
|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|
1717
|22|[Generate Parentheses](https://leetcode.com/problems/generate-parentheses/)|[JavaScript](./src/generate-parentheses/res.js)|Medium|
1818
|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|
19+
|29|[Divide Two Integers](https://leetcode.com/problems/divide-two-integers/)|[JavaScript](./src/divide-two-integers/res.js)|Medium|
1920
|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|
2021
|34|[Search for a Range](https://leetcode.com/problems/search-for-a-range/)|[JavaScript](./src/search-for-a-range/res.js)|Medium|
2122
|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 (hijiangtao@gmail.com)
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+
letdivide=function(dividend,divisor){
21+
letMIN_INT=-2147483648,
22+
MAX_INT=2147483647,
23+
positive=0;
24+
25+
if(!divisor||(dividend===MIN_INT&&divisor===-1)){
26+
returnMAX_INT;
27+
}
28+
29+
letres=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+
lettmpres=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+
returnres;
55+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp