# 2103. Rings and Rods ###### tags: `Leetcode` `Easy` Link: https://leetcode.com/problems/rings-and-rods/description/ ## Code ```java= class Solution { public int countPoints(String rings) { //0 B 1 R 2 G boolean[][] colors = new boolean[10][3]; for(int i=0; i+1<rings.length(); i+=2){ int rod = rings.charAt(i+1)-'0'; char c = rings.charAt(i); if(c=='B' && !colors[rod][0]) colors[rod][0]=true; else if(c=='R' && !colors[rod][1]) colors[rod][1]=true; else if(c=='G' && !colors[rod][2]) colors[rod][2] = true; } int ans = 0; for(int i=0; i<10; i++){ if(colors[i][0] && colors[i][1] && colors[i][2]) ans++; } return ans; } } ``` Bit Manipulation ```java= class Solution { public int countPoints(String rings) { //0 B 1 R 2 G int[] colors = new int[10]; for(int i=0; i+1<rings.length(); i+=2){ int rod = rings.charAt(i+1)-'0'; char c = rings.charAt(i); if(c=='B' && (colors[rod]&1)==0) colors[rod]+=1; else if(c=='R' && ((colors[rod]>>1)&1)==0) colors[rod]+=(1<<1); else if(c=='G' && ((colors[rod]>>2)&1)==0) colors[rod]+=(1<<2); } int ans = 0; int full = 1+(1<<1)+(1<<2); for(int i=0; i<10; i++){ if(colors[i]==full) ans++; } return ans; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up