Skip to content

Commit b4ec5e8

Browse files
committedApr 28, 2021
feat: add solutions to leetcode problem: No.1805. Number of Different Integer in a String
1 parent 93f0475 commit b4ec5e8

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed
 

‎solution/1800-1899/1805.Number of Different Integers in a String/README.md

+24-2
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,49 @@
4848
<li><code>word</code> 由数字和小写英文字母组成</li>
4949
</ul>
5050

51-
5251
## 解法
5352

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

55+
`word` 按照字母切分,得到数字数组 `nums`,然后利用 set 去重,返回 set 的长度即可。
56+
5657
<!-- tabs:start -->
5758

5859
### **Python3**
5960

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

6263
```python
64+
import re
6365

66+
class Solution:
67+
def numDifferentIntegers(self, word: str) -> int:
68+
nums = re.split(r'[a-z]+', word)
69+
return len({int(num) for num in nums if num != ''})
6470
```
6571

6672
### **Java**
6773

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

7076
```java
71-
77+
class Solution {
78+
public int numDifferentIntegers(String word) {
79+
String[] nums = word.split("[a-z]+");
80+
Set<String> numSet = new HashSet<>();
81+
for (String num : nums) {
82+
if ("".equals(num)) {
83+
continue;
84+
}
85+
int j = 0;
86+
while (j < num.length() - 1 && num.charAt(j) == '0') {
87+
++j;
88+
}
89+
numSet.add(num.substring(j));
90+
}
91+
return numSet.size();
92+
}
93+
}
7294
```
7395

7496
### **...**

‎solution/1800-1899/1805.Number of Different Integers in a String/README_EN.md

+22-2
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,41 @@ the leading zeros are ignored when comparing their decimal values.
4545
<li><code>word</code> consists of digits and lowercase English letters.</li>
4646
</ul>
4747

48-
4948
## Solutions
5049

5150
<!-- tabs:start -->
5251

5352
### **Python3**
5453

5554
```python
55+
import re
5656

57+
class Solution:
58+
def numDifferentIntegers(self, word: str) -> int:
59+
nums = re.split(r'[a-z]+', word)
60+
return len({int(num) for num in nums if num != ''})
5761
```
5862

5963
### **Java**
6064

6165
```java
62-
66+
class Solution {
67+
public int numDifferentIntegers(String word) {
68+
String[] nums = word.split("[a-z]+");
69+
Set<String> numSet = new HashSet<>();
70+
for (String num : nums) {
71+
if ("".equals(num)) {
72+
continue;
73+
}
74+
int j = 0;
75+
while (j < num.length() - 1 && num.charAt(j) == '0') {
76+
++j;
77+
}
78+
numSet.add(num.substring(j));
79+
}
80+
return numSet.size();
81+
}
82+
}
6383
```
6484

6585
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int numDifferentIntegers(String word) {
3+
String[] nums = word.split("[a-z]+");
4+
Set<String> numSet = new HashSet<>();
5+
for (String num : nums) {
6+
if ("".equals(num)) {
7+
continue;
8+
}
9+
int j = 0;
10+
while (j < num.length() - 1 && num.charAt(j) == '0') {
11+
++j;
12+
}
13+
numSet.add(num.substring(j));
14+
}
15+
return numSet.size();
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import re
2+
3+
class Solution:
4+
def numDifferentIntegers(self, word: str) -> int:
5+
nums = re.split(r'[a-z]+', word)
6+
return len({int(num) for num in nums if num != ''})

0 commit comments

Comments
 (0)