classSolution { publicintcountPoints(String s) { intn= s.length(), ans = 0; int[] map = newint[128]; for (inti=0; i < n; i += 2) map[s.charAt(i) - 'B'] |= 1 << (s.charAt(i + 1) - '0'); for (inti=0; i < 10; i++) { inttot=0; for (char c : newchar[]{'R', 'G', 'B'}) tot += (map[c - 'B'] >> i) & 1; if (tot == 3) ans++; } return ans; } }
C++ 代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution { public: intcountPoints(string s){ int n = s.size(), ans = 0; vector<int> map(128, 0); for (int i = 0; i < n; i += 2) map[s[i] - 'B'] |= 1 << (s[i + 1] - '0'); for (int i = 0; i < 10; i++) { int tot = 0; for (char c : {'R', 'G', 'B'}) tot += (map[c - 'B'] >> i) & 1; if (tot == 3) ans++; } return ans; } };
Python 代码:
1 2 3 4 5 6 7 8 9 10 11 12
classSolution: defcountPoints(self, s: str) -> int: n, ans = len(s), 0 map = [0] * 128 for i inrange(0, n, 2): map[ord(s[i]) - ord('B')] |= 1 << (int(s[i + 1]) - int('0')) for i inrange(10): tot = 0 for c in ['R', 'G', 'B']: tot += (map[ord(c) - ord('B')] >> i) & 1 ans += 1if tot == 3else0 return ans
TypeScript 代码:
1 2 3 4 5 6 7 8 9 10 11 12 13
functioncountPoints(s: string): number { let n = s.length, ans = 0; const map = newArray(128).fill(0); for (let i = 0; i < n; i += 2) { map[s.charCodeAt(i) - 'B'.charCodeAt(0)] |= 1 << (s.charCodeAt(i + 1) - '0'.charCodeAt(0)); } for (let i = 0; i < 10; i++) { let tot = 0; for (const c of ['R', 'G', 'B']) tot += ((map[c.charCodeAt(0) - 'B'.charCodeAt(0)]) >> i) & 1; if (tot == 3) ans++; } return ans; };