File tree 2 files changed +29
-0
lines changed
2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change 4
4
| :---:| :---:| :---:| :---:|
5
5
| 1| [ 两数之和] ( https://leetcode-cn.com/problems/two-sum/ ) | [ JavaScript] ( ./algorithms/two-sum.js ) | Easy|
6
6
| 2| [ 两数相加] ( https://leetcode-cn.com/problems/add-two-numbers/ ) | [ JavaScript] ( ./algorithms/add-two-numbers.js ) | Medium|
7
+ | 7| [ 整数反转] ( https://leetcode-cn.com/problems/reverse-integer/ ) | [ JavaScript] ( ./algorithms/reverse-integer.js ) | Medium|
7
8
| 9| [ 回文数] ( https://leetcode-cn.com/problems/palindrome-number/ ) | [ JavaScript] ( ./algorithms/palindrome-number.js ) | Easy|
8
9
| 14| [ 最长公共前缀] ( https://leetcode-cn.com/problems/longest-common-prefix/ ) | [ JavaScript] ( ./algorithms/longest-common-prefix.js ) | Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number } x
3
+ * @return {number }
4
+ */
5
+ var reverse = function ( x ) {
6
+ const isNegtive = x < 0 ;
7
+ let sum = 0 ;
8
+
9
+ if ( x === 0 ) {
10
+ return 0 ;
11
+ }
12
+
13
+ x = Math . abs ( x ) ;
14
+
15
+ while ( x ) {
16
+ let t = x % 10 ;
17
+ sum = t + sum * 10 ;
18
+ x = Math . floor ( x / 10 ) ;
19
+ }
20
+
21
+ sum = isNegtive ? - sum : sum ;
22
+
23
+ if ( sum < - Math . pow ( 2 , 31 ) || sum > Math . pow ( 2 , 31 ) - 1 ) {
24
+ return 0 ;
25
+ }
26
+
27
+ return sum ;
28
+ } ;
You can’t perform that action at this time.
0 commit comments