File tree Expand file tree Collapse file tree 7 files changed +138
-0
lines changed
problemset/number-of-different-integers-in-a-string Expand file tree Collapse file tree 7 files changed +138
-0
lines changed Original file line number Diff line number Diff line change 2069
2069
"title" : " 交替合并字符串" ,
2070
2070
"path" : " ./problemset/merge-strings-alternately/README.md" ,
2071
2071
"difficulty" : " 简单"
2072
+ },
2073
+ {
2074
+ "id" : " 1805" ,
2075
+ "title" : " 字符串中不同整数的数目" ,
2076
+ "path" : " ./problemset/number-of-different-integers-in-a-string/README.md" ,
2077
+ "difficulty" : " 简单"
2072
2078
}
2073
2079
]
2074
2080
},
Original file line number Diff line number Diff line change 5279
5279
"url" : " https://leetcode.cn/problems/maximum-ascending-subarray-sum/" ,
5280
5280
"path" : " ./problemset/maximum-ascending-subarray-sum/README.md"
5281
5281
},
5282
+ {
5283
+ "id" : " 1805" ,
5284
+ "title" : {
5285
+ "cn" : " 字符串中不同整数的数目" ,
5286
+ "en" : " number-of-different-integers-in-a-string"
5287
+ },
5288
+ "difficulty" : " 简单" ,
5289
+ "url" : " https://leetcode.cn/problems/number-of-different-integers-in-a-string/" ,
5290
+ "path" : " ./problemset/number-of-different-integers-in-a-string/README.md"
5291
+ },
5282
5292
{
5283
5293
"id" : " 1822" ,
5284
5294
"title" : {
Original file line number Diff line number Diff line change 382
382
| 1417. [ 重新格式化字符串] ( ../../problemset/reformat-the-string/README.md ) | 简单 |
383
383
| 1455. [ 检查单词是否为句中其他单词的前缀] ( ../../problemset/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/README.md ) | 简单 |
384
384
| 1768. [ 交替合并字符串] ( ../../problemset/merge-strings-alternately/README.md ) | 简单 |
385
+ | 1805. [ 字符串中不同整数的数目] ( ../../problemset/number-of-different-integers-in-a-string/README.md ) | 简单 |
385
386
386
387
## 模拟
387
388
Original file line number Diff line number Diff line change 1056
1056
1057
1057
[ 1800. 最大升序子数组和] ( ../../problemset/maximum-ascending-subarray-sum/README.md )
1058
1058
1059
+ [ 1805. 字符串中不同整数的数目] ( ../../problemset/number-of-different-integers-in-a-string/README.md )
1060
+
1059
1061
[ 1822. 数组元素积的符号] ( ../../problemset/sign-of-the-product-of-an-array/README.md )
1060
1062
1061
1063
[ 1823. 找出游戏的获胜者] ( ../../problemset/find-the-winner-of-the-circular-game/README.md )
Original file line number Diff line number Diff line change
1
+ # 字符串中不同整数的数目
2
+
3
+ > 难度:简单
4
+ >
5
+ > https://leetcode.cn/problems/number-of-different-integers-in-a-string/
6
+
7
+ ## 题目
8
+
9
+ 给你一个字符串 ` word ` ,该字符串由数字和小写英文字母组成。
10
+
11
+ 请你用空格替换每个不是数字的字符。例如,"a123bc34d8ef34" 将会变成 " 123 34 8 34" 。注意,剩下的这些整数为(相邻彼此至少有一个空格隔开):"123"、"34"、"8" 和 "34" 。
12
+
13
+ 返回对 ` word ` 完成替换后形成的 ** 不同** 整数的数目。
14
+
15
+ 只有当两个整数的 ** 不含前导零** 的十进制表示不同, 才认为这两个整数也不同。
16
+
17
+ ### 示例
18
+
19
+ #### 示例 1:
20
+
21
+ ```
22
+ 输入:word = "a123bc34d8ef34"
23
+ 输出:3
24
+ 解释:不同的整数有 "123"、"34" 和 "8" 。注意,"34" 只计数一次。
25
+ ```
26
+
27
+ #### 示例 2:
28
+
29
+ ```
30
+ 输入:word = "leet1234code234"
31
+ 输出:2
32
+ ```
33
+
34
+ #### 示例 3:
35
+
36
+ ```
37
+ 输入:word = "a1b01c001"
38
+ 输出:1
39
+ 解释:"1"、"01" 和 "001" 视为同一个整数的十进制表示,因为在比较十进制值时会忽略前导零的存在。
40
+ ```
41
+
42
+ ## 解题
43
+
44
+ ``` ts
45
+ /**
46
+ * 双指针
47
+ * @desc 时间复杂度 O(N) 空间复杂度 O(N)
48
+ * @param word
49
+ * @returns
50
+ */
51
+ export function numDifferentIntegers(word : string ): number {
52
+ const isDigit = (ch : string ) => ch >= ' 0' && ch <= ' 9'
53
+
54
+ const set = new Set ()
55
+ const n = word .length ; let p1 = 0 ; let p2
56
+ while (true ) {
57
+ while (p1 < n && ! isDigit (word [p1 ]))
58
+ p1 ++
59
+ if (p1 === n )
60
+ break
61
+
62
+ p2 = p1
63
+ while (p2 < n && isDigit (word [p2 ]))
64
+ p2 ++
65
+
66
+ while (p2 - p1 > 1 && word [p1 ] === ' 0' )
67
+ p1 ++
68
+
69
+ set .add (word .slice (p1 , p2 ))
70
+ p1 = p2
71
+ }
72
+ return set .size
73
+ }
74
+ ```
Original file line number Diff line number Diff line change
1
+ import { describe , expect , it } from 'vitest'
2
+ import { numDifferentIntegers } from '.'
3
+
4
+ describe ( '字符串中不同整数的数目' , ( ) => {
5
+ testCase ( numDifferentIntegers )
6
+ } )
7
+
8
+ function testCase ( fn : ( word : string ) => number ) {
9
+ it . each ( [
10
+ [ 'a123bc34d8ef34' , 3 ] ,
11
+ [ 'leet1234code234' , 2 ] ,
12
+ [ 'a1b01c001' , 1 ] ,
13
+ ] ) ( '示例%#' , ( word , expected ) => {
14
+ expect ( fn ( word ) ) . toBe ( expected )
15
+ } )
16
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 双指针
3
+ * @desc 时间复杂度 O(N) 空间复杂度 O(N)
4
+ * @param word
5
+ * @returns
6
+ */
7
+ export function numDifferentIntegers ( word : string ) : number {
8
+ const isDigit = ( ch : string ) => ch >= '0' && ch <= '9'
9
+
10
+ const set = new Set ( )
11
+ const n = word . length ; let p1 = 0 ; let p2
12
+ while ( true ) {
13
+ while ( p1 < n && ! isDigit ( word [ p1 ] ) )
14
+ p1 ++
15
+ if ( p1 === n )
16
+ break
17
+
18
+ p2 = p1
19
+ while ( p2 < n && isDigit ( word [ p2 ] ) )
20
+ p2 ++
21
+
22
+ while ( p2 - p1 > 1 && word [ p1 ] === '0' )
23
+ p1 ++
24
+
25
+ set . add ( word . slice ( p1 , p2 ) )
26
+ p1 = p2
27
+ }
28
+ return set . size
29
+ }
You can’t perform that action at this time.
0 commit comments