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

}

};

相关推荐
Omics Pro9 小时前
Arc研究所Cell|全新虚拟细胞官方基准评测框架
大数据·人工智能·深度学习·算法·机器学习
CoderYanger11 小时前
A.每日一题:1140. 石子游戏 II
java·程序人生·算法·leetcode·游戏·职场和发展·深度优先
小雨笙笙11 小时前
机器学习:评估模型与选择
人工智能·算法·机器学习
高亦真11 小时前
今天是学习嵌入式的第35天
linux·学习·算法
HugoStudio_SWAN11 小时前
洛谷 P1319 / P1320 压缩技术——同一枚硬币的正反面
c++·学习·程序人生·算法
wdfk_prog13 小时前
canopennode-rtt推荐,不只可以做从站,也可以承担主站角色
c语言·开发语言·数据库·学习·算法·深度优先
BioRunYiXue13 小时前
科研干货 | IC50全面解读:概念解析、实验设计与数据分析要点
java·开发语言·javascript·人工智能·算法·数据挖掘·数据分析
hahaha601613 小时前
HLS高层次综合设计技巧--UART_TX接收模块设计案例
人工智能·算法·计算机视觉
302wanger13 小时前
一个程序员看完《逆行人生》:电影没说透的,比电影更扎心
算法
hele_two13 小时前
C++模拟蚁群(二)
c++·算法·数学建模·图形渲染