
数据结构与算法
文章平均质量分 66
数据结构与算法,以及一些LeetCode刷题笔记等
圆师傅
这个作者很懒,什么都没留下…
展开
-
LeetCode刷题之环形链表
为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。从图上可以看到,链表中环前面的长度是a,环的长度是r,环的起点是A,快慢指针相遇的点是B, AB两点之间的距离是b。为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。很容易可以想到,遍历链表,把链表元素添加到Set中,如果这个元素在Set中已经存在了,那么说明当前链表是有环的。如果全部链表都遍历完,也没有重复的元素,那说明链表是没有环的。原创 2024-05-10 18:46:07 · 949 阅读 · 0 评论 -
LeetCode刷题之买卖股票的最佳时机
对于第i天,计算不持有股票时的最大利润dp[i][0],取前一天不持有股票的最大利润dp[i-1][0]和前一天持有股票的最大利润加上当天卖出股票的价格dp[i-1][1] + prices[i]的较大值。对于第i天,计算持有股票时的最大利润dp[i][1],取前一天持有股票的最大利润dp[i-1][1]和前一天不持有股票的最大利润减去当天买入股票的价格dp[i-1][0] - prices[i]的较大值。与之相对应,继续持有股票,那么今天的利润是前一天持有股票的相反数,即只花了钱买股票,没有卖出。原创 2024-05-02 13:50:15 · 748 阅读 · 0 评论 -
LeetCode刷题之删除重复元素
解释:函数应返回新长度 length = 7, 并且原数组的前七个元素被修改为 0, 0, 1, 1, 2, 3, 3。解释:函数应该返回新的长度 5 , 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。// 长度正确的期望答案。给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使得出现次数超过两次的元素只出现两次 ,返回删除后数组的新长度。输入:nums = [0,0,1,1,1,2,2,3,3,4]输入:nums = [0,0,1,1,1,1,2,3,3]原创 2024-04-27 22:12:48 · 683 阅读 · 0 评论 -
LeetCode刷题之二叉树前、中、后序遍历及层次遍历
中序遍历就是左-根-右顺序遍历。然后每个子树都要进行递归调用。递归调用是维护了一个隐式的栈的,迭代就是自己显式维护这个栈。给定一个二叉树的根节点 root ,返回 它的。输入:root = [1,null,2,3]输入:root = [1]输入:root = []输出:[1,3,2]原创 2024-04-09 16:51:08 · 394 阅读 · 0 评论 -
LeetCode刷题之105. 从前序与中序遍历序列构造二叉树
二叉树的前序遍历是一种常见的树遍历方法,用于按照根-左子树-右子树的顺序遍历二叉树的节点。访问当前节点:首先访问二叉树的根节点。递归遍历左子树:如果当前节点有左子树,就递归地对左子树进行前序遍历,即重复步骤1和步骤2。递归遍历右子树:如果当前节点有右子树,就递归地对右子树进行前序遍历,即重复步骤1和步骤2。以下是一个示例二叉树的前序遍历序列:1-2-4-5-3。原创 2024-04-07 15:47:16 · 1119 阅读 · 0 评论 -
LeetCode刷题之31.下一个排列
整数数组的 下一个排列 是指其整数的下一个字典序更大的排列。更正式地,如果数组的所有排列根据其字典顺序从小到大排列在一个容器中,那么数组的 下一个排列 就是在这个有序容器中排在它后面的那个排列。如[5,7,6,4]这个排列,交换5和7,得到的是[7,5,6,4]这个组合,显然不是下一个更大排列,毕竟手动排列组合一下,发现下一个组合应该是[6,4,5,7]那么,应该是考虑从后向前第一个升序段的后一个元素开始,将后面所有的元素按字典排序,然后找到紧挨着升序点前一个元素的那个元素,将其交换即可。原创 2024-04-06 16:31:34 · 545 阅读 · 0 评论 -
LeetCode之268.Missing number
文章目录1. Description2. Follow Up3. Test Cases4. Solution4.14.31. DescriptionGiven an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.2. Follow UpCould you implement a soluti转载 2021-05-20 14:37:27 · 152 阅读 · 0 评论 -
LeetCode刷题之509 Fibonacci Number
文章目录1. Description2.Test Cases3. Solution3.1 暴力递归3.2 带备忘录递归3.3 动态规划1. DescriptionThe Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and原创 2021-05-14 10:55:34 · 179 阅读 · 0 评论 -
LeetCode刷题之53 Maximum Subarray
文章目录1.Description2.Test Cases3.Solution3.11.DescriptionGiven an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.2.Test CasesExample 1:Input: nums = [-2,1,-3,4,-1,2,1,-5,4]转载 2021-05-08 11:13:36 · 130 阅读 · 0 评论 -
LeetCode刷题之58 Length of Last Word
文章目录1.Description2. Test Cases3. Solution3.13.21.DescriptionGiven a string s consists of some words separated by spaces, return the length of the last word in the string. If the last word does not exist, return 0.A word is a maximal substring consisting转载 2021-05-08 15:12:01 · 304 阅读 · 0 评论 -
LeetCode刷题之322 Coin Change
文章目录1. Description2. Test Cases3. Solution3.1 带备忘录递归3.2 iterative4. 思考1. DescriptionYou are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.Return the fewest number of原创 2021-05-17 11:12:55 · 296 阅读 · 0 评论 -
LeetCode刷题之69. Sqrt(x)
文章目录1.Description2. Test Cases Example3.Solution3.13.21.DescriptionGiven a non-negative integer x, compute and return the square root of x.Since the return type is an integer, the decimal digits are truncated, and only the integer part of the result is转载 2021-05-08 17:33:58 · 182 阅读 · 0 评论 -
LeetCode刷题之26 Remove Duplicates from Sorted Array
文章目录1.Description2. test cases3. Solution4. conclusion1.DescriptionGiven a sorted array nums, remove the duplicates in-place such that each element appears only once and returns the new length.Do not allocate extra space for another array, you must do t转载 2021-05-07 10:59:42 · 186 阅读 · 0 评论 -
LeetCode刷题之 28 Implement strStr()
文章目录1. Description2.Test Cases3. Solution3.1 my solution3.2 one line solution1. DescriptionImplement strStr().Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Clarification:What should we return w转载 2021-05-07 15:16:26 · 219 阅读 · 0 评论 -
LeetCode刷题之1773. Count Items Matching a Rule
文章目录1.Description2.Test Cases3.Solution3.13.21.DescriptionYou are given an array items, where each items[i] = [typei, colori, namei] describes the type, color, and name of the ith item. You are also given a rule represented by two strings, ruleKey and ru转载 2021-05-10 00:13:31 · 240 阅读 · 0 评论 -
Leetcode 刷题之696. Count Binary Substrings
文章目录1.问题描述2.示例3.备注4.分析5.代码6.优化1.问题描述Give a string s, count the number of non-empty (contiguous) substrings that have the same number of 0’s and 1’s, and all the 0’s and all the 1’s in these substrings are grouped consecutively.Substrings that occur mult原创 2020-10-28 15:50:19 · 378 阅读 · 0 评论 -
LeetCode刷题之 35. Search Insert Position
文章目录1. Description2.Test Cases3.Soultion1. DescriptionGiven a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.2.Test CasesExample 1:转载 2021-05-07 17:23:45 · 188 阅读 · 0 评论 -
LeetCode刷题之101 Symmetric Tree
文章目录1. Description2.Test Cases3. Solution3.1 Recursive3.2 Iterative1. DescriptionGiven the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).2.Test CasesExample 1:Input: root = [1,2,2,3,4,4,3]Output:转载 2021-05-10 17:09:23 · 181 阅读 · 0 评论 -
LeetCode刷题之88 Merge Sorted Array
文章目录1.Description2.Test Cases3.Solution1.DescriptionGiven two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.The number of elements initialized in nums1 and nums2 are m and n respectively. You may assume that nums1 has转载 2021-05-09 22:29:47 · 145 阅读 · 0 评论 -
LeetCode之6 ZigZag Conversion
文章目录1.Description2. TC3.Solution1.DescriptionThe string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I I GY I转载 2021-08-26 17:20:00 · 265 阅读 · 0 评论 -
LeetCode 刷题之20 Valid Parentheses
文章目录1.问题2. 解法一2.1 分析2.2 code3. 解法二1.问题Given a string s containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.An input string is valid if:Open brackets must be closed by the same type of brackets.Open b原创 2020-12-27 20:47:07 · 253 阅读 · 0 评论 -
一次搞懂各种树之二叉搜索树
树1. 二叉树1.1定义1.2 性质2. 二叉查找树2.1 定义2.2 性质2.3二叉查找树的插入过程:2.4 二叉查找树的删除2.4.1 删除叶子节点2.4.2 删除单支节点2.4.3 节点完整的点树是一种非常常见且非常重要的数据结构。与线性数据结构不同,树是非线性数据结构,因而也比链表、数组等要复杂。但是学好树也不难,学不好的话,那就只能找棵树自挂东南枝了。树的定义:树是由结点或顶点和边组...原创 2020-05-14 00:07:41 · 739 阅读 · 0 评论 -
java数据结构 -- Stack源码解析
栈的基本结构以及常用方法,通过阅读源码,确定栈的初识容量、扩容机制等。原创 2020-03-26 16:44:32 · 421 阅读 · 0 评论 -
四种常见排序的实现:选择排序、插入排序、冒泡排序、快速排序
java中四种排序排序:选择排序、快速排序、冒泡排序、快速排序。1、选择排序进行两层for循环,第一轮找出一个最小的数,第二轮找出一个第二小的数,即除去已经找出的第一个数,剩下的所有数中最小的数。以此类推。public static void selectSort(int[] a) { for (int i = 0; i < a.length; i++) { ...原创 2019-01-15 14:56:21 · 1297 阅读 · 1 评论 -
leetcode Sort a linked list in O(n log n) time using constant space complexity.
分析:使用o(nlogn)的时间复杂度和常量空间复杂度,对链表排序,只能使用归并排序。归并排序是将两个或两个以上的有序链表合并成一个新的链表。常见的是二路归并排序算法,思想是将数组或链表中前后相邻的两个有序序列归并为一个有序序列,时间复杂度为o(nlogn),需要等数量的辅助空间。下面的源码,用到的技巧有快慢指针,获取链表的中间指针。链表存储的归并排序 时间复杂度O(nlogn)空间复杂度 O原创 2017-11-06 15:37:49 · 1312 阅读 · 0 评论 -
Hashtable和HashMap
1. 关于HashMap的一些说法:a) HashMap实际上是一个“链表散列”的数据结构,即数组和链表的结合体。HashMap的底层结构是一个数组,数组中的每一项是一条链表。b) HashMap的实例有俩个参数影响其性能: “初始容量” 和 装填因子。c) HashMap实现不同步,线程不安全。 HashTable线程安全d) HashMap中的key转载 2017-11-08 10:07:40 · 234 阅读 · 0 评论