diff --git a/solution/2100-2199/2103.Rings and Rods/README.md b/solution/2100-2199/2103.Rings and Rods/README.md index b5f9dcd66f8b3..f330da6375382 100644 --- a/solution/2100-2199/2103.Rings and Rods/README.md +++ b/solution/2100-2199/2103.Rings and Rods/README.md @@ -68,7 +68,15 @@ -哈希表实现。 +**方法一:位运算** + +我们可以用一个长度为 $10$ 的数组 $mask$ 来表示每根杆上的环的颜色情况,其中 $mask[i]$ 表示第 $i$ 根杆上的环的颜色情况,如果第 $i$ 根杆上有红色、绿色、蓝色的环,那么 $mask[i]$ 的二进制表示为 $111$,即 $mask[i] = 7$。 + +我们遍历字符串 $rings$,对于每个颜色位置对 $(c, j)$,其中 $c$ 表示环的颜色,$j$ 表示环所在的杆的编号,我们将 $mask[j]$ 对应的二进制位进行置位,即 $mask[j] |= d[c]$,其中 $d[c]$ 表示颜色 $c$ 对应的二进制位。 + +最后我们统计 $mask$ 中值为 $7$ 的元素的个数,即为集齐全部三种颜色环的杆的数目。 + +时间复杂度 $O(n)$,空间复杂度 $O(|\Sigma|)$,其中 $n$ 表示字符串 $rings$ 的长度,而 $|\Sigma|$ 表示字符集的大小。 @@ -79,11 +87,13 @@ ```python class Solution: def countPoints(self, rings: str) -> int: - mp = defaultdict(set) - for i in range(1, len(rings), 2): - c = int(rings[i]) - mp[c].add(rings[i - 1]) - return sum(len(v) == 3 for v in mp.values()) + mask = [0] * 10 + d = {"R": 1, "G": 2, "B": 4} + for i in range(0, len(rings), 2): + c = rings[i] + j = int(rings[i + 1]) + mask[j] |= d[c] + return mask.count(7) ``` ### **Java** @@ -93,14 +103,19 @@ class Solution: ```java class Solution { public int countPoints(String rings) { - Map> mp = new HashMap<>(); - for (int i = 1; i < rings.length(); i += 2) { - int c = rings.charAt(i) - '0'; - mp.computeIfAbsent(c, k -> new HashSet<>()).add(rings.charAt(i - 1)); + int[] d = new int['Z']; + d['R'] = 1; + d['G'] = 2; + d['B'] = 4; + int[] mask = new int[10]; + for (int i = 0, n = rings.length(); i < n; i += 2) { + int c = rings.charAt(i); + int j = rings.charAt(i + 1) - '0'; + mask[j] |= d[c]; } int ans = 0; - for (Set e : mp.values()) { - if (e.size() == 3) { + for (int x : mask) { + if (x == 7) { ++ans; } } @@ -115,16 +130,14 @@ class Solution { class Solution { public: int countPoints(string rings) { - unordered_map> mp; - for (int i = 1; i < rings.size(); i += 2) { - int c = rings[i] - '0'; - mp[c].insert(rings[i - 1]); + int d['Z']{['R'] = 1, ['G'] = 2, ['B'] = 4}; + int mask[10]{}; + for (int i = 0, n = rings.size(); i < n; i += 2) { + int c = rings[i]; + int j = rings[i + 1] - '0'; + mask[j] |= d[c]; } - int ans = 0; - for (int i = 0; i < 10; ++i) - if (mp[i].size() == 3) - ++ans; - return ans; + return count(mask, mask + 10, 7); } }; ``` @@ -132,22 +145,20 @@ public: ### **Go** ```go -func countPoints(rings string) int { - mp := make(map[byte]map[byte]bool) - for i := 1; i < len(rings); i += 2 { +func countPoints(rings string) (ans int) { + d := ['Z']int{'R': 1, 'G': 2, 'B': 4} + mask := [10]int{} + for i, n := 0, len(rings); i < n; i += 2 { c := rings[i] - if len(mp[c]) == 0 { - mp[c] = make(map[byte]bool) - } - mp[c][rings[i-1]] = true + j := int(rings[i+1] - '0') + mask[j] |= d[c] } - ans := 0 - for _, v := range mp { - if len(v) == 3 { + for _, x := range mask { + if x == 7 { ans++ } } - return ans + return } ``` @@ -155,14 +166,18 @@ func countPoints(rings string) int { ```ts function countPoints(rings: string): number { - const helper = (c: string) => c.charCodeAt(0) - 'A'.charCodeAt(0); - const n = rings.length; - const target = (1 << helper('R')) + (1 << helper('G')) + (1 << helper('B')); - const count = new Array(10).fill(0); - for (let i = 0; i < n; i += 2) { - count[rings[i + 1]] |= 1 << helper(rings[i]); + const idx = (c: string) => c.charCodeAt(0) - 'A'.charCodeAt(0); + const d: number[] = Array(26).fill(0); + d[idx('R')] = 1; + d[idx('G')] = 2; + d[idx('B')] = 4; + const mask: number[] = Array(10).fill(0); + for (let i = 0; i < rings.length; i += 2) { + const c = rings[i]; + const j = rings[i + 1].charCodeAt(0) - '0'.charCodeAt(0); + mask[j] |= d[idx(c)]; } - return count.reduce((r, v) => (r += v === target ? 1 : 0), 0); + return mask.filter(x => x === 7).length; } ``` @@ -171,16 +186,22 @@ function countPoints(rings: string): number { ```rust impl Solution { pub fn count_points(rings: String) -> i32 { - let rings = rings.as_bytes(); - let target = (1 << b'R' - b'A') + (1 << b'G' - b'A') + (1 << b'B' - b'A'); - let n = rings.len(); - let mut count = [0; 10]; - let mut i = 0; - while i < n { - count[(rings[i + 1] - b'0') as usize] |= 1 << rings[i] - b'A'; - i += 2; + let mut d: [i32; 90] = [0; 90]; + d['R' as usize] = 1; + d['G' as usize] = 2; + d['B' as usize] = 4; + + let mut mask: [i32; 10] = [0; 10]; + + let cs: Vec = rings.chars().collect(); + + for i in (0..cs.len()).step_by(2) { + let c = cs[i] as usize; + let j = cs[i + 1] as usize - '0' as usize; + mask[j] |= d[c]; } - count.iter().filter(|&v| *v == target).count() as i32 + + mask.iter().filter(|&&x| x == 7).count() as i32 } } ``` @@ -189,17 +210,28 @@ impl Solution { ```c int countPoints(char* rings) { - int target = (1 << ('R' - 'A')) + (1 << ('G' - 'A')) + (1 << ('B' - 'A')); - int count[10] = {0}; - for (int i = 0; rings[i]; i += 2) { - count[rings[i + 1] - '0'] |= 1 << (rings[i] - 'A'); + int d['Z']; + memset(d, 0, sizeof(d)); + d['R'] = 1; + d['G'] = 2; + d['B'] = 4; + + int mask[10]; + memset(mask, 0, sizeof(mask)); + + for (int i = 0, n = strlen(rings); i < n; i += 2) { + int c = rings[i]; + int j = rings[i + 1] - '0'; + mask[j] |= d[c]; } + int ans = 0; for (int i = 0; i < 10; i++) { - if (count[i] == target) { + if (mask[i] == 7) { ans++; } } + return ans; } ``` diff --git a/solution/2100-2199/2103.Rings and Rods/README_EN.md b/solution/2100-2199/2103.Rings and Rods/README_EN.md index f6faee4bd4452..7cd317c3a3970 100644 --- a/solution/2100-2199/2103.Rings and Rods/README_EN.md +++ b/solution/2100-2199/2103.Rings and Rods/README_EN.md @@ -62,7 +62,15 @@ Only one ring is given. Thus, no rods have all three colors. ## Solutions -Using hash table. +**Solution 1: Bit Manipulation** + +We can use an array $mask$ of length $10$ to represent the color situation of the rings on each rod, where $mask[i]$ represents the color situation of the ring on the $i$th rod. If there are red, green, and blue rings on the $i$th rod, then the binary representation of $mask[i]$ is $111$, that is, $mask[i] = 7$. + +We traverse the string $rings$. For each color position pair $(c, j)$, where $c$ represents the color of the ring and $j$ represents the number of the rod where the ring is located, we set the corresponding binary bit of $mask[j]$, that is, $mask[j] |= d[c]$, where $d[c]$ represents the binary bit corresponding to color $c$. + +Finally, we count the number of elements in $mask$ that are $7$, which is the number of rods that have collected all three colors of rings. + +The time complexity is $O(n)$, and the space complexity is $O(|\Sigma|)$, where $n$ represents the length of the string $rings$, and $|\Sigma|$ represents the size of the character set. @@ -71,11 +79,13 @@ Using hash table. ```python class Solution: def countPoints(self, rings: str) -> int: - mp = defaultdict(set) - for i in range(1, len(rings), 2): - c = int(rings[i]) - mp[c].add(rings[i - 1]) - return sum(len(v) == 3 for v in mp.values()) + mask = [0] * 10 + d = {"R": 1, "G": 2, "B": 4} + for i in range(0, len(rings), 2): + c = rings[i] + j = int(rings[i + 1]) + mask[j] |= d[c] + return mask.count(7) ``` ### **Java** @@ -83,14 +93,19 @@ class Solution: ```java class Solution { public int countPoints(String rings) { - Map> mp = new HashMap<>(); - for (int i = 1; i < rings.length(); i += 2) { - int c = rings.charAt(i) - '0'; - mp.computeIfAbsent(c, k -> new HashSet<>()).add(rings.charAt(i - 1)); + int[] d = new int['Z']; + d['R'] = 1; + d['G'] = 2; + d['B'] = 4; + int[] mask = new int[10]; + for (int i = 0, n = rings.length(); i < n; i += 2) { + int c = rings.charAt(i); + int j = rings.charAt(i + 1) - '0'; + mask[j] |= d[c]; } int ans = 0; - for (Set e : mp.values()) { - if (e.size() == 3) { + for (int x : mask) { + if (x == 7) { ++ans; } } @@ -105,16 +120,14 @@ class Solution { class Solution { public: int countPoints(string rings) { - unordered_map> mp; - for (int i = 1; i < rings.size(); i += 2) { - int c = rings[i] - '0'; - mp[c].insert(rings[i - 1]); + int d['Z']{['R'] = 1, ['G'] = 2, ['B'] = 4}; + int mask[10]{}; + for (int i = 0, n = rings.size(); i < n; i += 2) { + int c = rings[i]; + int j = rings[i + 1] - '0'; + mask[j] |= d[c]; } - int ans = 0; - for (int i = 0; i < 10; ++i) - if (mp[i].size() == 3) - ++ans; - return ans; + return count(mask, mask + 10, 7); } }; ``` @@ -122,22 +135,20 @@ public: ### **Go** ```go -func countPoints(rings string) int { - mp := make(map[byte]map[byte]bool) - for i := 1; i < len(rings); i += 2 { +func countPoints(rings string) (ans int) { + d := ['Z']int{'R': 1, 'G': 2, 'B': 4} + mask := [10]int{} + for i, n := 0, len(rings); i < n; i += 2 { c := rings[i] - if len(mp[c]) == 0 { - mp[c] = make(map[byte]bool) - } - mp[c][rings[i-1]] = true + j := int(rings[i+1] - '0') + mask[j] |= d[c] } - ans := 0 - for _, v := range mp { - if len(v) == 3 { + for _, x := range mask { + if x == 7 { ans++ } } - return ans + return } ``` @@ -145,14 +156,18 @@ func countPoints(rings string) int { ```ts function countPoints(rings: string): number { - const helper = (c: string) => c.charCodeAt(0) - 'A'.charCodeAt(0); - const n = rings.length; - const target = (1 << helper('R')) + (1 << helper('G')) + (1 << helper('B')); - const count = new Array(10).fill(0); - for (let i = 0; i < n; i += 2) { - count[rings[i + 1]] |= 1 << helper(rings[i]); + const idx = (c: string) => c.charCodeAt(0) - 'A'.charCodeAt(0); + const d: number[] = Array(26).fill(0); + d[idx('R')] = 1; + d[idx('G')] = 2; + d[idx('B')] = 4; + const mask: number[] = Array(10).fill(0); + for (let i = 0; i < rings.length; i += 2) { + const c = rings[i]; + const j = rings[i + 1].charCodeAt(0) - '0'.charCodeAt(0); + mask[j] |= d[idx(c)]; } - return count.reduce((r, v) => (r += v === target ? 1 : 0), 0); + return mask.filter(x => x === 7).length; } ``` @@ -161,16 +176,22 @@ function countPoints(rings: string): number { ```rust impl Solution { pub fn count_points(rings: String) -> i32 { - let rings = rings.as_bytes(); - let target = (1 << b'R' - b'A') + (1 << b'G' - b'A') + (1 << b'B' - b'A'); - let n = rings.len(); - let mut count = [0; 10]; - let mut i = 0; - while i < n { - count[(rings[i + 1] - b'0') as usize] |= 1 << rings[i] - b'A'; - i += 2; + let mut d: [i32; 90] = [0; 90]; + d['R' as usize] = 1; + d['G' as usize] = 2; + d['B' as usize] = 4; + + let mut mask: [i32; 10] = [0; 10]; + + let cs: Vec = rings.chars().collect(); + + for i in (0..cs.len()).step_by(2) { + let c = cs[i] as usize; + let j = cs[i + 1] as usize - '0' as usize; + mask[j] |= d[c]; } - count.iter().filter(|&v| *v == target).count() as i32 + + mask.iter().filter(|&&x| x == 7).count() as i32 } } ``` @@ -179,17 +200,28 @@ impl Solution { ```c int countPoints(char* rings) { - int target = (1 << ('R' - 'A')) + (1 << ('G' - 'A')) + (1 << ('B' - 'A')); - int count[10] = {0}; - for (int i = 0; rings[i]; i += 2) { - count[rings[i + 1] - '0'] |= 1 << (rings[i] - 'A'); + int d['Z']; + memset(d, 0, sizeof(d)); + d['R'] = 1; + d['G'] = 2; + d['B'] = 4; + + int mask[10]; + memset(mask, 0, sizeof(mask)); + + for (int i = 0, n = strlen(rings); i < n; i += 2) { + int c = rings[i]; + int j = rings[i + 1] - '0'; + mask[j] |= d[c]; } + int ans = 0; for (int i = 0; i < 10; i++) { - if (count[i] == target) { + if (mask[i] == 7) { ans++; } } + return ans; } ``` diff --git a/solution/2100-2199/2103.Rings and Rods/Solution.c b/solution/2100-2199/2103.Rings and Rods/Solution.c index 22df38ac62683..47cfaa9e5e772 100644 --- a/solution/2100-2199/2103.Rings and Rods/Solution.c +++ b/solution/2100-2199/2103.Rings and Rods/Solution.c @@ -1,14 +1,25 @@ -int countPoints(char* rings) { - int target = (1 << ('R' - 'A')) + (1 << ('G' - 'A')) + (1 << ('B' - 'A')); - int count[10] = {0}; - for (int i = 0; rings[i]; i += 2) { - count[rings[i + 1] - '0'] |= 1 << (rings[i] - 'A'); - } - int ans = 0; - for (int i = 0; i < 10; i++) { - if (count[i] == target) { - ans++; - } - } - return ans; -} +int countPoints(char* rings) { + int d['Z']; + memset(d, 0, sizeof(d)); + d['R'] = 1; + d['G'] = 2; + d['B'] = 4; + + int mask[10]; + memset(mask, 0, sizeof(mask)); + + for (int i = 0, n = strlen(rings); i < n; i += 2) { + int c = rings[i]; + int j = rings[i + 1] - '0'; + mask[j] |= d[c]; + } + + int ans = 0; + for (int i = 0; i < 10; i++) { + if (mask[i] == 7) { + ans++; + } + } + + return ans; +} diff --git a/solution/2100-2199/2103.Rings and Rods/Solution.cpp b/solution/2100-2199/2103.Rings and Rods/Solution.cpp index 56e01d2b9512c..d561b7ae3108d 100644 --- a/solution/2100-2199/2103.Rings and Rods/Solution.cpp +++ b/solution/2100-2199/2103.Rings and Rods/Solution.cpp @@ -1,15 +1,13 @@ -class Solution { -public: - int countPoints(string rings) { - unordered_map> mp; - for (int i = 1; i < rings.size(); i += 2) { - int c = rings[i] - '0'; - mp[c].insert(rings[i - 1]); - } - int ans = 0; - for (int i = 0; i < 10; ++i) - if (mp[i].size() == 3) - ++ans; - return ans; - } +class Solution { +public: + int countPoints(string rings) { + int d['Z']{['R'] = 1, ['G'] = 2, ['B'] = 4}; + int mask[10]{}; + for (int i = 0, n = rings.size(); i < n; i += 2) { + int c = rings[i]; + int j = rings[i + 1] - '0'; + mask[j] |= d[c]; + } + return count(mask, mask + 10, 7); + } }; \ No newline at end of file diff --git a/solution/2100-2199/2103.Rings and Rods/Solution.go b/solution/2100-2199/2103.Rings and Rods/Solution.go index b67bd7b8815d8..5cb6ba63247df 100644 --- a/solution/2100-2199/2103.Rings and Rods/Solution.go +++ b/solution/2100-2199/2103.Rings and Rods/Solution.go @@ -1,17 +1,15 @@ -func countPoints(rings string) int { - mp := make(map[byte]map[byte]bool) - for i := 1; i < len(rings); i += 2 { +func countPoints(rings string) (ans int) { + d := ['Z']int{'R': 1, 'G': 2, 'B': 4} + mask := [10]int{} + for i, n := 0, len(rings); i < n; i += 2 { c := rings[i] - if len(mp[c]) == 0 { - mp[c] = make(map[byte]bool) - } - mp[c][rings[i-1]] = true + j := int(rings[i+1] - '0') + mask[j] |= d[c] } - ans := 0 - for _, v := range mp { - if len(v) == 3 { + for _, x := range mask { + if x == 7 { ans++ } } - return ans + return } \ No newline at end of file diff --git a/solution/2100-2199/2103.Rings and Rods/Solution.java b/solution/2100-2199/2103.Rings and Rods/Solution.java index 221099ed1dcef..f1c2d529c9dda 100644 --- a/solution/2100-2199/2103.Rings and Rods/Solution.java +++ b/solution/2100-2199/2103.Rings and Rods/Solution.java @@ -1,16 +1,21 @@ -class Solution { - public int countPoints(String rings) { - Map> mp = new HashMap<>(); - for (int i = 1; i < rings.length(); i += 2) { - int c = rings.charAt(i) - '0'; - mp.computeIfAbsent(c, k -> new HashSet<>()).add(rings.charAt(i - 1)); - } - int ans = 0; - for (Set e : mp.values()) { - if (e.size() == 3) { - ++ans; - } - } - return ans; - } +class Solution { + public int countPoints(String rings) { + int[] d = new int['Z']; + d['R'] = 1; + d['G'] = 2; + d['B'] = 4; + int[] mask = new int[10]; + for (int i = 0, n = rings.length(); i < n; i += 2) { + int c = rings.charAt(i); + int j = rings.charAt(i + 1) - '0'; + mask[j] |= d[c]; + } + int ans = 0; + for (int x : mask) { + if (x == 7) { + ++ans; + } + } + return ans; + } } \ No newline at end of file diff --git a/solution/2100-2199/2103.Rings and Rods/Solution.py b/solution/2100-2199/2103.Rings and Rods/Solution.py index 4d479e8b8e492..3acf68f2620b4 100644 --- a/solution/2100-2199/2103.Rings and Rods/Solution.py +++ b/solution/2100-2199/2103.Rings and Rods/Solution.py @@ -1,7 +1,9 @@ -class Solution: - def countPoints(self, rings: str) -> int: - mp = defaultdict(set) - for i in range(1, len(rings), 2): - c = int(rings[i]) - mp[c].add(rings[i - 1]) - return sum(len(v) == 3 for v in mp.values()) +class Solution: + def countPoints(self, rings: str) -> int: + mask = [0] * 10 + d = {"R": 1, "G": 2, "B": 4} + for i in range(0, len(rings), 2): + c = rings[i] + j = int(rings[i + 1]) + mask[j] |= d[c] + return mask.count(7) diff --git a/solution/2100-2199/2103.Rings and Rods/Solution.rs b/solution/2100-2199/2103.Rings and Rods/Solution.rs index 0782356be9e4c..6196aea8f7483 100644 --- a/solution/2100-2199/2103.Rings and Rods/Solution.rs +++ b/solution/2100-2199/2103.Rings and Rods/Solution.rs @@ -1,14 +1,20 @@ -impl Solution { - pub fn count_points(rings: String) -> i32 { - let rings = rings.as_bytes(); - let target = (1 << b'R' - b'A') + (1 << b'G' - b'A') + (1 << b'B' - b'A'); - let n = rings.len(); - let mut count = [0; 10]; - let mut i = 0; - while i < n { - count[(rings[i + 1] - b'0') as usize] |= 1 << rings[i] - b'A'; - i += 2; - } - count.iter().filter(|&v| *v == target).count() as i32 - } -} +impl Solution { + pub fn count_points(rings: String) -> i32 { + let mut d: [i32; 90] = [0; 90]; + d['R' as usize] = 1; + d['G' as usize] = 2; + d['B' as usize] = 4; + + let mut mask: [i32; 10] = [0; 10]; + + let cs: Vec = rings.chars().collect(); + + for i in (0..cs.len()).step_by(2) { + let c = cs[i] as usize; + let j = cs[i + 1] as usize - '0' as usize; + mask[j] |= d[c]; + } + + mask.iter().filter(|&&x| x == 7).count() as i32 + } +} \ No newline at end of file diff --git a/solution/2100-2199/2103.Rings and Rods/Solution.ts b/solution/2100-2199/2103.Rings and Rods/Solution.ts index 635fe966d9cc2..7844cf2c37cf2 100644 --- a/solution/2100-2199/2103.Rings and Rods/Solution.ts +++ b/solution/2100-2199/2103.Rings and Rods/Solution.ts @@ -1,10 +1,14 @@ function countPoints(rings: string): number { - const helper = (c: string) => c.charCodeAt(0) - 'A'.charCodeAt(0); - const n = rings.length; - const target = (1 << helper('R')) + (1 << helper('G')) + (1 << helper('B')); - const count = new Array(10).fill(0); - for (let i = 0; i < n; i += 2) { - count[rings[i + 1]] |= 1 << helper(rings[i]); + const idx = (c: string) => c.charCodeAt(0) - 'A'.charCodeAt(0); + const d: number[] = Array(26).fill(0); + d[idx('R')] = 1; + d[idx('G')] = 2; + d[idx('B')] = 4; + const mask: number[] = Array(10).fill(0); + for (let i = 0; i < rings.length; i += 2) { + const c = rings[i]; + const j = rings[i + 1].charCodeAt(0) - '0'.charCodeAt(0); + mask[j] |= d[idx(c)]; } - return count.reduce((r, v) => (r += v === target ? 1 : 0), 0); + return mask.filter(x => x === 7).length; }