![](https://img-blog.csdnimg.cn/20201014180756923.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
数据结构
文章平均质量分 77
数据结构:
链表与邻接表:树与图的存储
栈与队列:单调队列、单调栈
KMP
Trie
并查集
堆
哈希表
....
carroll18
你想要拥有你从未有过的东西,你必须去做你从未做过的事情。
展开
-
数据结构之AC自动机问题详细介绍和例题练习
AC自动机AC自动机例题原创 2021-06-08 22:47:53 · 350 阅读 · 0 评论 -
数据结构之线段树问题详细介绍和例题练习
线段树线段树算法模板class NumArray { int[] tree; int[] nums; int n; public void buildTree(int node, int start, int end) { if (start == end) { tree[node] = nums[start]; return; } int mid = (s原创 2021-06-08 15:26:18 · 220 阅读 · 0 评论 -
数据结构之树状数组问题详细介绍和例题练习
树状数组树状数组例题原创 2021-06-01 15:23:04 · 221 阅读 · 0 评论 -
如何实现LRU算法
146. LRU缓存机制原题地址class LRUCache extends LinkedHashMap<Integer, Integer>{ private int capacity; public LRUCache(int capacity) { super(capacity, 0.75F, true); this....原创 2020-07-28 17:26:15 · 148 阅读 · 0 评论 -
常见数据结构总结(经典)
文章目录栈队列数组链表树二叉树二叉查找树/二叉排序树平衡二叉树红黑树数据存储的常用结构有:栈、队列、数组、链表和树。栈栈:stack,又称堆栈,它是运算受限的线性表,其限制是仅允许在标的一端进行插入和删除操作,不允许在其他任何位置进行添加、查找、删除等操作。简单的说:采用该结构的集合,对元素的存取有如下的特点先进后出(即,存进去的元素,要在后它后面的元素依次取出后,才能取出该元...原创 2020-04-08 14:42:38 · 970 阅读 · 0 评论 -
队列和栈(单调栈和单调队列)
用两个栈实现队列package 算法.queue;import java.util.Stack;//用两个栈实现队列public class Stack2Queue { public static void main(String[] args) { Stack2Queue stack2Queue = new Stack2Queue(); sta...原创 2020-02-20 21:58:20 · 271 阅读 · 1 评论 -
数据结构之前缀树问题详细介绍和例题练习
208. 实现 Trie (前缀树)class TrieNode { public char val; public boolean isWord; TrieNode[] child = new TrieNode[26]; public TrieNode() {} TrieNode(char c) { TrieNode node = new TrieNode(); node.val = c; }}class Trie {原创 2020-06-01 17:12:00 · 217 阅读 · 0 评论 -
面试题03. 数组中重复的数字
class Solution { public int findRepeatNumber(int[] nums) { Arrays.sort(nums); for(int i=1;i<nums.length;i++){ if(nums[i]==nums[i-1]){ return nums[i]; } } return -1; }}//.原创 2020-05-26 17:16:48 · 125 阅读 · 0 评论 -
945. 使数组唯一的最小增量
class Solution { public int minIncrementForUnique(int[] A) { Arrays.sort(A); int step = 0; for(int i = 1;i<A.length;i++){ if(A[i]<=A[i-1]){ int temp = A[i]; A[i] = A[i-1] + 1; .原创 2020-05-25 21:40:21 · 117 阅读 · 0 评论 -
1160. 拼写单词
class Solution { public int countCharacters(String[] words, String chars) { int[] chars_count = count(chars); // 统计字母表的字母出现次数 int res = 0; for (String word : words) { int[] word_count = count(word); // 统计单词的字母出现次数 .原创 2020-05-20 15:43:36 · 157 阅读 · 0 评论 -
347. 前 K 个高频元素
最小堆class Solution { public int[] topKFrequent(int[] nums, int k) { Map<Integer,Integer> map = new HashMap<>(); for(int num:nums){ map.put(num,map.getOrDefault(num,0)+1); } PriorityQueue<I..原创 2020-05-09 10:42:05 · 123 阅读 · 0 评论 -
49. 字母异位词分组
class Solution { public List<List<String>> groupAnagrams(String[] strs) { if (strs == null || strs.length ==0) return new ArrayList<List<String>>(); Map&l...原创 2020-04-27 08:45:41 · 191 阅读 · 0 评论 -
136. 只出现一次的数字
给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。说明:你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?示例 1:输入: [2,2,1]输出: 1示例 2:输入: [4,1,2,1,2]输出: 4位运算class Solution { public int singleNum...原创 2020-04-23 16:10:18 · 99 阅读 · 0 评论 -
哈希表
1. 两数之和给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。示例:给定 nums = [2, 7, 11, 15], target = 9因为 nums[0] + nums[1] = 2 + 7 = 9所以返回 [0, 1]...原创 2020-05-16 23:46:44 · 155 阅读 · 0 评论 -
数据结构之并查集问题详细介绍和例题练习
基本概念200. 岛屿数量给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。示例 1:输入:11110110101100000000输出: 1示例 2:输入:11000110000010000011输出: 3在这里插入代码...原创 2020-06-10 11:02:22 · 248 阅读 · 0 评论 -
剑指 Offer 33. 二叉搜索树的后序遍历序列
递归class Solution { public boolean verifyPostorder(int[] postorder) { return helper(postorder,0,postorder.length-1); } public boolean helper(int[] postorder,int i , int j){ if(i>=j){ return true; } ..原创 2020-06-22 21:04:05 · 180 阅读 · 0 评论 -
【LeetCode】单调栈相关练习总结
496. 下一个更大元素 Iclass Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { Stack<Integer> stack = new Stack<>(); Map<Integer,Integer> map = new HashMap<>(); for(int i = 0;i<nums2.lengt原创 2020-06-01 17:15:17 · 240 阅读 · 0 评论 -
如何去除有序数组/链表的重复元素
26. 删除排序数组中的重复项原题地址class Solution { public int removeDuplicates(int[] nums) { if(nums.length==0){ return 0; } int slow = 0; int fast = 1; while(fast<nums.length){ if(nums[slow] != n原创 2020-07-28 17:26:55 · 168 阅读 · 0 评论 -
23. 合并K个排序链表
class Solution { public ListNode mergeKLists(ListNode[] lists) { if (lists == null || lists.length == 0) return null; PriorityQueue<ListNode> queue = new PriorityQueue<>(lists.length, (o1,o2) -> o1.val - o2.val); .原创 2020-06-29 22:31:57 · 122 阅读 · 0 评论 -
LeetCode链表问题常见解题套路
文章目录160. 相交链表141. 环形链表142. 环形链表 II206. 反转链表92. 反转链表 II160. 相交链表原题地址class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { if(headA==null||headB==null){...原创 2020-05-19 11:30:06 · 296 阅读 · 0 评论 -
数组和链表
226. 翻转二叉树翻转一棵二叉树。示例:输入: 4 / \ 2 7 / \ / \1 3 6 9输出: 4 / \ 7 2 / \ / \9 6 3 1备注:这个问题是受到 Max Howell 的 原问题 启发的 :谷歌:我们90%的工程师使用您编写的软件(Homebrew)...原创 2020-05-16 23:46:10 · 150 阅读 · 0 评论 -
234. 回文链表
class Solution { public boolean isPalindrome(ListNode head) { if(head==null || head.next==null) return true; ListNode fast = head,slow = head; //快慢指针寻找链表中点 while(fast.next!=null && fast.next.next!=null){ .原创 2020-05-10 17:58:08 · 190 阅读 · 0 评论 -
1103. 分糖果 II
class Solution { public int[] distributeCandies(int candies, int num_people) { int[] res = new int[num_people]; int i = 0; while(candies>0){ res[i%num_peopl...原创 2020-05-07 11:08:39 · 141 阅读 · 0 评论 -
2. 两数相加
迭代class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { //定义一个虚拟头结点,它的下一个结点用来保存链表的头结点 ListNode dummyHead = new ListNode(-1); ListNode prev = dummyH...原创 2020-05-01 21:38:36 · 200 阅读 · 0 评论 -
328. 奇偶链表
class Solution { public ListNode oddEvenList(ListNode head) { if(head==null || head.next==null) return head; //o为奇数节点的尾结点,head为头结点 ListNode o = head; //p为偶数节点的头结点...原创 2020-04-25 22:01:03 · 113 阅读 · 0 评论 -
238. 除自身以外数组的乘积
暴力(不符合时间O(n)的时间复杂度)class Solution { public int[] productExceptSelf(int[] nums) { int[] res = new int[nums.length]; for(int i = 0;i < nums.length;i++){ int sum = ...原创 2020-04-24 21:52:43 · 182 阅读 · 0 评论 -
78. 子集
class Solution { public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> list = new ArrayList<>(); list.add(new ArrayList<>()); ...原创 2020-04-22 08:32:00 · 149 阅读 · 0 评论 -
344. 反转字符串
异或运算交换两个值 a = a^b b = a^ba = a^bb = 原来的aa = 原来的b位运算 + 双指针class Solution { public void reverseString(char[] s) { int l = 0,r = s.length-1; while(l<r){ s[l] ...原创 2020-04-21 21:28:06 · 144 阅读 · 0 评论 -
面试题06. 从尾到头打印链表
利用栈的特点,先进后出class Solution { public int[] reversePrint(ListNode head) { Stack<Integer> stack = new Stack<>(); while(head != null){ stack.push(head.val);...原创 2020-04-21 20:47:13 · 146 阅读 · 0 评论 -
409. 最长回文串
数组计数class Solution { public int longestPalindrome(String s) { int[] cnt = new int[58]; for(char c:s.toCharArray()){ cnt[c-'A'] += 1; } int ans = 0...原创 2020-04-20 20:35:21 · 160 阅读 · 0 评论 -
83. 删除排序链表中的重复元素
给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。示例 1:输入: 1->1->2输出: 1->2示例 2:输入: 1->1->2->3->3输出: 1->2->3//迭代class Solution { public ListNode deleteDuplicates(ListNode...原创 2020-04-14 21:26:56 · 106 阅读 · 0 评论 -
66. 加一
给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。你可以假设除了整数 0 之外,这个整数不会以零开头。示例 1:输入: [1,2,3]输出: [1,2,4]解释: 输入数组表示数字 123。示例 2:输入: [4,3,2,1]输出: [4,3,2,2]解释: 输入数组表示数字 ...原创 2020-04-13 20:24:27 · 94 阅读 · 0 评论