随笔分类 -  java数据结构与算法

摘要:java使用贪心算法解决电台覆盖问题 代码实现 /** * 贪心算法实现集合覆盖 */ public class Demo { public static void main(String[] args) { // 创建电台和地区集合 HashMap<String, HashSet<String>> 阅读全文
posted @ 2022-04-25 10:05 CoderCatIce 阅读(74) 评论(0) 推荐(0)
摘要:java实现字符串匹配 暴力匹配 /** * 暴力匹配 * * @param str1 需要找的总字符串 * @param str2 需要找到的字符串 * @return 找到的字符串的下标 */ private static int violence(String str1, String str 阅读全文
posted @ 2022-04-25 08:53 CoderCatIce 阅读(206) 评论(0) 推荐(0)
摘要:java实现字符串暴力匹配 /** * 暴力匹配 * * @param str1 需要找的总字符串 * @param str2 需要找到的字符串 * @return 找到的字符串的下标 */ private static int violence(String str1, String str2) 阅读全文
posted @ 2022-04-24 23:26 CoderCatIce 阅读(91) 评论(0) 推荐(0)
摘要:java动态规划实现01背包问题 代码实现 /** * 动态规划解决01背包问题 */ public class Bag { public static void main(String[] args) { // 重量和价值 int[] w = {1, 4, 3}; int[] val = {150 阅读全文
posted @ 2022-04-24 23:24 CoderCatIce 阅读(282) 评论(0) 推荐(0)
摘要:java分治实现汉诺塔 /** * 分治实现汉诺塔 * * @param num 盘子的数量 * @param a * @param b * @param c */ public static void hanoiTower(int num, char a, char b, char c) { // 阅读全文
posted @ 2022-04-24 23:23 CoderCatIce 阅读(34) 评论(0) 推荐(0)
摘要:/** * 二分查找 * * @param arr 数组需要升序排列 * @return 数组下标 */ private static int binarySearch(int[] arr, int target) { int left = 0; int right = arr.length - 1 阅读全文
posted @ 2022-04-24 21:39 CoderCatIce 阅读(35) 评论(0) 推荐(0)
摘要:java实现图的DFS和BFS public class GraphDemo { /** * 存储顶点集合 */ private ArrayList<String> vertexList; /** * 存储图对应的领结矩阵 */ private int[][] edges; /** * 表示边的数目 阅读全文
posted @ 2022-04-24 21:22 CoderCatIce 阅读(196) 评论(0) 推荐(0)
摘要:java实现平衡二叉树 和普通的二叉排序树,在节点的添加方法中进行了修改 /** * 节点 */ class Node { int value; Node left; Node right; public Node(int value) { this.value = value; } @Overri 阅读全文
posted @ 2022-04-23 18:27 CoderCatIce 阅读(139) 评论(0) 推荐(0)
摘要:java实现二叉排序树的删除节点 节点类中方法 拿到希望删除的节点 /** * 找到希望删除的节点 * * @return 没找到为null */ public Node findDelete(int value) { if (this.value == value) { return this; 阅读全文
posted @ 2022-04-23 13:55 CoderCatIce 阅读(377) 评论(0) 推荐(0)
摘要:java实现堆排序 /** * 推排序 */ public static void heapSort(int[] arr) { int temp; // 生成大顶堆 for (int i = arr.length / 2 - 1; i >= 0; i--) { adjustHeap(arr, i, 阅读全文
posted @ 2022-04-19 17:28 CoderCatIce 阅读(192) 评论(0) 推荐(0)
摘要:java实现中序线索化二叉树遍历 节点类 /** * 节点类 */ class Node { private int id; private Node left; private Node right; /** * 规定: * 1.如果leftType==0表示指向是左子树,如果等于1表示指向前驱节 阅读全文
posted @ 2022-04-18 17:21 CoderCatIce 阅读(64) 评论(0) 推荐(0)
摘要:java实现中序线索化二叉树 节点类 /** * 节点类 */ class Node { private int id; private Node left; private Node right; /** * 规定: * 1.如果leftType==0表示指向是左子树,如果等于1表示指向前驱节点 阅读全文
posted @ 2022-04-18 17:04 CoderCatIce 阅读(113) 评论(0) 推荐(0)
摘要:java实现数组顺序存储二叉树遍历 二叉树类 class ArrBinaryTree { private int[] arr; public ArrBinaryTree(int[] arr) { this.arr = arr; } /** * 重载,方便使用 */ public void preOr 阅读全文
posted @ 2022-04-18 16:16 CoderCatIce 阅读(100) 评论(0) 推荐(0)
摘要:java实现二叉树删除节点 仅展示新增方法,全代码见前一章 节点类中 /** * 删除节点 * * @param id 需要删除节点的id */ public void delete(int id) { if (this.left != null && this.left.id == id) { t 阅读全文
posted @ 2022-04-18 15:48 CoderCatIce 阅读(143) 评论(0) 推荐(0)
摘要:java实现二叉树查找 节点类 /** * 节点类 */ class Node { private int id; private Node left; private Node right; /** * 前序查找 * * @param id 需要被寻找的id */ public Node preS 阅读全文
posted @ 2022-04-17 16:09 CoderCatIce 阅读(120) 评论(0) 推荐(0)
摘要:java实现二叉树的遍历 节点类 /** * 节点类 */ class Node { private int id; private Node left; private Node right; /** * 前序遍历的方法 */ public void preOrder() { // 先输出该节点 阅读全文
posted @ 2022-04-16 22:29 CoderCatIce 阅读(97) 评论(0) 推荐(0)
摘要:java实现hashTable 节点类 // 链表的节点 class Node { public int id; public String name; public Node pre; public Node next; @Override public String toString() { r 阅读全文
posted @ 2022-04-06 17:03 CoderCatIce 阅读(123) 评论(0) 推荐(0)
摘要:java常用查找算法 线性查找 /** * 找到一个就返回 * @param arr 数组 * @param value 需要找的数 * @return 找的数的下标,没找到为-1 */ public static int seqSearch(int[] arr, int value) { for 阅读全文
posted @ 2022-04-06 15:20 CoderCatIce 阅读(90) 评论(0) 推荐(0)
摘要:java常用排序算法 冒泡排序 public static void bubble(int[] arr) { int tem; for (int i = 1; i < arr.length; ++i) { // 负辅助变量,用于提前结束 int count = 0; for (int j = 1; 阅读全文
posted @ 2022-04-02 22:34 CoderCatIce 阅读(62) 评论(0) 推荐(0)
摘要:java实现八皇后问题 代码实现 public class Demo { int max = 8; int[] arr = new int[max]; static int count = 0; public static void main(String[] args) { Demo demo = 阅读全文
posted @ 2022-03-30 16:07 CoderCatIce 阅读(91) 评论(0) 推荐(0)