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

算法学习-基础数据结构

基础数据结构

一.栈

1.普通栈

套路:从前往后遍历 + 需要考虑相邻元素 + 有消除操作 = 栈。

2.单调栈

二.队列

1.普通队列

2.优先队列

三.Trie

使用场景:可以求某个字符串在众多字符串中出现的次数,以某个字符串为前缀出现的次数
Trie中,Trie数组是必须得,其他的根据业务场景决定(如:isEnd,count等)
在这里插入图片描述
模版1

class Trie {Trie[] children;boolean isEnd;public Trie() {children = new Trie[26];isEnd = false;}public void insert(String word) {Trie node = this;for(char c : word.toCharArray()){int index = c - 'a';if(node.children[index]==null){node.children[index] = new Trie();}node = node.children[index];}node.isEnd = true;}public boolean search(String word) {Trie node = this;for(char c : word.toCharArray()){int index = c - 'a';if(node.children[index]==null){return false;}node = node.children[index];}return node.isEnd;}public boolean startsWith(String prefix) {Trie node = this;for(char c : prefix.toCharArray()){int index = c - 'a';if(node.children[index]==null){return false;}node = node.children[index];}return true;}
}/*** Your Trie object will be instantiated and called as such:* Trie obj = new Trie();* obj.insert(word);* boolean param_2 = obj.search(word);* boolean param_3 = obj.startsWith(prefix);*/

模版2

