Title: "[[biweekly-contest-133]]"
Contest: leetcode
status: DONE
tags: Contest
Rank: 769
Total: 2326
Credits: 12
T1: Accepted
T2: Accepted
T3: Accepted
T4: Uncommitted
Author: AllenYGY
统计不能被 0 整除的数的数量
class Solution {
public:
int minimumOperations(vector<int>& nums) {
int ans=0;
for(int i=0;i<nums.size();i++){
if(nums[i]%3!=0)
ans+=1;
}
return ans;
}
};
#贪心
从左至右遍历逐个检查
class Solution {
public:
int minOperations(vector<int>& nums) {
int n = nums.size();
int count = 0;
for (int i = 0; i <= n - 3; ++i) {
if (nums[i] == 0) {
nums[i] = 1 - nums[i];
nums[i + 1] = 1 - nums[i + 1];
nums[i + 2] = 1 - nums[i + 2];
count++;
cout<<i<<" ";
}
}
if (nums[n - 2] == 0 || nums[n - 1] == 0) {
return -1;
}
return count;
}
};
#贪心
从左至右,每发现一个需要翻转的数
翻转次数+1
翻转状态翻转(因为翻转1次,后面所有的都会翻转)
class Solution {
public:
int minOperations(vector<int>& nums) {
int flips = 0;
int n = nums.size();
bool shouldBeOne = false;
for (int i = 0; i < n; ++i) {
if (nums[i] == shouldBeOne) {
flips++;
shouldBeOne = !shouldBeOne;
}
}
return flips;
}
};
#DP
看了题解还是不会