模拟|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 {};

}

};

相关推荐
尽兴-29 分钟前
2.1 向量基础:Embedding、余弦相似度、欧氏距离、向量检索
算法·embedding·欧氏距离·向量检索·余弦相似度
Black蜡笔小新1 小时前
自动化AI算法训练服务器DLTM训推一体工作站赋能多行业智能化升级
人工智能·算法·自动化
怪兽学LLM1 小时前
LeetCode 438 找到字符串中所有字母异位词(Python 固定滑动窗口+字符计数解法)
python·算法·leetcode
满怀冰雪1 小时前
第04篇-双指针算法-从有序数组到回文判断的高频解法
java·算法
CC数学建模1 小时前
2026年江西省研究生数学建模竞赛1题:空间数据分析中的过拟合识别完整思路、代码、模型、文章,全网首发高质量分享!
python·算法·数学建模
leo__5201 小时前
MATLAB实现牧羊人算法
开发语言·算法·matlab
Gauss松鼠会2 小时前
【GaussDB】GaussDB SMP特性调优详解
java·服务器·前端·数据库·sql·算法·gaussdb
Tisfy2 小时前
LeetCode 3689.最大子数组总值 I:What The Medium
算法·leetcode·题解·贪心·模拟·脑筋急转弯
葬送的代码人生2 小时前
JavaScript 数组完全指南:从入门到实战
前端·javascript·算法
春日见2 小时前
决策规划控制面经汇总
人工智能·深度学习·算法·机器学习·自动驾驶