3115. 质数的最大距离
题目链接:3115. 质数的最大距离
代码如下:
class Solution
{
public:
int maximumPrimeDifference(vector<int>& nums)
{
int dist = 0;
int left, right;
for (left = 0; left < nums.size(); left++)
{
if (isPrime(nums[left])) { break; }
}
for (right = nums.size() - 1; right >= 0; right--)
{
if (isPrime(nums[right])) { break; }
}
return right - left;
}
private:
bool isPrime(int num)
{
for (int i = 2; i * i <= num; i++)
{
if (num % i == 0) { return false; }
}
return num >= 2;
}
};