68
68
69
69
<!-- 这里可写通用的实现逻辑 -->
70
70
71
- 哈希表实现。
71
+ ** 方法一:位运算**
72
+
73
+ 我们可以用一个长度为 $10$ 的数组 $mask$ 来表示每根杆上的环的颜色情况,其中 $mask[ i] $ 表示第 $i$ 根杆上的环的颜色情况,如果第 $i$ 根杆上有红色、绿色、蓝色的环,那么 $mask[ i] $ 的二进制表示为 $111$,即 $mask[ i] = 7$。
74
+
75
+ 我们遍历字符串 $rings$,对于每个颜色位置对 $(c, j)$,其中 $c$ 表示环的颜色,$j$ 表示环所在的杆的编号,我们将 $mask[ j] $ 对应的二进制位进行置位,即 $mask[ j] |= d[ c] $,其中 $d[ c] $ 表示颜色 $c$ 对应的二进制位。
76
+
77
+ 最后我们统计 $mask$ 中值为 $7$ 的元素的个数,即为集齐全部三种颜色环的杆的数目。
78
+
79
+ 时间复杂度 $O(n)$,空间复杂度 $O(|\Sigma|)$,其中 $n$ 表示字符串 $rings$ 的长度,而 $|\Sigma|$ 表示字符集的大小。
72
80
73
81
<!-- tabs:start -->
74
82
79
87
``` python
80
88
class Solution :
81
89
def countPoints (self , rings : str ) -> int :
82
- mp = defaultdict(set )
83
- for i in range (1 , len (rings), 2 ):
84
- c = int (rings[i])
85
- mp[c].add(rings[i - 1 ])
86
- return sum (len (v) == 3 for v in mp.values())
90
+ mask = [0 ] * 10
91
+ d = {" R" : 1 , " G" : 2 , " B" : 4 }
92
+ for i in range (0 , len (rings), 2 ):
93
+ c = rings[i]
94
+ j = int (rings[i + 1 ])
95
+ mask[j] |= d[c]
96
+ return mask.count(7 )
87
97
```
88
98
89
99
### ** Java**
@@ -93,14 +103,19 @@ class Solution:
93
103
``` java
94
104
class Solution {
95
105
public int countPoints (String rings ) {
96
- Map<Integer , Set<Character > > mp = new HashMap<> ();
97
- for (int i = 1 ; i < rings. length(); i += 2 ) {
98
- int c = rings. charAt(i) - ' 0' ;
99
- mp. computeIfAbsent(c, k - > new HashSet<> ()). add(rings. charAt(i - 1 ));
106
+ int [] d = new int [' Z' ];
107
+ d[' R' ] = 1 ;
108
+ d[' G' ] = 2 ;
109
+ d[' B' ] = 4 ;
110
+ int [] mask = new int [10 ];
111
+ for (int i = 0 , n = rings. length(); i < n; i += 2 ) {
112
+ int c = rings. charAt(i);
113
+ int j = rings. charAt(i + 1 ) - ' 0' ;
114
+ mask[j] |= d[c];
100
115
}
101
116
int ans = 0 ;
102
- for (Set< Character > e : mp . values() ) {
103
- if (e . size() == 3 ) {
117
+ for (int x : mask ) {
118
+ if (x == 7 ) {
104
119
++ ans;
105
120
}
106
121
}
@@ -115,54 +130,54 @@ class Solution {
115
130
class Solution {
116
131
public:
117
132
int countPoints(string rings) {
118
- unordered_map<int, unordered_set<char >> mp;
119
- for (int i = 1; i < rings.size(); i += 2) {
120
- int c = rings[ i] - '0';
121
- mp[ c] .insert(rings[ i - 1] );
133
+ int d[ 'Z'] {[ 'R'] = 1, [ 'G'] = 2, [ 'B'] = 4};
134
+ int mask[ 10] {};
135
+ for (int i = 0, n = rings.size(); i < n; i += 2) {
136
+ int c = rings[ i] ;
137
+ int j = rings[ i + 1] - '0';
138
+ mask[ j] |= d[ c] ;
122
139
}
123
- int ans = 0;
124
- for (int i = 0; i < 10; ++i)
125
- if (mp[ i] .size() == 3)
126
- ++ans;
127
- return ans;
140
+ return count(mask, mask + 10, 7);
128
141
}
129
142
};
130
143
```
131
144
132
145
### **Go**
133
146
134
147
```go
135
- func countPoints(rings string) int {
136
- mp := make(map[byte]map[byte]bool)
137
- for i := 1; i < len(rings); i += 2 {
148
+ func countPoints(rings string) (ans int) {
149
+ d := ['Z']int{'R': 1, 'G': 2, 'B': 4}
150
+ mask := [10]int{}
151
+ for i, n := 0, len(rings); i < n; i += 2 {
138
152
c := rings[i]
139
- if len(mp[c]) == 0 {
140
- mp[c] = make(map[byte]bool)
141
- }
142
- mp[c][rings[i-1]] = true
153
+ j := int(rings[i+1] - '0')
154
+ mask[j] |= d[c]
143
155
}
144
- ans := 0
145
- for _, v := range mp {
146
- if len(v) == 3 {
156
+ for _, x := range mask {
157
+ if x == 7 {
147
158
ans++
148
159
}
149
160
}
150
- return ans
161
+ return
151
162
}
152
163
```
153
164
154
165
### ** TypeScript**
155
166
156
167
``` ts
157
168
function countPoints(rings : string ): number {
158
- const helper = (c : string ) => c .charCodeAt (0 ) - ' A' .charCodeAt (0 );
159
- const n = rings .length ;
160
- const target = (1 << helper (' R' )) + (1 << helper (' G' )) + (1 << helper (' B' ));
161
- const count = new Array (10 ).fill (0 );
162
- for (let i = 0 ; i < n ; i += 2 ) {
163
- count [rings [i + 1 ]] |= 1 << helper (rings [i ]);
169
+ const idx = (c : string ) => c .charCodeAt (0 ) - ' A' .charCodeAt (0 );
170
+ const d: number [] = Array (26 ).fill (0 );
171
+ d [idx (' R' )] = 1 ;
172
+ d [idx (' G' )] = 2 ;
173
+ d [idx (' B' )] = 4 ;
174
+ const mask: number [] = Array (10 ).fill (0 );
175
+ for (let i = 0 ; i < rings .length ; i += 2 ) {
176
+ const c = rings [i ];
177
+ const j = rings [i + 1 ].charCodeAt (0 ) - ' 0' .charCodeAt (0 );
178
+ mask [j ] |= d [idx (c )];
164
179
}
165
- return count . reduce (( r , v ) => ( r += v === target ? 1 : 0 ), 0 ) ;
180
+ return mask . filter ( x => x === 7 ). length ;
166
181
}
167
182
```
168
183
@@ -171,16 +186,22 @@ function countPoints(rings: string): number {
171
186
``` rust
172
187
impl Solution {
173
188
pub fn count_points (rings : String ) -> i32 {
174
- let rings = rings . as_bytes ();
175
- let target = (1 << b 'R' - b 'A' ) + (1 << b 'G' - b 'A' ) + (1 << b 'B' - b 'A' );
176
- let n = rings . len ();
177
- let mut count = [0 ; 10 ];
178
- let mut i = 0 ;
179
- while i < n {
180
- count [(rings [i + 1 ] - b '0' ) as usize ] |= 1 << rings [i ] - b 'A' ;
181
- i += 2 ;
189
+ let mut d : [i32 ; 90 ] = [0 ; 90 ];
190
+ d ['R' as usize ] = 1 ;
191
+ d ['G' as usize ] = 2 ;
192
+ d ['B' as usize ] = 4 ;
193
+
194
+ let mut mask : [i32 ; 10 ] = [0 ; 10 ];
195
+
196
+ let cs : Vec <char > = rings . chars (). collect ();
197
+
198
+ for i in (0 .. cs . len ()). step_by (2 ) {
199
+ let c = cs [i ] as usize ;
200
+ let j = cs [i + 1 ] as usize - '0' as usize ;
201
+ mask [j ] |= d [c ];
182
202
}
183
- count . iter (). filter (| & v | * v == target ). count () as i32
203
+
204
+ mask . iter (). filter (|&& x | x == 7 ). count () as i32
184
205
}
185
206
}
186
207
```
@@ -189,17 +210,28 @@ impl Solution {
189
210
190
211
``` c
191
212
int countPoints (char* rings) {
192
- int target = (1 << ('R' - 'A')) + (1 << ('G' - 'A')) + (1 << ('B' - 'A'));
193
- int count[ 10] = {0};
194
- for (int i = 0; rings[ i] ; i += 2) {
195
- count[ rings[ i + 1] - '0'] |= 1 << (rings[ i] - 'A');
213
+ int d[ 'Z'] ;
214
+ memset(d, 0, sizeof(d));
215
+ d[ 'R'] = 1;
216
+ d[ 'G'] = 2;
217
+ d[ 'B'] = 4;
218
+
219
+ int mask[10];
220
+ memset(mask, 0, sizeof(mask));
221
+
222
+ for (int i = 0, n = strlen(rings); i < n; i += 2) {
223
+ int c = rings[i];
224
+ int j = rings[i + 1] - '0';
225
+ mask[j] |= d[c];
196
226
}
227
+
197
228
int ans = 0;
198
229
for (int i = 0; i < 10; i++) {
199
- if (count [ i] == target ) {
230
+ if (mask [i] == 7 ) {
200
231
ans++;
201
232
}
202
233
}
234
+
203
235
return ans;
204
236
}
205
237
```
0 commit comments