news 2026/4/16 9:18:40

计数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/16 1:21:59

如何在Spring Boot项目中实现高效数据翻译框架

如何在Spring Boot项目中实现高效数据翻译框架 【免费下载链接】easy-trans easy-trans是一个数据翻译组件&#xff0c;开发者可以通过一个注解将vo中的id翻译为title、name&#xff1b;可以将字典码sex 1翻译为男/女。支持缓存、微服务等各种各样的有趣玩法。 项目地址: htt…

作者头像 李华
网站建设 2026/4/15 8:50:46

不愧是京东大牛手码的“redis 深度笔记”从基础到源码应有尽有

写在前面 Redis&#xff08;Remote DIctionary Server&#xff09;作为一个开源/C实现/高性能/基于内存的key-value存储系统&#xff0c;相信做Java的小伙伴都不会陌生。Redis常用于缓存、分布式锁、队列(或有序集合)等场景&#xff0c;追求技术的小伙伴们肯定不只满足于Redis…

作者头像 李华
网站建设 2026/4/8 22:58:15

汇川H3U PLC控制走CANLink带触摸屏完整程序实战分享

汇川H3UPLC控制走CANLink带触摸屏完整程序 汇川H3U程序 1.实际工程应用程序&#xff0c;稳定运行&#xff1b; 2.带3个步进电机16个私服一共19个轴&#xff1b; 3.包含系统控制程序&#xff0c;报警程序&#xff0c;气缸操作&#xff0c;当班产能&#xff0c;历史产能&#xff…

作者头像 李华
网站建设 2026/4/16 1:01:32

如何快速搭建专业文件目录网站:Directory Lister完整指南

如何快速搭建专业文件目录网站&#xff1a;Directory Lister完整指南 【免费下载链接】DirectoryLister &#x1f4c2; Directory Lister is the easiest way to expose the contents of any web-accessible folder for browsing and sharing. 项目地址: https://gitcode.com…

作者头像 李华
网站建设 2026/4/15 9:16:23

Wan2.2-T2V-A14B支持多人物角色协同动作生成吗?

Wan2.2-T2V-A14B支持多人物角色协同动作生成吗&#xff1f; 你有没有遇到过这样的场景&#xff1a;想让AI生成一段“两位商务人士在会议室握手”的视频&#xff0c;结果出来的画面要么是两人动作不同步&#xff0c;要么直接穿模、漂移&#xff0c;甚至手都没碰上——尴尬得像在…

作者头像 李华
网站建设 2026/4/16 9:00:54

如何快速掌握FATE:分布式隐私计算的完整入门指南

如何快速掌握FATE&#xff1a;分布式隐私计算的完整入门指南 【免费下载链接】FATE 项目地址: https://gitcode.com/gh_mirrors/fat/FATE 在数据安全和隐私保护日益重要的今天&#xff0c;FATE&#xff08;Federated AI Technology Enabler&#xff09;作为一个开源的分…

作者头像 李华