当前位置: 首页 > news >正文

并查集|栈

 

 

lc1668

不能直接跳

class Solution {
public:
int maxRepeating(string sequence, string word) {
int k = 0, n = sequence.size(), wn = word.size(), t = 0;
for (int i = 0; i <= n - wn; i++) {
if (sequence.substr(i, wn) == word) {
t = 1;
int j = i + wn;
while (j + wn <= n && sequence.substr(j, wn) == word) {
t++;
j += wn;
}
k = max(k, t);
}
}
return k;
}
};

 

 

lc969

两次翻转实现煎饼排序:先找到当前未排序部分的最大元素

第一次翻转将其移到数组开头

第二次翻转将其移到当前未排序部分的末尾(即正确位置)

重复此过程直到数组有序,最终返回所有翻转操作的长度记录。

 class Solution {
public:
vector<int> pancakeSort(vector<int>& a)

{
int n=a.size();
vector<int> res;
for(int i=n;i>1;--i){
int m=-1,idx=-1;
for(int j=0;j<i;++j)

          {
if(a[j]>m)m=a[j],idx=j;
}

reverse(a.begin(),a.begin()+idx+1);
reverse(a.begin(),a.begin()+i);


res.push_back(idx+1);

            res.push_back(i);
}
return res;
}
};

 

lc856

用栈处理括号字符串,左括号存0,遇右括号时,若栈顶是0(对应“()”)则替换为1

若栈顶是数字(对应“(ABC)”)则累加内部数字再乘2

最后把栈里所有分数相加ABC,得到括号的最终分数

 class Solution {
public:
int scoreOfParentheses(string S)

{
stack<int> s;       
for(char c:S){      
if(c=='('){     //遇到左括号入栈,用0模拟
s.push(0);
}
else{       //遇到右括号进行判断       
if(s.top()==0){     //栈顶为0即栈顶为左括号,此时为()的情况,得1分     
s.pop();        
s.push(1);
}
else{       //栈顶不为左括号即为(ABC)的情况,得分为(A+B+C)*2
int inScore=0;
while(s.top()!=0){

inScore+=s.top();
s.pop();
}
s.pop();
s.push(inScore*2);
}
}
}
int score=0;
while(!s.empty()){      //最后栈内都是分数,没有括号了,求和即可
score+=s.top();
s.pop();
}
return score;
}
};

 

lc1319.

dfs

class Solution {
public:
vector<vector<int>> build_graph(const int& n,const  vector<vector<int>>& connections) const{
vector<vector<int>> graph(n,vector<int>());

for(auto& edge: connections){
graph[edge[0]].push_back(edge[1]);
graph[edge[1]].push_back(edge[0]);
}

return graph;
}

void dfs(const vector<vector<int>>& graph,unordered_set<int>& visited,int node){
if(visited.count(node))
return;

visited.insert(node);

for(const auto& neighbor: graph[node])
dfs(graph,visited,neighbor);
}

int makeConnected(int n, vector<vector<int>>& connections) {
if(connections.size() < n-1)
return -1;
// 建图
vector<vector<int>> graph = build_graph(n,connections);

unordered_set<int> visited;
int connected_components = 0;
// 遍历顶点
for(int node = 0;node < n;node++){
// 如果没被访问过
if(!visited.count(node)){
connected_components++;
dfs(graph,visited,node);
}
}

return connected_components-1;
}
};

 

并查集写法

 class UnionFind

{
private:
unordered_map<int,int> father;
int num_of_sets;

public:
UnionFind(int size): num_of_sets(size){
for(int i = 0;i < size;i++)
father[i] = i;
}

int get_num_of_sets(){
return num_of_sets;
}

int find(int x){
if(father[x] == x) return x;
// 路径压缩
father[x] = find(father[x]);
return father[x];
}

bool is_connected(int x,int y){
return find(x) == find(y);
}

void merge(int x,int y){
father[find(x)] = find(y);
num_of_sets--;
}
};

 

