质数专题
我的思路就是一个素数筛,然后双指针
class Solution {
public:int maximumPrimeDifference(vector<int>& nums) {unordered_map<int, int> mp;for (int i = 2; i <= 100; i++) {if (mp[i] == 0) {for (int j = 2 * i; j <= 100; j += i) {mp[j] = 1;}}}mp[1] = 1;int l = 0, r = nums.size()-1;int flag = 0;while (l != r) {flag = 0;if (mp[nums[l]]) l++,flag = 1;if (mp[nums[r]]) r--,flag = 1;if (!flag) break;}return r-l;}
};
leecode2521
我们可以构造一个质因子的序列(因为数据比较小)
class Solution {
public:int distinctPrimeFactors(vector<int>& nums) {vector<int> record[1005];// 预处理for (int i = 2; i <= 1000; i++) {if (record[i].size() == 0) {for (int j = 2 * i; j <= 1000; j++) {record[j].push_back(i);}}}vector<int> vis(1005);int ans = 0;for (int u : nums) {if (record[u].size() == 0) {if (vis[u] == 0) {ans++; vis[u] = 1;}}else {for (int r : record[u]) {if (vis[r] == 0) {ans++, vis[r] = 1;}}}}return ans;}
};
阶乘
leecode阶乘后的零
class Solution {
public:int trailingZeroes(int n) {int ans = 0;for (int i = 5; i <= n; i += 5) {for (int x = i; x % 5 == 0; x /= 5) {++ans;}}return ans;}
};
leecode793阶乘函数后k个零
这一题相较与上一题是一个进阶的题目,如果用暴力是肯定不能过关的