11-23刷题记录

1.3238. 求出胜利玩家的数目 - 力扣(LeetCode)

这是今天力扣的每日一题

cpp 复制代码
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)

cpp 复制代码
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)

cpp 复制代码
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)

cpp 复制代码
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)

cpp 复制代码
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)

cpp 复制代码
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)

cpp 复制代码
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)

cpp 复制代码
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)

cpp 复制代码
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)

cpp 复制代码
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)

cpp 复制代码
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
相关推荐
AAA修煤气灶刘哥17 分钟前
明明搜到了同一个故障码,RAG 为什么还会教错你?
算法·llm·agent
龙砚序25 分钟前
【数据结构|线性表实战 01】基于顺序表的地铁站点管理:查找、增删、有序二路归并(C 语言实现)
c语言·数据结构·算法
灯澜忆梦33 分钟前
拓扑排序(Kahn BFS)| LeetCode 210 课程表 II
算法·leetcode·排序算法·宽度优先
极客先躯41 分钟前
智能体开发高级面试每日一题|2026-05-23|什么是 AI Agent?它和普通 LLM 应用有什么区别?
人工智能·面试·职场和发展·高级面试·智能体开发
Rabitebla1 小时前
【Linux】文件系统(四):目录、路径解析与 dentry 缓存
linux·数据结构·c++·笔记·学习·算法·缓存
kiracrimson2 小时前
最短路径:Dijkstra 与 Bellman-Ford、Floyd 各自的边界
数据结构·c++·算法·最短路径·c++学习
一座城和一个人2 小时前
2026年复旦大学计算机考研复试机试真题解析
数据结构·考研·算法·考研机试·复试上机
Nuanyt2 小时前
LeetCode Hot 100 贪心算法专题 121 / 55 / 45 / 763 (Java)
java·算法·leetcode·贪心算法·dp
AI的探索之旅3 小时前
OpenCV Day48 —— SVM 手写数字:HOG 与 Hellinger 核
算法·机器学习·支持向量机
金榜题名萱3 小时前
蓝桥杯 - 拍照 —— 动态规划
c语言·数据结构·c++·算法·职场和发展·蓝桥杯·动态规划