Skip to content

Commit a5ff830

Browse files
committed
feat: update solutions to leetcode problems: No.0007, No.0389
1 parent 8399348 commit a5ff830

File tree

7 files changed

+122
-53
lines changed

7 files changed

+122
-53
lines changed

solution/0000-0099/0007.Reverse Integer/README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
<li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li>
5050
</ul>
5151

52-
5352
## 解法
5453

5554
<!-- 这里可写通用的实现逻辑 -->
@@ -104,6 +103,23 @@ public:
104103
};
105104
```
106105
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+
107123
### **...**
108124

109125
```

solution/0000-0099/0007.Reverse Integer/README_EN.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
<li><code>-2<sup>31</sup> &lt;= x &lt;= 2<sup>31</sup> - 1</code></li>
3030
</ul>
3131

32-
3332
## Solutions
3433

3534
<!-- tabs:start -->
@@ -75,6 +74,23 @@ public:
7574
};
7675
```
7776
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+
7894
### **...**
7995

8096
```

solution/0000-0099/0007.Reverse Integer/Solution.js

+5-45
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,11 @@
22
* @param {number} x
33
* @return {number}
44
*/
5-
6-
/**
7-
* Author: mcnwork2018
8-
*/
9-
105
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);
5010
}
51-
return num;
11+
return res < Math.pow(-2, 31) || res > Math.pow(2, 31) - 1 ? 0 : res;
5212
};

solution/0300-0399/0389.Find the Difference/README.md

+29-3
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,53 @@
4949
<li><code>s</code> 和 <code>t</code> 只包含小写字母</li>
5050
</ul>
5151

52-
5352
## 解法
5453

5554
<!-- 这里可写通用的实现逻辑 -->
5655

56+
计数器实现。
57+
5758
<!-- tabs:start -->
5859

5960
### **Python3**
6061

6162
<!-- 这里可写当前语言的特殊实现逻辑 -->
6263

6364
```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
6575
```
6676

6777
### **Java**
6878

6979
<!-- 这里可写当前语言的特殊实现逻辑 -->
7080

7181
```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+
}
7399
```
74100

75101
### **...**

solution/0300-0399/0389.Find the Difference/README_EN.md

+27-3
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,45 @@
4949
<li><code>s</code> and <code>t</code> consist of lower-case English letters.</li>
5050
</ul>
5151

52-
5352
## Solutions
5453

5554
<!-- tabs:start -->
5655

5756
### **Python3**
5857

5958
```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
6169
```
6270

6371
### **Java**
6472

6573
```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+
}
6791
```
6892

6993
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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

0 commit comments

Comments
 (0)