news 2026/4/23 13:45:57

计数if|

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
计数if|

lc2593

1.mask ll也会溢出 转vec bool

2.sort pii(nums,idx) 后标记自身 &左右

一次遍历即可

class Solution {
typedef long long ll;
public:
long long findScore(vector<int>& nums) {
ll ret = 0;
int n = nums.size();
if (n == 1) return nums[0];

vector<bool> mask(n, false);

// 1. 元素按“值+下标”排序
vector<pair<int, int>> arr;
for (int i = 0; i < n; ++i)
arr.emplace_back(nums[i], i);
sort(arr.begin(), arr.end());

// 2. 按排序处理,用mask标记
for (auto& [val, idx] : arr)

{
if (!mask[idx]) { // 当前元素未标记
ret += val;
mask[idx] = true; // 标记自身
if (idx > 0) mask[idx - 1] = true; // 标记左邻
if (idx < n - 1) mask[idx + 1] = true; // 标记右邻
}
}
return ret;
}
};

lc2155

presum预处理

class Solution {
public:
vector<int> maxScoreIndices(vector<int>& nums)
{
int n = nums.size();
vector<int> pre(n + 1);
for (int i = 1; i <= n; ++i)
pre[i] = pre[i-1] + nums[i-1];

int max_s = 0;
vector<int> res;

for (int i = 0; i <= n; ++i) {
int l0 = i - pre[i];
int r1 = pre[n] - pre[i];
int s = l0 + r1;

if (s > max_s) {
max_s = s;
res = {i};
}
else if (s == max_s)
res.push_back(i);
}
return res;
}
};

lc2216

模拟栈

维护一个有效序列,遇到“偶数长度序列末尾元素和当前元素相同”的情况就替换末尾元素,最后保证序列是偶数长度

原数组长度减去有效长度得到最少删除次数

class Solution {
public:
int minDeletion(vector<int>& nums) {
vector<int> ans;
for (int num : nums) {
if (ans.size() > 0 &&num == ans.back() && ans.size() % 2 == 1)
continue;//ignore
else
ans.push_back(num);
}
int len = ans.size() % 2 == 0 ? ans.size() : ans.size() - 1;
return nums.size() - len;
}
};

lc2038

统计连续相同颜色的长度

class Solution {
public:
bool winnerOfGame(string colors)
{
int n = colors.size();
if (n < 3) return false;

int alice = 0, bob = 0;
int count = 1;
// 记录当前连续相同字符的长度

for (int i = 1; i < n; ++i) {
if (colors[i] == colors[i-1])
count++;//相等_计数
else
{ // 不等_统计上一段连续字符的可op
if (colors[i-1] == 'A')
alice += max(0, count - 2);
else
bob += max(0, count - 2);
count = 1;//重置
}
}
// 处理最后一段连续字符
if (colors.back() == 'A')

alice += max(0, count - 2);
else
bob += max(0, count - 2);

return alice > bob;
}
};

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/23 10:44:23

Wan2.2-T2V-A14B在AI编剧+视频自动生成闭环中的角色

Wan2.2-T2V-A14B&#xff1a;当AI编剧遇上视频生成&#xff0c;闭环来了 &#x1f3ac;✨ 你有没有想过—— 只需要一句话&#xff1a;“一个穿红斗篷的女孩在秋日森林奔跑&#xff0c;阳光穿过树叶洒下斑驳光影”&#xff0c;下一秒&#xff0c;这段画面就真的动起来了&#x…

作者头像 李华
网站建设 2026/4/22 19:23:46

PDown百度网盘下载器2025终极指南:突破限速的免费解决方案

PDown百度网盘下载器2025终极指南&#xff1a;突破限速的免费解决方案 【免费下载链接】pdown 百度网盘下载器&#xff0c;2020百度网盘高速下载 项目地址: https://gitcode.com/gh_mirrors/pd/pdown 在当今数字化时代&#xff0c;百度网盘作为国内主流的云存储平台&…

作者头像 李华
网站建设 2026/4/23 12:10:45

跨平台应用分发终极指南:从开发到部署的完整方案

跨平台应用分发终极指南&#xff1a;从开发到部署的完整方案 【免费下载链接】upscayl &#x1f199; Upscayl - Free and Open Source AI Image Upscaler for Linux, MacOS and Windows built with Linux-First philosophy. 项目地址: https://gitcode.com/GitHub_Trending/…

作者头像 李华
网站建设 2026/4/23 10:43:55

MyFlash数据库回滚工具:轻松实现MySQL数据恢复的终极指南

MyFlash数据库回滚工具&#xff1a;轻松实现MySQL数据恢复的终极指南 【免费下载链接】MyFlash flashback mysql data to any point 项目地址: https://gitcode.com/gh_mirrors/my/MyFlash 在数据库运维过程中&#xff0c;误操作导致的数据丢失是每个开发者都可能面临的…

作者头像 李华
网站建设 2026/4/23 13:35:29

三星设备固件管理新方案:Bifrost跨平台工具实战指南

在三星设备的使用过程中&#xff0c;固件管理往往是让用户头疼的问题。传统方式需要记忆复杂的命令行参数&#xff0c;在不同平台间切换时更是困难重重。现在&#xff0c;有了Bifrost工具&#xff0c;这一切都变得简单直观。这款跨平台应用彻底改变了三星固件的管理方式&#x…

作者头像 李华
网站建设 2026/4/18 12:15:59

基于Java+ vue宠物寄养系统(源码+数据库+文档)

宠物寄养系统 目录 基于springboot vue宠物寄养系统 一、前言 二、系统功能演示 详细视频演示 三、技术选型 四、其他项目参考 五、代码参考 六、测试参考 七、最新计算机毕设选题推荐 八、源码获取&#xff1a; 基于springboot vue宠物寄养系统 一、前言 博主介绍…

作者头像 李华