File tree 4 files changed +69
-4
lines changed
solution/1800-1899/1805.Number of Different Integers in a String
4 files changed +69
-4
lines changed Original file line number Diff line number Diff line change 48
48
<li><code>word</code> 由数字和小写英文字母组成</li>
49
49
</ul >
50
50
51
-
52
51
## 解法
53
52
54
53
<!-- 这里可写通用的实现逻辑 -->
55
54
55
+ 将 ` word ` 按照字母切分,得到数字数组 ` nums ` ,然后利用 set 去重,返回 set 的长度即可。
56
+
56
57
<!-- tabs:start -->
57
58
58
59
### ** Python3**
59
60
60
61
<!-- 这里可写当前语言的特殊实现逻辑 -->
61
62
62
63
``` python
64
+ import re
63
65
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 != ' ' })
64
70
```
65
71
66
72
### ** Java**
67
73
68
74
<!-- 这里可写当前语言的特殊实现逻辑 -->
69
75
70
76
``` 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
+ }
72
94
```
73
95
74
96
### ** ...**
Original file line number Diff line number Diff line change @@ -45,21 +45,41 @@ the leading zeros are ignored when comparing their decimal values.
45
45
<li><code>word</code> consists of digits and lowercase English letters.</li>
46
46
</ul >
47
47
48
-
49
48
## Solutions
50
49
51
50
<!-- tabs:start -->
52
51
53
52
### ** Python3**
54
53
55
54
``` python
55
+ import re
56
56
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 != ' ' })
57
61
```
58
62
59
63
### ** Java**
60
64
61
65
``` 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
+ }
63
83
```
64
84
65
85
### ** ...**
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 != '' })
You can’t perform that action at this time.
0 commit comments