模拟|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 = b[0], y = b[1];

// 预处理行极值

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

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

// 预处理列极值

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

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

}

int ans=0;

for(auto& b:buildings)

{

int x=b[0],y=b[1];

bool up=(y<row_max[x]);

bool down=(y>row_min[x]);

bool left=(x>col_min[y]);

bool right=(x<col_max[y]);

++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=array2[i];

sum2+=a;

hash[a]=i;;

}

for(auto& a:array1)

sum1+=a;

int d=sum1-sum2;

if(d%2) return {};

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

{

int a=array1[i];

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

return {a,f};

}

return {};

}

};

相关推荐
小O的算法实验室21 小时前
2026年ASOC,基于深度强化学习的无人机三维复杂环境分层自适应导航规划方法,深度解析+性能实测
算法·无人机·论文复现·智能算法·智能算法改进
郭涤生1 天前
STL vector 扩容机制与自定义内存分配器设计分析
c++·算法
༾冬瓜大侠༿1 天前
vector
c语言·开发语言·数据结构·c++·算法
Ricky111zzz1 天前
leetcode学python记录1
python·算法·leetcode·职场和发展
汀、人工智能1 天前
[特殊字符] 第58课:两个正序数组的中位数
数据结构·算法·数据库架构··数据流·两个正序数组的中位数
liu****1 天前
第16届省赛蓝桥杯大赛C/C++大学B组(京津冀)
开发语言·数据结构·c++·算法·蓝桥杯
汀、人工智能1 天前
[特殊字符] 第79课:分割等和子集
数据结构·算法·数据库架构·位运算·哈希表·分割等和子集
汀、人工智能1 天前
[特殊字符] 第74课:完全平方数
数据结构·算法·数据库架构·图论·bfs·完全平方数
CoderCodingNo1 天前
【GESP】C++四、五级练习题 luogu-P1177 【模板】排序
数据结构·c++·算法
Proxy_ZZ01 天前
从零实现LDPC比特翻转译码器:C语言实战与底层逻辑解析
c语言·算法