Solution: Hashset
Use 10 hashsets to track the status of each rod, check whether it contains three unique elements (R,G,B).
Time complexity: O(n)
Space complexity: O(10*3)
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// Author: Huahua class Solution { public: int countPoints(string rings) { constexpr int kRods = 10; vector<unordered_set<char>> s(kRods); for (int i = 0; i < rings.size(); i += 2) s[rings[i + 1] - '0'].insert(rings[i]); int ans = 0; for (int i = 0; i < kRods; ++i) if (s[i].size() == 3) ++ans; return ans; } }; |
请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.
Be First to Comment