LeetCode 2761. 和等于目标值的质数对 C#
原题链接
中等
作者:
hpstory
,
2023-07-03 14:24:57
,
所有人可见
,
阅读 102
C# 代码
public class Solution {
private static bool[] nums;
private static HashSet<int> primes;
static Solution(){
int m = 1000010;
nums = new bool[m];
primes = new HashSet<int>();
for (int i = 2; i < m / i; i++){
if (!nums[i]){
for (int j = i + i; j < m; j += i){
nums[j] = true;
}
}
}
for (int i = 2; i < m; i++){
if (!nums[i]) primes.Add(i);
}
}
public IList<IList<int>> FindPrimePairs(int n) {
List<IList<int>> result = new List<IList<int>>();
for (int i = 1; i <= n / 2; i++){
if (primes.Contains(i) && primes.Contains(n - i)){
result.Add(new int[2] {i, n - i});
}
}
return result;
}
}