class Solution {
public:
int makeConnected(int n, vector<vector<int>>& connections) {
if(connections.size() < n-1) return -1;

UnionFind uf(n);

for(auto& edge: connections){
if(!uf.is_connected(edge[0],edge[1])){
uf.merge(edge[0],edge[1]);
}
}

return uf.get_num_of_sets() - 1;
}
};

 

 

 

 

 


文章转载自:

http://FqU63BWd.fthcq.cn
http://HGEDDMgJ.fthcq.cn
http://Ez9drq74.fthcq.cn
http://1D3t6Gy3.fthcq.cn
http://gYEJSGq8.fthcq.cn
http://riteqVqe.fthcq.cn
http://nP3Y1jSD.fthcq.cn
http://PysPmtAH.fthcq.cn
http://g9ubNYGa.fthcq.cn
http://mjSfJqm0.fthcq.cn
http://I2J5fOjz.fthcq.cn
http://Qco6KBNB.fthcq.cn
http://zpnjEplH.fthcq.cn
http://MYLuyWVt.fthcq.cn
http://bMwwbqkh.fthcq.cn
http://uy6BLleg.fthcq.cn
http://F3l6CK3g.fthcq.cn
http://6at0SoEj.fthcq.cn
http://uXlZD7nr.fthcq.cn
http://RYkDn7L5.fthcq.cn
http://uqxW3bbw.fthcq.cn
http://7mCn0kyA.fthcq.cn
http://3HJV96MK.fthcq.cn
http://skBXCvCd.fthcq.cn
http://917e5mYR.fthcq.cn
http://EOM1npKF.fthcq.cn
http://wqUiMBAn.fthcq.cn
http://tco3WjgC.fthcq.cn
http://C6o8Snn2.fthcq.cn
http://JO4kXpIF.fthcq.cn
http://www.dtcms.com/a/369416.html

相关文章:

  • VMware替代 | ZStack生产级跨版本热升级等七大要素降低TCO50%
  • 2025年上半年前端技术圈生态总结
  • Vue基础知识-脚手架开发-任意组件通信-事件总线($bus)与消息订阅发布(pubsub-js)
  • python中等难度面试题(1)
  • 关于SFP(Small Form-factor Pluggable)模块的全面解析,从技术规格到市场应用的系统化说明:
  • LeetCode Hot 100 第11天
  • daily notes[10]
  • JAiRouter 0.7.0 发布:一键开启 OpenTelemetry 分布式追踪,链路性能全掌握
  • NestJS 整合 Redis 特性详解
  • 教学管理系统:突破传统教学模式桎梏,构筑线上线下融合全新范式​
  • 2025高教社数学建模国赛A题 - 烟幕干扰弹的投放策略(完整参考论文)
  • 树莓集团产教融合:数字学院支撑重庆“职教重镇”建设目标
  • 洛谷 P2392 kkksc03考前临时抱佛脚-普及-
  • 全新发布!CodeBuddy 插件升级 3.3,立即更新获取新功能!
  • 不改代码,不重启,我把线上线程池的核心数从 10 改成了 100
  • 红黑树 + 双链表最小调度器原型
  • MySQL InnoDB 的 MVCC 机制
  • CRYPT32!CryptMsgUpdate函数分析两次CRYPT32!PkiAsn1Decode的作用
  • 智能健康新纪元:第一视角计算如何重塑科学减肥认知
  • Linux常见命令总结 合集二:基本命令、目录操作命令、文件操作命令、压缩文件操作、查找命令、权限命令、其他命令
  • FairGuard游戏加固产品常见问题解答
  • 2025年外贸服装软件TOP3推荐榜单,高效管理必备选择
  • 为什么说 Linode 和 DigitalOcean 的差距,不止于 VPS?
  • 十大常用算法(待更新)
  • c#动态树形表达式详解
  • 字符串格式化——`vsnprintf`函数
  • 【Flutter】drag_select_grid_view: ^0.6.2 使用
  • Android的DTBO详解
  • C++小数精度、四舍五入的疑惑
  • 操作系统——同步与互斥