File tree 5 files changed +132
-0
lines changed
solution/2100-2199/2103.Rings and Rods
5 files changed +132
-0
lines changed Original file line number Diff line number Diff line change @@ -151,7 +151,54 @@ func countPoints(rings string) int {
151
151
### ** TypeScript**
152
152
153
153
``` ts
154
+ function countPoints(rings : string ): number {
155
+ const helper = (c : string ) => c .charCodeAt (0 ) - ' A' .charCodeAt (0 );
156
+ const n = rings .length ;
157
+ const target = (1 << helper (' R' )) + (1 << helper (' G' )) + (1 << helper (' B' ));
158
+ const count = new Array (10 ).fill (0 );
159
+ for (let i = 0 ; i < n ; i += 2 ) {
160
+ count [rings [i + 1 ]] |= 1 << helper (rings [i ]);
161
+ }
162
+ return count .reduce ((r , v ) => (r += v === target ? 1 : 0 ), 0 );
163
+ }
164
+ ```
154
165
166
+ ### ** Rust**
167
+
168
+ ``` rust
169
+ impl Solution {
170
+ pub fn count_points (rings : String ) -> i32 {
171
+ let rings = rings . as_bytes ();
172
+ let target = (1 << b 'R' - b 'A' ) + (1 << b 'G' - b 'A' ) + (1 << b 'B' - b 'A' );
173
+ let n = rings . len ();
174
+ let mut count = [0 ; 10 ];
175
+ let mut i = 0 ;
176
+ while i < n {
177
+ count [(rings [i + 1 ] - b '0' ) as usize ] |= 1 << rings [i ] - b 'A' ;
178
+ i += 2 ;
179
+ }
180
+ count . iter (). filter (| & v | * v == target ). count () as i32
181
+ }
182
+ }
183
+ ```
184
+
185
+ ### ** C**
186
+
187
+ ``` c
188
+ int countPoints (char * rings) {
189
+ int target = (1 << ('R' - 'A')) + (1 << ('G' - 'A')) + (1 << ('B' - 'A'));
190
+ int count[ 10] = {0};
191
+ for (int i = 0; rings[ i] ; i += 2) {
192
+ count[ rings[ i + 1] - '0'] |= 1 << (rings[ i] - 'A');
193
+ }
194
+ int ans = 0;
195
+ for (int i = 0; i < 10; i++) {
196
+ if (count[ i] == target) {
197
+ ans++;
198
+ }
199
+ }
200
+ return ans;
201
+ }
155
202
```
156
203
157
204
### **...**
Original file line number Diff line number Diff line change @@ -144,7 +144,54 @@ func countPoints(rings string) int {
144
144
### ** TypeScript**
145
145
146
146
``` ts
147
+ function countPoints(rings : string ): number {
148
+ const helper = (c : string ) => c .charCodeAt (0 ) - ' A' .charCodeAt (0 );
149
+ const n = rings .length ;
150
+ const target = (1 << helper (' R' )) + (1 << helper (' G' )) + (1 << helper (' B' ));
151
+ const count = new Array (10 ).fill (0 );
152
+ for (let i = 0 ; i < n ; i += 2 ) {
153
+ count [rings [i + 1 ]] |= 1 << helper (rings [i ]);
154
+ }
155
+ return count .reduce ((r , v ) => (r += v === target ? 1 : 0 ), 0 );
156
+ }
157
+ ```
147
158
159
+ ### ** Rust**
160
+
161
+ ``` rust
162
+ impl Solution {
163
+ pub fn count_points (rings : String ) -> i32 {
164
+ let rings = rings . as_bytes ();
165
+ let target = (1 << b 'R' - b 'A' ) + (1 << b 'G' - b 'A' ) + (1 << b 'B' - b 'A' );
166
+ let n = rings . len ();
167
+ let mut count = [0 ; 10 ];
168
+ let mut i = 0 ;
169
+ while i < n {
170
+ count [(rings [i + 1 ] - b '0' ) as usize ] |= 1 << rings [i ] - b 'A' ;
171
+ i += 2 ;
172
+ }
173
+ count . iter (). filter (| & v | * v == target ). count () as i32
174
+ }
175
+ }
176
+ ```
177
+
178
+ ### ** C**
179
+
180
+ ``` c
181
+ int countPoints (char * rings) {
182
+ int target = (1 << ('R' - 'A')) + (1 << ('G' - 'A')) + (1 << ('B' - 'A'));
183
+ int count[ 10] = {0};
184
+ for (int i = 0; rings[ i] ; i += 2) {
185
+ count[ rings[ i + 1] - '0'] |= 1 << (rings[ i] - 'A');
186
+ }
187
+ int ans = 0;
188
+ for (int i = 0; i < 10; i++) {
189
+ if (count[ i] == target) {
190
+ ans++;
191
+ }
192
+ }
193
+ return ans;
194
+ }
148
195
```
149
196
150
197
### **...**
Original file line number Diff line number Diff line change
1
+ int countPoints (char * rings ) {
2
+ int target = (1 << ('R' - 'A' )) + (1 << ('G' - 'A' )) + (1 << ('B' - 'A' ));
3
+ int count [10 ] = {0 };
4
+ for (int i = 0 ; rings [i ]; i += 2 ) {
5
+ count [rings [i + 1 ] - '0' ] |= 1 << (rings [i ] - 'A' );
6
+ }
7
+ int ans = 0 ;
8
+ for (int i = 0 ; i < 10 ; i ++ ) {
9
+ if (count [i ] == target ) {
10
+ ans ++ ;
11
+ }
12
+ }
13
+ return ans ;
14
+ }
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn count_points ( rings : String ) -> i32 {
3
+ let rings = rings. as_bytes ( ) ;
4
+ let target = ( 1 << b'R' - b'A' ) + ( 1 << b'G' - b'A' ) + ( 1 << b'B' - b'A' ) ;
5
+ let n = rings. len ( ) ;
6
+ let mut count = [ 0 ; 10 ] ;
7
+ let mut i = 0 ;
8
+ while i < n {
9
+ count[ ( rings[ i + 1 ] - b'0' ) as usize ] |= 1 << rings[ i] - b'A' ;
10
+ i += 2 ;
11
+ }
12
+ count. iter ( ) . filter ( |& v| * v == target) . count ( ) as i32
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ function countPoints ( rings : string ) : number {
2
+ const helper = ( c : string ) => c . charCodeAt ( 0 ) - 'A' . charCodeAt ( 0 ) ;
3
+ const n = rings . length ;
4
+ const target = ( 1 << helper ( 'R' ) ) + ( 1 << helper ( 'G' ) ) + ( 1 << helper ( 'B' ) ) ;
5
+ const count = new Array ( 10 ) . fill ( 0 ) ;
6
+ for ( let i = 0 ; i < n ; i += 2 ) {
7
+ count [ rings [ i + 1 ] ] |= 1 << helper ( rings [ i ] ) ;
8
+ }
9
+ return count . reduce ( ( r , v ) => ( r += v === target ? 1 : 0 ) , 0 ) ;
10
+ }
You can’t perform that action at this time.
0 commit comments