File tree 1 file changed +46
-0
lines changed
1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
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 >
You can’t perform that action at this time.
0 commit comments