Skip to content

Commit 8dcb462

Browse files
committed
feat: add solutions to lc problem: No.1805
No.1805.Number of Different Integers in a String
1 parent e7fdf45 commit 8dcb462

File tree

9 files changed

+231
-52
lines changed

9 files changed

+231
-52
lines changed

solution/1200-1299/1249.Minimum Remove to Make Valid Parentheses/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public:
141141
if (c == ')') ++x;
142142
else if (c == '(') --x;
143143
ans.push_back(c);
144-
}
144+
}
145145
reverse(ans.begin(), ans.end());
146146
return ans;
147147
}

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

+84-16
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,15 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55-
`word` 按照字母切分,得到数字数组 `nums`,然后利用 set 去重,返回 set 的长度即可。
55+
**方法一:双指针 + 模拟**
56+
57+
遍历字符串,找到每个整数的起始位置和结束位置,截取出这一个子串,将其存入哈希表 `s` 中。
58+
59+
最后返回哈希表 `s` 的大小即可。
60+
61+
注意,这里要去掉整数的前导零,并且不能直接将字符串转为整数,因为整数可能很大,超出 `int` 的范围。
62+
63+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 `word` 的长度。
5664

5765
<!-- tabs:start -->
5866

@@ -61,13 +69,21 @@
6169
<!-- 这里可写当前语言的特殊实现逻辑 -->
6270

6371
```python
64-
import re
65-
66-
6772
class Solution:
6873
def numDifferentIntegers(self, word: str) -> int:
69-
nums = re.split(r'[a-z]+', word)
70-
return len({int(num) for num in nums if num != ''})
74+
s = set()
75+
i, n = 0, len(word)
76+
while i < n:
77+
if word[i].isdigit():
78+
while i < n and word[i] == '0':
79+
i += 1
80+
j = i
81+
while j < n and word[j].isdigit():
82+
j += 1
83+
s.add(word[i: j])
84+
i = j
85+
i += 1
86+
return len(s)
7187
```
7288

7389
### **Java**
@@ -77,20 +93,72 @@ class Solution:
7793
```java
7894
class Solution {
7995
public int numDifferentIntegers(String word) {
80-
String[] nums = word.split("[a-z]+");
81-
Set<String> numSet = new HashSet<>();
82-
for (String num : nums) {
83-
if ("".equals(num)) {
84-
continue;
96+
Set<String> s = new HashSet<>();
97+
int n = word.length();
98+
for (int i = 0; i < n; ++i) {
99+
if (Character.isDigit(word.charAt(i))) {
100+
while (i < n && word.charAt(i) == '0') {
101+
++i;
102+
}
103+
int j = i;
104+
while (j < n && Character.isDigit(word.charAt(j))) {
105+
++j;
106+
}
107+
s.add(word.substring(i, j));
108+
i = j;
85109
}
86-
int j = 0;
87-
while (j < num.length() - 1 && num.charAt(j) == '0') {
88-
++j;
110+
}
111+
return s.size();
112+
}
113+
}
114+
```
115+
116+
### **C++**
117+
118+
```cpp
119+
class Solution {
120+
public:
121+
int numDifferentIntegers(string word) {
122+
unordered_set<string> s;
123+
int n = word.size();
124+
for (int i = 0; i < n; ++i) {
125+
if (isdigit(word[i])) {
126+
while (i < n && word[i] == '0') {
127+
++i;
128+
}
129+
int j = i;
130+
while (j < n && isdigit(word[j])) {
131+
++j;
132+
}
133+
s.insert(word.substr(i, j - i));
134+
i = j;
89135
}
90-
numSet.add(num.substring(j));
91136
}
92-
return numSet.size();
137+
return s.size();
93138
}
139+
};
140+
```
141+
142+
### **Go**
143+
144+
```go
145+
func numDifferentIntegers(word string) int {
146+
s := map[string]struct{}{}
147+
n := len(word)
148+
for i := 0; i < n; i++ {
149+
if word[i] >= '0' && word[i] <= '9' {
150+
for i < n && word[i] == '0' {
151+
i++
152+
}
153+
j := i
154+
for j < n && word[j] >= '0' && word[j] <= '9' {
155+
j++
156+
}
157+
s[word[i:j]] = struct{}{}
158+
i = j
159+
}
160+
}
161+
return len(s)
94162
}
95163
```
96164

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

+75-15
Original file line numberDiff line numberDiff line change
@@ -52,34 +52,94 @@ the leading zeros are ignored when comparing their decimal values.
5252
### **Python3**
5353

5454
```python
55-
import re
56-
57-
5855
class Solution:
5956
def numDifferentIntegers(self, word: str) -> int:
60-
nums = re.split(r'[a-z]+', word)
61-
return len({int(num) for num in nums if num != ''})
57+
s = set()
58+
i, n = 0, len(word)
59+
while i < n:
60+
if word[i].isdigit():
61+
while i < n and word[i] == '0':
62+
i += 1
63+
j = i
64+
while j < n and word[j].isdigit():
65+
j += 1
66+
s.add(word[i: j])
67+
i = j
68+
i += 1
69+
return len(s)
6270
```
6371

6472
### **Java**
6573

