Skip to content

Commit 3f3cef1

Browse files
committedMar 25, 2018
提交29题
1 parent 909ee99 commit 3f3cef1

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
 

‎029.两数相除/029 solution.html

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
8+
<title>Document</title>
9+
</head>
10+
11+
<body>
12+
<script>
13+
// Source : https://leetcode.com/problems/divide-two-integers/
14+
// Author : 悬笔e绝
15+
// Date : 2018-03-25
16+
17+
/**
18+
* @param {number} dividend
19+
* @param {number} divisor
20+
* @return {number}
21+
*/
22+
var divide = function (dividend, divisor) {
23+
//1左移31位到了符号位,表示负数,所以是最小负数 -2147483648
24+
var MAX_NEGATIVE_INT = (1 << 31);
25+
// ~表示按位取反,得到最大正数 2147483647
26+
var MAX_POSITIVE_INT = ~(1 << 31);
27+
//向下取整
28+
var ans = Math.floor(dividend / divisor);
29+
30+
if (ans < MAX_NEGATIVE_INT)
31+
ans = MAX_NEGATIVE_INT;
32+
33+
if (ans > MAX_POSITIVE_INT)
34+
ans = MAX_POSITIVE_INT;
35+
36+
return ans;
37+
};
38+
39+
//测试
40+
console.log(divide(3,2));
41+
console.log(divide(10,3));
42+
43+
</script>
44+
</body>
45+
46+
</html>

0 commit comments

Comments
 (0)
Please sign in to comment.