模拟|str_dp

lc2405

hash

class Solution {

public:

int partitionString(string s)

{

unordered_set<char> set;

int ret = 0;

for(auto& c : s)

{

++if(set.count(c))++

{

ret++;

set.clear();

}

set.insert(c);

}

++ret += 1;//last++

return ret;

}

};

mask位运算优化set

++if(mask & (1 << bit))++ // 查1

++mask |= (1 << bit); //置1++

class Solution {

public:

int partitionString(string s)

{

int mask = 0; // 用二进制位标记字符是否出现:第i位为1表示'a'+i已存在

int ret = 0;

for(auto& c : s)

{

int bit = c - 'a';

++if(mask & (1 << bit))++ // 查1

{

ret += 1;

mask = 0; // 重置mask

}

++mask |= (1 << bit); //置1++

}

ret += 1;

return ret;

}

};

lc3531

预处理统计每个x行的y最值、y列的x最值

若建筑的x、y都在对应行列最值中间,就是被覆盖的,计数返回。

class Solution {

public:

int countCoveredBuildings(int n, vector<vector<int>>& buildings)

{

//++预处理 行位置极值和列位置极值++

unordered_map<int,int> row_min,row_max;

unordered_map<int,int> col_min,col_max;

for(auto& b:buildings)

{

int x = b0, y = b1;

// 预处理行极值

if (!row_min.count(x) || y < row_minx) row_minx = y;

if (!row_max.count(x) || y > row_maxx) row_maxx = y;

// 预处理列极值

if (!col_min.count(y) || x < col_miny) col_miny = x;

if (!col_max.count(y) || x > col_maxy) col_maxy = x;

}

int ans=0;

for(auto& b:buildings)

{

int x=b0,y=b1;

bool up=(y<row_maxx);

bool down=(y>row_minx);

bool left=(x>col_miny);

bool right=(x<col_maxy);

++if(up && down && left && right) ans++;++

}

return ans;

}

};

lc16.21

找到去掉差值的一半

class Solution {

/*

输入:array1 = 4, 1, 2, 1, 1, 2, array2 = 3, 6, 3, 3

输出:1, 3

*/

public:

vector<int> findSwapValues(vector<int>& array1, vector<int>& array2)

{

unordered_map<int,int> hash;

int sum1=0,sum2=0;

int m=array1.size(),n=array2.size();

for(int i=0;i<n;i++)

{

int a=array2i;

sum2+=a;

hasha=i;;

}

for(auto& a:array1)

sum1+=a;

int d=sum1-sum2;

if(d%2) return {};

for(int i=0;i<m;i++)

{

int a=array1i;

++int f=a-d/2;
if(hash.count(f))
++

return {a,f};

}

return {};

}

};

相关推荐
noipp6 分钟前
推荐题目:洛谷 P6231 [JSOI2013] 公交系统
c语言·数据结构·c++·算法·游戏·洛谷·luogu
c2385620 分钟前
C/C++每日一练6
c语言·c++·算法
AI小码1 小时前
把动作「画」给视频世界模型,跨本体双向推演,李飞飞参与
大数据·人工智能·算法·ai·大模型·音视频·编程
KaMeidebaby2 小时前
卡梅德生物技术快报|bli亲和力检测gst:告别批量跑胶:BLI实时酶切监测技术加速GST融合蛋白下游流程优化
前端·网络·数据库·人工智能·算法
alphaTao3 小时前
LeetCode 每日一题 2026/7/20-2026/7/26
算法·leetcode
青山木6 小时前
Hot 100 ---腐烂的橘子
java·数据结构·后端·算法·leetcode·广度优先
未来之窗软件服务6 小时前
计算机考试-快速排序—东方仙盟
数据结构·算法·排序算法·仙盟创梦ide·东方仙盟
小龙报6 小时前
【优选算法】1. 水果成蓝 2.找到字符串中所有字母的异位词
java·c语言·数据结构·数据库·c++·redis·算法
txzrxz7 小时前
数论:排列数、组合数、费马小定理、逆元、同余定理
c++·算法·数论·组合数·费马小定理·逆元·排列数
xqqxqxxq7 小时前
LeetCode Hot100 双指针专项题解笔记
笔记·算法·leetcode