|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: 简单 |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3591.Check%20if%20Any%20Element%20Has%20Prime%20Frequency/README.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [3591. 检查元素频次是否为质数](https://leetcode.cn/problems/check-if-any-element-has-prime-frequency) |
| 10 | + |
| 11 | +[English Version](/solution/3500-3599/3591.Check%20if%20Any%20Element%20Has%20Prime%20Frequency/README_EN.md) |
| 12 | + |
| 13 | +## 题目描述 |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<p>给你一个整数数组 <code>nums</code>。</p> |
| 18 | + |
| 19 | +<p>如果数组中任一元素的 <strong>频次 </strong>是 <strong>质数</strong>,返回 <code>true</code>;否则,返回 <code>false</code>。</p> |
| 20 | + |
| 21 | +<p>元素 <code>x</code> 的 <strong>频次 </strong>是它在数组中出现的次数。</p> |
| 22 | + |
| 23 | +<p>质数是一个大于 1 的自然数,并且只有两个因数:1 和它本身。</p> |
| 24 | + |
| 25 | +<p> </p> |
| 26 | + |
| 27 | +<p><strong class="example">示例 1:</strong></p> |
| 28 | + |
| 29 | +<div class="example-block"> |
| 30 | +<p><strong>输入:</strong> <span class="example-io">nums = [1,2,3,4,5,4]</span></p> |
| 31 | + |
| 32 | +<p><strong>输出:</strong> <span class="example-io">true</span></p> |
| 33 | + |
| 34 | +<p><strong>解释:</strong></p> |
| 35 | + |
| 36 | +<p>数字 4 的频次是 2,而 2 是质数。</p> |
| 37 | +</div> |
| 38 | + |
| 39 | +<p><strong class="example">示例 2:</strong></p> |
| 40 | + |
| 41 | +<div class="example-block"> |
| 42 | +<p><strong>输入:</strong> <span class="example-io">nums = [1,2,3,4,5]</span></p> |
| 43 | + |
| 44 | +<p><strong>输出:</strong> <span class="example-io">false</span></p> |
| 45 | + |
| 46 | +<p><strong>解释:</strong></p> |
| 47 | + |
| 48 | +<p>所有元素的频次都是 1。</p> |
| 49 | +</div> |
| 50 | + |
| 51 | +<p><strong class="example">示例 3:</strong></p> |
| 52 | + |
| 53 | +<div class="example-block"> |
| 54 | +<p><strong>输入:</strong> <span class="example-io">nums = [2,2,2,4,4]</span></p> |
| 55 | + |
| 56 | +<p><strong>输出:</strong> <span class="example-io">true</span></p> |
| 57 | + |
| 58 | +<p><strong>解释:</strong></p> |
| 59 | + |
| 60 | +<p>数字 2 和 4 的频次都是质数。</p> |
| 61 | +</div> |
| 62 | + |
| 63 | +<p> </p> |
| 64 | + |
| 65 | +<p><strong>提示:</strong></p> |
| 66 | + |
| 67 | +<ul> |
| 68 | + <li><code>1 <= nums.length <= 100</code></li> |
| 69 | + <li><code>0 <= nums[i] <= 100</code></li> |
| 70 | +</ul> |
| 71 | + |
| 72 | +<!-- description:end --> |
| 73 | + |
| 74 | +## 解法 |
| 75 | + |
| 76 | +<!-- solution:start --> |
| 77 | + |
| 78 | +### 方法一:计数 + 判断质数 |
| 79 | + |
| 80 | +我们用一个哈希表 $\text{cnt}$ 统计每个元素的频次。然后遍历 $\text{cnt}$ 中的值,判断是否有质数,如果有则返回 `true`,否则返回 `false`。 |
| 81 | + |
| 82 | +时间复杂度 $O(n \times \sqrt{M})$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\text{nums}$ 的长度,而 $M$ 是 $\text{cnt}$ 中的最大值。 |
| 83 | + |
| 84 | +<!-- tabs:start --> |
| 85 | + |
| 86 | +#### Python3 |
| 87 | + |
| 88 | +```python |
| 89 | +class Solution: |
| 90 | + def checkPrimeFrequency(self, nums: List[int]) -> bool: |
| 91 | + def is_prime(x: int) -> bool: |
| 92 | + if x < 2: |
| 93 | + return False |
| 94 | + return all(x % i for i in range(2, int(sqrt(x)) + 1)) |
| 95 | + |
| 96 | + cnt = Counter(nums) |
| 97 | + return any(is_prime(x) for x in cnt.values()) |
| 98 | +``` |
| 99 | + |
| 100 | +#### Java |
| 101 | + |
| 102 | +```java |
| 103 | +import java.util.*; |
| 104 | + |
| 105 | +class Solution { |
| 106 | + public boolean checkPrimeFrequency(int[] nums) { |
| 107 | + Map<Integer, Integer> cnt = new HashMap<>(); |
| 108 | + for (int x : nums) { |
| 109 | + cnt.merge(x, 1, Integer::sum); |
| 110 | + } |
| 111 | + |
| 112 | + for (int x : cnt.values()) { |
| 113 | + if (isPrime(x)) { |
| 114 | + return true; |
| 115 | + } |
| 116 | + } |
| 117 | + return false; |
| 118 | + } |
| 119 | + |
| 120 | + private boolean isPrime(int x) { |
| 121 | + if (x < 2) { |
| 122 | + return false; |
| 123 | + } |
| 124 | + for (int i = 2; i <= x / i; i++) { |
| 125 | + if (x % i == 0) { |
| 126 | + return false; |
| 127 | + } |
| 128 | + } |
| 129 | + return true; |
| 130 | + } |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +#### C++ |
| 135 | + |
| 136 | +```cpp |
| 137 | +class Solution { |
| 138 | +public: |
| 139 | + bool checkPrimeFrequency(vector<int>& nums) { |
| 140 | + unordered_map<int, int> cnt; |
| 141 | + for (int x : nums) { |
| 142 | + ++cnt[x]; |
| 143 | + } |
| 144 | + |
| 145 | + for (auto& [_, x] : cnt) { |
| 146 | + if (isPrime(x)) { |
| 147 | + return true; |
| 148 | + } |
| 149 | + } |
| 150 | + return false; |
| 151 | + } |
| 152 | + |
| 153 | +private: |
| 154 | + bool isPrime(int x) { |
| 155 | + if (x < 2) { |
| 156 | + return false; |
| 157 | + } |
| 158 | + for (int i = 2; i <= x / i; ++i) { |
| 159 | + if (x % i == 0) { |
| 160 | + return false; |
| 161 | + } |
| 162 | + } |
| 163 | + return true; |
| 164 | + } |
| 165 | +}; |
| 166 | +``` |
| 167 | +
|
| 168 | +#### Go |
| 169 | +
|
| 170 | +```go |
| 171 | +func checkPrimeFrequency(nums []int) bool { |
| 172 | + cnt := make(map[int]int) |
| 173 | + for _, x := range nums { |
| 174 | + cnt[x]++ |
| 175 | + } |
| 176 | + for _, x := range cnt { |
| 177 | + if isPrime(x) { |
| 178 | + return true |
| 179 | + } |
| 180 | + } |
| 181 | + return false |
| 182 | +} |
| 183 | +
|
| 184 | +func isPrime(x int) bool { |
| 185 | + if x < 2 { |
| 186 | + return false |
| 187 | + } |
| 188 | + for i := 2; i*i <= x; i++ { |
| 189 | + if x%i == 0 { |
| 190 | + return false |
| 191 | + } |
| 192 | + } |
| 193 | + return true |
| 194 | +} |
| 195 | +``` |
| 196 | + |
| 197 | +#### TypeScript |
| 198 | + |
| 199 | +```ts |
| 200 | +function checkPrimeFrequency(nums: number[]): boolean { |
| 201 | + const cnt: Record<number, number> = {}; |
| 202 | + for (const x of nums) { |
| 203 | + cnt[x] = (cnt[x] || 0) + 1; |
| 204 | + } |
| 205 | + for (const x of Object.values(cnt)) { |
| 206 | + if (isPrime(x)) { |
| 207 | + return true; |
| 208 | + } |
| 209 | + } |
| 210 | + return false; |
| 211 | +} |
| 212 | + |
| 213 | +function isPrime(x: number): boolean { |
| 214 | + if (x < 2) { |
| 215 | + return false; |
| 216 | + } |
| 217 | + for (let i = 2; i * i <= x; i++) { |
| 218 | + if (x % i === 0) { |
| 219 | + return false; |
| 220 | + } |
| 221 | + } |
| 222 | + return true; |
| 223 | +} |
| 224 | +``` |
| 225 | + |
| 226 | +<!-- tabs:end --> |
| 227 | + |
| 228 | +<!-- solution:end --> |
| 229 | + |
| 230 | +<!-- problem:end --> |
0 commit comments