6674
```java
6775
class Solution {
6876
public int numDifferentIntegers(String word) {
69-
String[] nums = word.split("[a-z]+");
70-
Set<String> numSet = new HashSet<>();
71-
for (String num : nums) {
72-
if ("".equals(num)) {
73-
continue;
77+
Set<String> s = new HashSet<>();
78+
int n = word.length();
79+
for (int i = 0; i < n; ++i) {
80+
if (Character.isDigit(word.charAt(i))) {
81+
while (i < n && word.charAt(i) == '0') {
82+
++i;
83+
}
84+
int j = i;
85+
while (j < n && Character.isDigit(word.charAt(j))) {
86+
++j;
87+
}
88+
s.add(word.substring(i, j));
89+
i = j;
7490
}
75-
int j = 0;
76-
while (j < num.length() - 1 && num.charAt(j) == '0') {
77-
++j;
91+
}
92+
return s.size();
93+
}
94+
}
95+
```
96+
97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
int numDifferentIntegers(string word) {
103+
unordered_set<string> s;
104+
int n = word.size();
105+
for (int i = 0; i < n; ++i) {
106+
if (isdigit(word[i])) {
107+
while (i < n && word[i] == '0') {
108+
++i;
109+
}
110+
int j = i;
111+
while (j < n && isdigit(word[j])) {
112+
++j;
113+
}
114+
s.insert(word.substr(i, j - i));
115+
i = j;
78116
}
79-
numSet.add(num.substring(j));
80117
}
81-
return numSet.size();
118+
return s.size();
82119
}
120+
};
121+
```
122+
123+
### **Go**
124+
125+
```go
126+
func numDifferentIntegers(word string) int {
127+
s := map[string]struct{}{}
128+
n := len(word)
129+
for i := 0; i < n; i++ {
130+
if word[i] >= '0' && word[i] <= '9' {
131+
for i < n && word[i] == '0' {
132+
i++
133+
}
134+
j := i
135+
for j < n && word[j] >= '0' && word[j] <= '9' {
136+
j++
137+
}
138+
s[word[i:j]] = struct{}{}
139+
i = j
140+
}
141+
}
142+
return len(s)
83143
}
84144
```
85145

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int numDifferentIntegers(string word) {
4+
unordered_set<string> s;
5+
int n = word.size();
6+
for (int i = 0; i < n; ++i) {
7+
if (isdigit(word[i])) {
8+
while (i < n && word[i] == '0') {
9+
++i;
10+
}
11+
int j = i;
12+
while (j < n && isdigit(word[j])) {
13+
++j;
14+
}
15+
s.insert(word.substr(i, j - i));
16+
i = j;
17+
}
18+
}
19+
return s.size();
20+
}
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func numDifferentIntegers(word string) int {
2+
s := map[string]struct{}{}
3+
n := len(word)
4+
for i := 0; i < n; i++ {
5+
if word[i] >= '0' && word[i] <= '9' {
6+
for i < n && word[i] == '0' {
7+
i++
8+
}
9+
j := i
10+
for j < n && word[j] >= '0' && word[j] <= '9' {
11+
j++
12+
}
13+
s[word[i:j]] = struct{}{}
14+
i = j
15+
}
16+
}
17+
return len(s)
18+
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
class Solution {
22
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;
3+
Set<String> s = new HashSet<>();
4+
int n = word.length();
5+
for (int i = 0; i < n; ++i) {
6+
if (Character.isDigit(word.charAt(i))) {
7+
while (i < n && word.charAt(i) == '0') {
8+
++i;
9+
}
10+
int j = i;
11+
while (j < n && Character.isDigit(word.charAt(j))) {
12+
++j;
13+
}
14+
s.add(word.substring(i, j));
15+
i = j;
816
}
9-
int j = 0;
10-
while (j < num.length() - 1 && num.charAt(j) == '0') {
11-
++j;
12-
}
13-
numSet.add(num.substring(j));
1417
}
15-
return numSet.size();
18+
return s.size();
1619
}
1720
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import re
2-
3-
41
class Solution:
52
def numDifferentIntegers(self, word: str) -> int:
6-
nums = re.split(r'[a-z]+', word)
7-
return len({int(num) for num in nums if num != ''})
3+
s = set()
4+
i, n = 0, len(word)
5+
while i < n:
6+
if word[i].isdigit():
7+
while i < n and word[i] == '0':
8+
i += 1
9+
j = i
10+
while j < n and word[j].isdigit():
11+
j += 1
12+
s.add(word[i:j])
13+
i = j
14+
i += 1
15+
return len(s)

solution/1800-1899/1807.Evaluate the Bracket Pairs of a String/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def evaluate(self, s: str, knowledge: List[List[str]]) -> str:
66
while i < n:
77
if s[i] == '(':
88
j = s.find(')', i + 1)
9-
ans.append(d.get(s[i + 1: j], '?'))
9+
ans.append(d.get(s[i + 1 : j], '?'))
1010
i = j
1111
else:
1212
ans.append(s[i])
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Solution:
2-
def maxTastiness(self, price: List[int], tastiness: List[int], maxAmount: int, maxCoupons: int) -> int:
2+
def maxTastiness(
3+
self, price: List[int], tastiness: List[int], maxAmount: int, maxCoupons: int
4+
) -> int:
35
@cache
46
def dfs(i, j, k):
57
if i == len(price):
@@ -8,8 +10,7 @@ def dfs(i, j, k):
810
if j >= price[i]:
911
ans = max(ans, dfs(i + 1, j - price[i], k) + tastiness[i])
1012
if j >= price[i] // 2 and k:
11-
ans = max(
12-
ans, dfs(i + 1, j - price[i] // 2, k - 1) + tastiness[i])
13+
ans = max(ans, dfs(i + 1, j - price[i] // 2, k - 1) + tastiness[i])
1314
return ans
1415

1516
return dfs(0, maxAmount, maxCoupons)

0 commit comments

Comments
 (0)