class Trie {Trie[] child = new Trie[26];int ref = -1;void insert(String word, int ref) {Trie node = this;for (char c : word.toCharArray()) {int index = c - 'a';if (node.child[index] == null) {node.child[index] = new Trie();}node = node.child[index];}node.ref = ref;}int search(String word) {Trie node = this;for (char c : word.toCharArray()) {int index = c - 'a';if (node.child[index] == null) {return -1;}node = node.child[index];//注意:判断都是在node = node.child[index];之后判断if (node.ref != -1) {return node.ref;}}return -1;}}

LeetCode例题

四.并查集

1.逆序思维,删除–合并
2.传递性

1.普通并查集

class UF{int[] parent;int[] size;int count ;UF(int n){parent = new int[n];size = new int[n];count = n;for(int i = 0;i<n;i++){parent[i] = i;size[i] = 1;}}void union(int p,int q){int rootP = find(p);int rootQ = find(q);if(rootP==rootQ){return;}if(size[rootP]>size[rootQ]){parent[rootQ] = rootP;size[rootP] += size[rootQ];}else{parent[rootP] = rootQ;size[rootQ] += size[rootP];}count--;}int find(int x){if(parent[x]!=x){parent[x] = find(parent[x]);}return parent[x];}boolean connect(int p,int q){return find(p)==find(q);}int size(int x){return size[x];}int count(){return count;}
}

2.map实现并查集

LeetCode例题

class Solution {public boolean areSentencesSimilarTwo(String[] sentence1, String[] sentence2, List<List<String>> similarPairs) {UF uf = new UF();for(List<String> str : similarPairs){uf.union(str.get(0),str.get(1));}int m = sentence1.length;int n = sentence2.length;if(m!=n){return false;}for(int i = 0;i<n;i++){if(!uf.connected(sentence1[i],sentence2[i])){return false;}}return true;}
}class UF{HashMap<String,String> map = new HashMap<>();void union(String p,String q){String rootP = find(p);String rootQ = find(q);if(!rootP.equals(rootQ)){map.put(rootP,rootQ);}}boolean connected(String p,String q){return find(p).equals(find(q));}String find(String x){while(map.containsKey(x)&&map.get(x)!=x){x = map.get(x);}return x;}
}

3.并查集结合map记录父结点信息

LeetCode例题

class Solution {public String smallestStringWithSwaps(String s, List<List<Integer>> pairs) {int n = s.length();UF uf = new UF(n);for(List<Integer> list : pairs){uf.union(list.get(0),list.get(1));}HashMap<Integer,PriorityQueue<Character>> map = new HashMap<>();for(int i = 0;i<n;i++){int j = uf.find(i);map.computeIfAbsent(j,k->new PriorityQueue<>()).offer(s.charAt(i));}StringBuilder sb = new StringBuilder();for(int i = 0;i<n;i++){PriorityQueue<Character> q = map.get(uf.find(i));sb.append(q.poll());}return sb.toString();}
}class UF{int[] parent;UF(int n){parent = new int[n];for(int i = 0;i<n;i++){parent[i] = i;}}void union(int p,int q){int rootP = find(p);int rootQ = find(q);if(rootP==rootQ){return;}parent[rootP] = rootQ;}int find(int x){if(x!=parent[x]){parent[x] = find(parent[x]);}return parent[x];}
}

4.公因数并查集

LeetCode例题

class Solution {public boolean canTraverseAllPairs(int[] nums) {UF uf = new UF(100001);if(nums.length==1){return true;}for(int num : nums){if(num==1){return false;}int t = num;for(int i = 2;i*i<=num;i++){if(num%i==0){uf.union(t,i);while(num%i==0){num /= i;}}if(num>1){uf.union(t,num);}}}int r = uf.find(nums[0]);for(int num : nums){if(uf.find(num)!=r){return false;}}return true;}
}class UF{int[] parent;UF(int n){parent = new int[n];for(int i = 0;i<n;i++){parent[i] = i;}}void union(int p,int q){int rootP = find(p);int rootQ = find(q);if(rootP==rootQ){return;}parent[rootP] = rootQ;}int find(int x){if(x!=parent[x]){parent[x] = find(parent[x]);}return parent[x];}
}

LeetCode3和4综合练习题

5.边权并查集

LeetCode例题

class Solution {public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {int n = equations.size();UF uf = new UF(2 * n);HashMap<String, Integer> map = new HashMap<>();int id = 0;for (int i = 0; i < n; i++) {List<String> equation = equations.get(i);String val1 = equation.get(0);String val2 = equation.get(1);if (!map.containsKey(val1)) {map.put(val1, id);id++;}if (!map.containsKey(val2)) {map.put(val2, id);id++;}uf.union(map.get(val1), map.get(val2), values[i]);}int queriesSize = queries.size();double[] res = new double[queriesSize];for (int i = 0; i < queriesSize; i++) {String val1 = queries.get(i).get(0);String val2 = queries.get(i).get(1);Integer id1 = map.get(val1);Integer id2 = map.get(val2);if (id1 == null || id2 == null) {res[i] = -1.0;} else {res[i] = uf.isConnected(id1, id2);}}return res;}
}class UF {int[] parent;double[] weight;UF(int n) {parent = new int[n];weight = new double[n];for (int i = 0; i < n; i++) {parent[i] = i;weight[i] = 1.0;}}void union(int p, int q, double value) {int rootP = find(p);int rootQ = find(q);if (rootP == rootQ) {return;}parent[rootP] = rootQ;weight[rootP] = weight[q] * value / weight[p];}int find(int x) {if (x != parent[x]) {int origin = parent[x];parent[x] = find(parent[x]);weight[x] *= weight[origin];}return parent[x];}double isConnected(int p, int q) {int rootP = find(p);int rootQ = find(q);if (rootP == rootQ) {return weight[p] / weight[q];}return -1.0;}
}

相关题目:
1.LeetCode
2.LeetCode
3.LeetCode

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 【python】懂车帝字体反爬逐层解密案例(附完整代码)
  • 中秋佳节,数码好礼伴团圆:中秋节五大数码礼品指南
  • docker compose用法详解
  • 深度确定问题中的树森林操作:分析与实现
  • OpenCV+Python识别机读卡
  • 盘点国内外最好用的12款源代码加密软件:总有一款适合你
  • Python爬虫,爬取某网站小说
  • Nvidia财报前夕:市场预期股价波动创纪录,AI芯片巨头引领市场热潮
  • DNS劫持问题
  • ArcGIS Pro技术应用
  • 【计算机网络】电路交换、报文交换、分组交换
  • 云计算实训37——Dockerfile的应用+私有仓库的创建与管理
  • 第三届环境工程与可持续能源国际会议(EESE 2024)
  • 【Liunx入门】Liunx软件包管理器
  • arthas源码刨析:arthas 命令粗谈 dashboard watch retransform (3)
  • __proto__ 和 prototype的关系
  • css系列之关于字体的事
  • Cumulo 的 ClojureScript 模块已经成型
  • iOS仿今日头条、壁纸应用、筛选分类、三方微博、颜色填充等源码
  • Java应用性能调优
  • PHP 程序员也能做的 Java 开发 30分钟使用 netty 轻松打造一个高性能 websocket 服务...
  • Spring Cloud(3) - 服务治理: Spring Cloud Eureka
  • spring security oauth2 password授权模式
  • win10下安装mysql5.7
  • Yii源码解读-服务定位器(Service Locator)
  • 复习Javascript专题(四):js中的深浅拷贝
  • 关于Java中分层中遇到的一些问题
  • 基于 Ueditor 的现代化编辑器 Neditor 1.5.4 发布
  • 计算机常识 - 收藏集 - 掘金
  • 猫头鹰的深夜翻译:Java 2D Graphics, 简单的仿射变换
  • 前端面试总结(at, md)
  • 体验javascript之美-第五课 匿名函数自执行和闭包是一回事儿吗?
  • Oracle Portal 11g Diagnostics using Remote Diagnostic Agent (RDA) [ID 1059805.
  • [地铁译]使用SSD缓存应用数据——Moneta项目: 低成本优化的下一代EVCache ...
  • MyCAT水平分库
  • 微龛半导体获数千万Pre-A轮融资,投资方为国中创投 ...
  • 新海诚画集[秒速5センチメートル:樱花抄·春]
  • # Redis 入门到精通(七)-- redis 删除策略
  • (MIT博士)林达华老师-概率模型与计算机视觉”
  • (教学思路 C#之类三)方法参数类型(ref、out、parmas)
  • (六) ES6 新特性 —— 迭代器(iterator)
  • (四)Android布局类型(线性布局LinearLayout)
  • (一)UDP基本编程步骤
  • (一)使用IDEA创建Maven项目和Maven使用入门(配图详解)
  • (转)创业的注意事项
  • (自适应手机端)行业协会机构网站模板
  • .equal()和==的区别 怎样判断字符串为空问题: Illegal invoke-super to void nio.file.AccessDeniedException
  • .htaccess配置重写url引擎
  • .NET CF命令行调试器MDbg入门(二) 设备模拟器
  • .NET Framework、.NET Core 、 .NET 5、.NET 6和.NET 7 和.NET8 简介及区别
  • .Net 访问电子邮箱-LumiSoft.Net,好用
  • .NET/C# 反射的的性能数据,以及高性能开发建议(反射获取 Attribute 和反射调用方法)
  • .net连接MySQL的方法
  • .NET中分布式服务
  • @Import注解详解