1.3238. 求出胜利玩家的数目 - 力扣(LeetCode)
这是今天力扣的每日一题
class Solution {
public:
int winningPlayerCount(int n, vector<vector<int>>& pick) {
vector<unordered_map<int, int>> vv(n);
for (auto& e : pick) {
vv[e[0]][e[1]]++;
}
int ret = 0;
for (int i = 0; i < n; i++) {
for (auto& e : vv[i])
if (e.second > i) {
ret++;
break;
}
}
return ret;
}
};
2.2122. 还原原数组 - 力扣(LeetCode)
class Solution {
public:
vector<int> recoverArray(vector<int>& nums) {
sort(nums.begin(), nums.end());
int left = 0, nn = nums.size(), n = nn / 2;
for (int right = 1; right * 2 <= nn; right++) {
int len = nums[right] - nums[left];
if (len % 2 == 1 || len == 0) continue;
vector<bool> vb(nn, false);
vector<int> ret;
auto fun = [&](int left, int right, auto& f) {
while (vb[left] == true)
left++;
vb[left] = true;
while (nums[right] < nums[left] + len)
{
right++;
if (right == nn) return false;
}
if (nums[right] != nums[left] + len)
return false;
vb[right] = true; // 相等
ret.push_back(nums[left] + len / 2);
if (right == nn - 1){
if(ret.size() != n)
return false;
return true;
}
else {
return f(left + 1, right + 1, f);
}
};
if (fun(left, right, fun)) {
// cout << left << ' ' << right << endl;
return ret;
}
}
return { 1, 2, 3 };
}
};
3.1793. 好子数组的最大分数 - 力扣(LeetCode)
class Solution {
public:
int maximumScore(vector<int>& nums, int k) {
int n = nums.size(), ans = nums[k];
int minnum = nums[k], left = k, right = k;
for (int t = 0; t < n - 1; t++) {
if (right == n - 1 || left && nums[left - 1] > nums[right + 1]) {
minnum = min(nums[--left], minnum);
} else {
minnum = min(nums[++right], minnum);
}
ans = max(ans, minnum * (right - left + 1));
}
return ans;
}
};
4.27. 移除元素 - 力扣(LeetCode)
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int left = 0, right = nums.size() - 1, ans = 0;
while (left <= right) {
while (left < right&& nums[right] == val)
right--,ans++;
nums[left] == val ? swap(nums[left++] ,nums[right--]), ans++ : left++;
}
return nums.size() - ans;
}
};
5.26. 删除有序数组中的重复项 - 力扣(LeetCode)
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int left = 1,right = 1,ans = 0;
while(right < nums.size()){
if(nums[right] != nums[right - 1]){
nums[left++] = nums[right];
}
right++;
}
return left;
}
};
6.80. 删除有序数组中的重复项 II - 力扣(LeetCode)
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int left = 0,right = 0,ans = 0;
for(;right < nums.size();right++)
{
if(left < 2 || nums[left - 2] != nums[right])
{
nums[left++] = nums[right];
}
}
return left;
}
};
7.283. 移动零 - 力扣(LeetCode)
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int i0 = 0;
for (auto& x : nums) {
if (x) {
swap(x, nums[i0]);
i0++;
}
}
}
};
8.905. 按奇偶排序数组 - 力扣(LeetCode)
class Solution {
public:
vector<int> sortArrayByParity(vector<int>& nums) {
int to = 0;
for (auto& x : nums) {
if (x%2 == 0) {
swap(x, nums[to]);
to++;
}
}
return nums;
}
};
9.922. 按奇偶排序数组 II - 力扣(LeetCode)
class Solution {
public:
vector<int> sortArrayByParityII(vector<int>& nums) {
int j = 1, n = nums.size();
for (int i = 0; i < nums.size();) {
while (i < n && nums[i] % 2 == 0)
i += 2;
while (j < n && nums[j] % 2 == 1)
j += 2;
if (i < n && j < n)
swap(nums[i], nums[j]);
else
break;
}
return nums;
}
};
102460. 对数组执行操作 - 力扣(LeetCode)
class Solution {
public:
vector<int> applyOperations(vector<int>& nums) {
int n = nums.size();
for (int i = 0; i < n - 1; i++) {
if (nums[i] == nums[i + 1]) {
nums[i] *=2, nums[i + 1] = 0;
}
}
int left = 0;
for (auto& x : nums) {
if (x) {
swap(nums[left++], x);
}
}
return nums;
}
};
11.1089. 复写零 - 力扣(LeetCode)
class Solution {
public:
void duplicateZeros(vector<int>& arr) {
int left = 0,n = arr.size(),right = n-1;
for(;left < right;left++){
if(arr[left] == 0)
right--;
}
int end = n - 1;
if(left == right && arr[left] == 0)
arr[end--] = 0,right--;
for(;right>=0;right--){
arr[end--] = arr[right];
if(arr[right] == 0)
{
arr[end--] = 0;
}
}
}
};
// 1 0 3 0 4 0 0
// 1 0 0 3 0 0 4