File tree 7 files changed +122
-53
lines changed
0000-0099/0007.Reverse Integer
0300-0399/0389.Find the Difference
7 files changed +122
-53
lines changed Original file line number Diff line number Diff line change 49
49
<li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li>
50
50
</ul >
51
51
52
-
53
52
## 解法
54
53
55
54
<!-- 这里可写通用的实现逻辑 -->
@@ -104,6 +103,23 @@ public:
104
103
};
105
104
```
106
105
106
+ ### **JavaScript**
107
+
108
+ ```js
109
+ /**
110
+ * @param {number} x
111
+ * @return {number}
112
+ */
113
+ var reverse = function (x) {
114
+ let res = 0;
115
+ while (x) {
116
+ res = res * 10 + (x % 10);
117
+ x = ~~(x / 10);
118
+ }
119
+ return res < Math.pow(-2, 31) || res > Math.pow(2, 31) - 1 ? 0 : res;
120
+ };
121
+ ```
122
+
107
123
### ** ...**
108
124
109
125
```
Original file line number Diff line number Diff line change 29
29
<li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li>
30
30
</ul >
31
31
32
-
33
32
## Solutions
34
33
35
34
<!-- tabs:start -->
@@ -75,6 +74,23 @@ public:
75
74
};
76
75
```
77
76
77
+ ### **JavaScript**
78
+
79
+ ```js
80
+ /**
81
+ * @param {number} x
82
+ * @return {number}
83
+ */
84
+ var reverse = function (x) {
85
+ let res = 0;
86
+ while (x) {
87
+ res = res * 10 + (x % 10);
88
+ x = ~~(x / 10);
89
+ }
90
+ return res < Math.pow(-2, 31) || res > Math.pow(2, 31) - 1 ? 0 : res;
91
+ };
92
+ ```
93
+
78
94
### ** ...**
79
95
80
96
```
Original file line number Diff line number Diff line change 2
2
* @param {number } x
3
3
* @return {number }
4
4
*/
5
-
6
- /**
7
- * Author: mcnwork2018
8
- */
9
-
10
5
var reverse = function ( x ) {
11
- let min = - Math . pow ( 2 , 31 ) ,
12
- max = Math . pow ( 2 , 31 ) - 1 ;
13
- let rev = 0 ;
14
- while ( x != 0 ) {
15
- let pop = x % 10 ;
16
- x = ( x - pop ) / 10 ;
17
- if ( rev > max / 10 || ( rev == max / 10 && pop > 7 ) ) return 0 ;
18
- if ( rev < min / 10 || ( rev == min / 10 && pop < - 8 ) ) return 0 ;
19
- rev = rev * 10 + pop ;
20
- }
21
- return rev ;
22
- } ;
23
-
24
- /**
25
- * Author: rookie
26
- */
27
-
28
- var reverse = function ( x ) {
29
- const s = x + "" ;
30
- let i = 0 ;
31
- let sign = 1 ;
32
- if ( s [ i ] == "-" ) {
33
- i ++ ;
34
- sign = - 1 ;
35
- }
36
- if ( s [ i ] == "+" ) {
37
- i ++ ;
38
- }
39
- let num = 0 ;
40
- for ( let j = s . length - 1 ; j >= i ; j -- ) {
41
- num = num * 10 + parseInt ( s [ j ] ) ;
42
- }
43
- num *= sign ;
44
- let max = 2 ;
45
- for ( let n = 0 ; n < 30 ; n ++ ) {
46
- max *= 2 ;
47
- }
48
- if ( num > max || num < - max ) {
49
- return 0 ;
6
+ let res = 0 ;
7
+ while ( x ) {
8
+ res = res * 10 + ( x % 10 ) ;
9
+ x = ~ ~ ( x / 10 ) ;
50
10
}
51
- return num ;
11
+ return res < Math . pow ( - 2 , 31 ) || res > Math . pow ( 2 , 31 ) - 1 ? 0 : res ;
52
12
} ;
Original file line number Diff line number Diff line change 49
49
<li><code>s</code> 和 <code>t</code> 只包含小写字母</li>
50
50
</ul >
51
51
52
-
53
52
## 解法
54
53
55
54
<!-- 这里可写通用的实现逻辑 -->
56
55
56
+ 计数器实现。
57
+
57
58
<!-- tabs:start -->
58
59
59
60
### ** Python3**
60
61
61
62
<!-- 这里可写当前语言的特殊实现逻辑 -->
62
63
63
64
``` python
64
-
65
+ class Solution :
66
+ def findTheDifference (self , s : str , t : str ) -> str :
67
+ counter = collections.Counter()
68
+ for c in s:
69
+ counter[c] += 1
70
+ for c in t:
71
+ if counter[c] <= 0 :
72
+ return c
73
+ counter[c] -= 1
74
+ return None
65
75
```
66
76
67
77
### ** Java**
68
78
69
79
<!-- 这里可写当前语言的特殊实现逻辑 -->
70
80
71
81
``` java
72
-
82
+ class Solution {
83
+ public char findTheDifference (String s , String t ) {
84
+ int [] counter = new int [26 ];
85
+ for (int i = 0 ; i < s. length(); ++ i) {
86
+ int index = s. charAt(i) - ' a' ;
87
+ ++ counter[index];
88
+ }
89
+ for (int i = 0 ; i < t. length(); ++ i) {
90
+ int index = t. charAt(i) - ' a' ;
91
+ if (counter[index] <= 0 ) {
92
+ return t. charAt(i);
93
+ }
94
+ -- counter[index];
95
+ }
96
+ return ' ' ;
97
+ }
98
+ }
73
99
```
74
100
75
101
### ** ...**
Original file line number Diff line number Diff line change 49
49
<li><code>s</code> and <code>t</code> consist of lower-case English letters.</li>
50
50
</ul >
51
51
52
-
53
52
## Solutions
54
53
55
54
<!-- tabs:start -->
56
55
57
56
### ** Python3**
58
57
59
58
``` python
60
-
59
+ class Solution :
60
+ def findTheDifference (self , s : str , t : str ) -> str :
61
+ counter = collections.Counter()
62
+ for c in s:
63
+ counter[c] += 1
64
+ for c in t:
65
+ if counter[c] <= 0 :
66
+ return c
67
+ counter[c] -= 1
68
+ return None
61
69
```
62
70
63
71
### ** Java**
64
72
65
73
``` java
66
-
74
+ class Solution {
75
+ public char findTheDifference (String s , String t ) {
76
+ int [] counter = new int [26 ];
77
+ for (int i = 0 ; i < s. length(); ++ i) {
78
+ int index = s. charAt(i) - ' a' ;
79
+ ++ counter[index];
80
+ }
81
+ for (int i = 0 ; i < t. length(); ++ i) {
82
+ int index = t. charAt(i) - ' a' ;
83
+ if (counter[index] <= 0 ) {
84
+ return t. charAt(i);
85
+ }
86
+ -- counter[index];
87
+ }
88
+ return ' ' ;
89
+ }
90
+ }
67
91
```
68
92
69
93
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public char findTheDifference (String s , String t ) {
3
+ int [] counter = new int [26 ];
4
+ for (int i = 0 ; i < s .length (); ++i ) {
5
+ int index = s .charAt (i ) - 'a' ;
6
+ ++counter [index ];
7
+ }
8
+ for (int i = 0 ; i < t .length (); ++i ) {
9
+ int index = t .charAt (i ) - 'a' ;
10
+ if (counter [index ] <= 0 ) {
11
+ return t .charAt (i );
12
+ }
13
+ --counter [index ];
14
+ }
15
+ return ' ' ;
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def findTheDifference (self , s : str , t : str ) -> str :
3
+ counter = collections .Counter ()
4
+ for c in s :
5
+ counter [c ] += 1
6
+ for c in t :
7
+ if counter [c ] <= 0 :
8
+ return c
9
+ counter [c ] -= 1
10
+ return None
You can’t perform that action at this time.
0 commit comments