![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode每日一题
记录LeetCode每日一题解法以及算法(英文)
Jasonzz_
这个作者很懒,什么都没留下…
展开
-
LeetCode Insertion Sort List (insertion sort)(java & golang)
ProblemAnalysis ProcessFirst of all, to avoid the judgment of the head, create a newHead, newHead. Since the head may be updated in the insertion sort, every time the head is removed, use newIndex = newhead.nextCreate two pointers and record the header原创 2020-08-26 22:52:57 · 135 阅读 · 0 评论 -
LeetCode Sort Colors(Dutch National Flag problem )(java & golang)
ProblemAnalysis ProcessThe question is known as the Dutch National Flag problemWe use three Pointers (p0, p2, and curr) to track the right-most boundary of 0, the left-most boundary of 2, and the currently considered element, respectivelyThe idea of t原创 2020-08-25 22:43:35 · 210 阅读 · 0 评论 -
LeetCode Word Ladder BFS(golang & java)
ProblemAnalysis Process1.Preprocess the given wordList to find all the universal states and record the universal states in the dictionary, the key is the universal state, and the value is all the words with the universal stateuniversal state:Replace a原创 2020-08-15 13:37:58 · 214 阅读 · 0 评论 -
LeetCode Flatten Binary Tree to Linked List BFS(java & golang)
ProblemAnalysis ProcessAfter the binary tree is expanded into a single linked list, the order of nodes in the single linked list is the order of nodes traversed and visited before the binary tree.Therefore, the binary tree can be preordered traversal to原创 2020-08-14 22:32:38 · 135 阅读 · 0 评论 -
LeetCode Binary Tree Zigzag Level Order Traversal BFS(golang & java)
ProblemAnalysis ProcessThe most intuitive approach is BFS, which traverses the tree layer by layer.The default order of BFS at each layer is from left to right, so the BFS algorithm needs to be adjusted to generate the sawtooth sequenceThe key is to u原创 2020-08-13 10:44:42 · 130 阅读 · 0 评论 -
LeetCode Symmetric Tree BFS (golang & java)
ProblemAnalysis ProcessBoth trees are mirror images of each other if the following conditions are metBoth of their roots have the same valueThe right subtree of each tree is mirrored to the left subtree of the other treeWe can achieve such a recursiv原创 2020-08-12 10:52:15 · 136 阅读 · 0 评论 -
LeetCode Surrounded Regions DFS (java & golang)
problemAnalysisi ProcessNotice in the explanation of the title that no O at any boundary is filled with X and we can imagine that all O that is not surrounded is directly or indirectly connected to O at the boundary and we can use this property to deter原创 2020-08-11 10:37:01 · 121 阅读 · 0 评论 -
LeetCode Letter Combinations of a Phone Number Backtracking (golang & java)
ProblemAnalysis Process1.Iterate over the letter corresponding to the first number2.A new letter is added to each number3.Adds the combined letters to the result set code until the numeric string is emptyas shown in the figureCodegolangfunc backt原创 2020-08-10 10:20:18 · 137 阅读 · 0 评论 -
LeetCode Restore IP Addresses DFS+Backtracking (golang & java)
ProblemAnalysis ProcessHow many options do we have when we do the first step? Take “25525511135” for examplepick “2” for the first segmentpick “25” for the first segmentpick “255” for the first segmentThere are three options. After you make your cho原创 2020-08-09 10:57:52 · 147 阅读 · 0 评论 -
LeetCode Rotting Oranges BFS (golang & java)
ProblemAnalysis ProcessBFS1.To start with, we take all the rotten oranges, and we queue them up as nodes at level 02.Then BFS is carried out. The adjacent nodes of each node may be nodes in four directions, top, bottom, left and right. Pay attention t原创 2020-08-08 10:20:08 · 244 阅读 · 0 评论 -
LeetCode Convert Sorted Array to Binary Search Tree BFS (golang & java)
ProblemAnalysis ProcessIn a given sequence traversal array, the number in each subtree must be continuous in the array, so the number contained in the subtree can be determined through the array subindex range, denoted as [left,right] for the entire mid原创 2020-08-07 10:05:40 · 153 阅读 · 0 评论 -
LeetCode Clone Graph (golang&java)
ProblemBasicsGraphIn computer science, a graph is defined as a set of vertices paired with a set of edges. The vertices are represented by circles, and the edges are the lines between them. Edges connect a vertex to other vertices.The following are a原创 2020-08-06 22:15:15 · 194 阅读 · 0 评论 -
LeetCode Course Schedule II Topological Sorting(golang&java)
ProblemAnalysis ProcessWhen we use a queue for breadth first search, all the nodes with an entry degree of 0 are placed in the queue, they are the first nodes that can be ordered topologically, and the relative order between them is irrelevantIn each s原创 2020-08-05 10:40:07 · 152 阅读 · 0 评论 -
LeetCode Course Schedule Topological Sorting(golang&java)
ProblemBasicsTopological SortingTo perform Topological ordering of a Directed Acyclic Graph (DAG)G, all vertices in G are arranged into a linear sequence, making any pair of vertices u and v in the Graph. If the edge <u,v> E(G), then u appears be原创 2020-08-04 18:33:40 · 185 阅读 · 0 评论 -
LeetCode Candy Greedy Algorithm(golang)
ProblemAnalysis ProcessRule definition:Suppose student A and student B are adjacent to each other, and AAis to the left of B;Left rule: when ratings A>ratings A ,B has more sugar than A,Right rule: when ratings A>ratings B ,A has more sugar tha原创 2020-08-03 15:16:56 · 138 阅读 · 0 评论 -
LeetCode Jump Game Greedy Algorithm(golang)
ProblemAnalysis ProcessUse the greedy algorithm ideaSelect the value of the first position as the maximum length of the initial jump,then compare the jump position to the last position to determine whether the position is less than the jump position v原创 2020-08-02 12:08:48 · 145 阅读 · 0 评论 -
LeetCode Is Subsequence Greedy Algorithm(golang)
ProblemBasicsGreddy AlgorithmThe greedy algorithm, which means that when you solve a problem, you always make what seems to be the best choice at the moment. In other words, instead of thinking about the overall optimal solution, the algorithm gives yo原创 2020-08-01 16:12:18 · 155 阅读 · 0 评论 -
Summary of algorithms I‘ve learned(dp,Binary search,Two pointer, Stack , Binary Tree)(golang)
ContentsDynamic programmingWhich promblem can be sovled by dp?General ideas of problem solving by dpThe steps of algorithm implementationPseudocodeExamplePackage ProblemBinary searchThe steps of algorithm implementationExamplesTwo pointerThe steps of algor原创 2020-07-31 18:33:13 · 264 阅读 · 0 评论 -
LeetCode BinaryTree Level Order Traversal (golang)
ProblemAnalysis Process1.Recursiveuse DFSWe can first traversal the binary tree in order (around the root), at the same time, record the level of the node, and define an array for each level, and then put the value of the accessed node into the array原创 2020-07-31 10:35:54 · 154 阅读 · 0 评论 -
LeetCode Binary Tree Postorder Traversal (golang)
ProblemAnalysis ProcessRecursive solution is easyIterative soltionIterating from the root, the top element pops out to the output list, then presses all its child nodes in turn, pressing the stack from top to bottom, left to rightCodeRecursive/**原创 2020-07-30 10:17:02 · 153 阅读 · 0 评论 -
LeetCode Binary Tree Preorder Traversal (golang)
ProblemAnalysis ProcessTwo kinds of solutionsOne is Rescursive it is very easyThe other is IterativeStart at the root node, each iteration pops the current top element and pushes its child onto the stack, pressing the right child first and then the原创 2020-07-29 13:48:53 · 148 阅读 · 0 评论 -
LeetCode Binary Tree Inorder Traversal (golang)
ProblemAnalysis ProcessRecursive solution is very easywe need to use iterative algorithmHere we are going to use the auxiliary stack1.The left subtree is traversed first to push the element onto the stack and then traversed in order to push the eleme原创 2020-07-28 10:43:41 · 144 阅读 · 0 评论 -
LeetCode Same Tree (golang)
ProblemAnalysis ProcessRecursion1.Determines whether the value of the current node is equal2.Recursively determines whether left and right subtrees are equalCode/** * Definition for a binary tree node. * type TreeNode struct { * Val int *原创 2020-07-27 10:14:22 · 123 阅读 · 0 评论 -
LeetCode Basic Calculator Stack (golang)
PromblemAnalysis Process1.Define two stacks,One is the numeric stack and the other is the symbol stack2.Define symbol priority3.Same-level symbols can pop up, low-level symbols can pop up high-level symbols (except open bracket)4.The numeric stack a原创 2020-07-25 09:54:27 · 356 阅读 · 0 评论 -
LeetCode Vaild Parentheses Stack (golang)
problembasicsStackA special data structure in a linear table in which data can only be inserted or deleted from a fixed end and the other end is blockedThe main characteristic of Stack is First In Last OutStack overflows when full, and underflows whe原创 2020-07-24 10:48:12 · 115 阅读 · 0 评论 -
LeetCode Median of Two Sorted Arrays Binary Search (golang)
ProblemAnalysis ProcessThe problem says there are two arrays and the overall run time complexity should be O(log(m+n)),it’s natural to think about merging arrays and binary search.By the definition of median, when m+n is odd,the median is the (m+n)/2原创 2020-07-23 11:00:23 · 161 阅读 · 0 评论 -
LeetCode Search in Rotated Sorted Array Binary Search (golang)
ProblemBasicsBinary SearchBinary Search is a more efficient method of finding data. However, Binary Search requires a linear table to be stored sequentially, and the elements in the table are arranged in order by keywordSearch ProcessFirst, assuming原创 2020-07-22 10:43:35 · 151 阅读 · 0 评论 -
LeetCode Container With Most Water Two Pointers (golang)
Problemanalytic processThe maximum yield depends on the second largest ordinateDouble pointer i j (i<j)is used to represent the head and tail of the array, and push forward until they meetCalculate the current water loadarea = (j-i) * min(height[i原创 2020-07-21 14:25:03 · 122 阅读 · 0 评论 -
LeetCode Two Sum II - Input array is sorted (Two pointer) (golang)
ProblemBasicsTwo pointersThe essence of two Pointers algorithm is to set two Pointers i,j(i.e., i <j) to point to the element to be solved respectively (i.e., i <j), and then move the two Pointers in the same direction or in the opposite directio原创 2020-07-20 22:32:46 · 145 阅读 · 0 评论 -
Leetcode每日一题 动态规划(Golang)
动态规划的思想是将一个复杂的问题转换为若干个子问题,并求子问题的最优解大家最熟悉的应该是背包问题以及货币分配问题下面是一道Leetcode上的题分析过程1.思路用坐标(0,0)来表示开始位置,f(i,j)来表示从(0,0)开始走到终点(i,j)的路径数机器人只能每次向右或向下移动一步,所以f(i,j) 只能通过 f(i-1, j)和 f(i, j-1) 转移得到(向右,向下)2.障碍题目中有说到网格中会有障碍物,用动态规划的思路来看,遇到障碍不是简单的调头选择 其他路线,是该子问题无原创 2020-07-06 19:43:07 · 1616 阅读 · 1 评论 -
Leetcode每日一题 路径总和 二叉树(golang)
目录题目二叉树基本知识(golang)性质遍历(golang实现)题目详解题目二叉树是我们经常用到的数据结构下面是一道运用二叉树遍历知识的题二叉树基本知识(golang)性质二叉树主要有以下几个性质1.二叉树的第i层上最多有2i-1(i≥1)个节点2.深度为h的二叉树中最多含有2h-1个节点3.若在任意一棵二叉树中,有n个叶子节点,有n2个度为2的节点,则必有n0=n2+14.具有n个节点的完全二叉树深为log2x+1(其中x表示不大于n的最大整数)5.若对一棵有n个节点的完全二叉树原创 2020-07-07 17:12:09 · 450 阅读 · 0 评论 -
Leetcode 每日一题 跳水板(golang)
题目分析思路运用数学方法,考虑以下情况1.k=0,则不能建造任何跳水板,返回空;2.shorter=longer 短跳水板长度=长跳水板长度,那么建造跳水板的长度是唯一的,都是shorterk,返回一个长度为1,组中元素为shorterk的数组;3.一般情况 shorter 不等于longer 且k>0,此时默认全部用短木板是一种,然后每多替换一个短木板为长木板就是一种新组合,因此又有k种组合,所以一共有k+1种长度接下来判断两种组合,1>有 i块长木板,则跳水板的长度是 sho原创 2020-07-08 17:51:10 · 268 阅读 · 0 评论 -
Leetcode 每日一题 恢复空格 动态规划(golang)
题目思路分析这里又要用到动态规划的知识1.定义dp[i]为sentence[:i]在字典中未识别的字符数,长度为sentence的长度+12.判断sentence[j:i] (i>j>=0)是否在字典中,若不在,则dp[i] = dp[i-1]+13.若在,则dp[i] = min(dp[i], dp[j])这和当前子串是否在给定串中以及计算子串出现次数有异曲同工之妙代码func respace(dictionary []string, sentence string) int原创 2020-07-09 17:28:29 · 210 阅读 · 0 评论 -
Leetcode 每日一题 最佳股票买卖时期含冷冻期 动态规划(golang)
题目思路分析这里还是用到了动态规划思想首先由题目可知股票交易有三种状态买入 卖出 冷冻期 要求最大收益,我们可以理解为买入为负收益,卖出为正收益我们用[0] 空仓 (,仓库内无股票,实行的动作是卖出股票)[1] 满仓 (仓库内有股票,实行的动作是买入股票,将要实行的动作是卖股票)[2] 冷冻期来表示各个状态设定初始状态1.第一天空仓 dp[0][0] = 02.第一天满仓(买入股票),实为负收益 dp[0][1] = -prices[0] //-prices[原创 2020-07-10 17:55:15 · 278 阅读 · 0 评论 -
Leetcode每日一题 计算右侧小于当前元素(golang)
目录题目暴力排序离散化树状数组题目暴力排序根据题意,直接用排序的方法可以轻松解决思路1.进行两次循环,查找出右侧小于当前元素的个数代码func countSmaller(nums []int) []int { counts:=[]int{} //创建空数组counts for i, num := range nums{ count :=0 //先循环遍历num数组,定义一个计数变量count为0 for j := i + 1; j < len(n原创 2020-07-11 11:55:49 · 260 阅读 · 0 评论 -
Leetcode 每日一题 地下城游戏 动态规划(golang)
1原创 2020-07-12 15:08:54 · 854 阅读 · 0 评论 -
Leetcode 每日一题 两个数组的交集 (golang)
目录题目排序哈希表题目排序思路1.首先明确如果两个数组是有序的,那么有利于求交集2.对两个数组排序,并对其遍历3.把相同元素放在第三个数组中,直到一个数组遍历完代码func intersect(nums1 []int, nums2 []int) []int { sort.Ints(nums1) sort.Ints(nums2) //对两数组排序 res := []int{} //创建第三个数组 i,j := 0,0 for i < len(nums1) &&原创 2020-07-13 12:05:15 · 834 阅读 · 0 评论 -
LeetCode每日一题 三角形最小路径和 动态规划(galang)
题目分析过程1.自上而下还是用到动态规划的思想1.用 dp(i,j) 表示从三角形顶端,走到坐标 (i,j) 的所有路径中最小的路径和2.根据题意,坐标 (i,j)只可能由它左上角或右上角走过来,因此这两个点的坐标分别为dp(i-1,j-1),dp(i-1,j)3.给出dp(i,j) 的状态转移方程,我们只需要对左上角坐标和右上角坐标的最短路径和求较小值,然后再加上坐标 (i,j) 的元素值就可以得到 dp(i,j), 即dp(i,j) = min(dp(i-1,j-1),dp(i-1,j)原创 2020-07-14 13:51:43 · 181 阅读 · 0 评论 -
LeetCode每日一题 不同的二叉搜索树 动态规划(golang)
题目分析过程二叉搜索树首先要了解二叉搜索树的特点若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值再来特殊示例1.当n=1时,只能形成一个单节点二叉搜索树2.当n=2时,可以形成两颗二叉搜索树(1为根节点,2为右子树;2为根节点,1为左子树)3.当n=3时,可以形成五颗二叉搜索树(1为根节点,2,3/3,2右子树;2为根节点,1,3左右子树;3为根节点,1,2/2,1左子树)类推到一般情况设G(n): 长度为 n原创 2020-07-15 14:51:25 · 180 阅读 · 0 评论 -
LeetCode每日一题 判断二分图 染色法(golang)
题目分析过程这里用到的染色法根据题意,二分图是指顶点由两个集合组成,且所有边的两个顶点正好分别处于两个集合里。我们可以用两种颜色代表这两个集合,相邻的顶点不能是同一种颜色,比如我们把一个节点染成红色,那么它相邻的节点就染成蓝色,当所有节点都能染上色,就是一个二分图。于是我们遍历每个顶点,再遍历当前顶点的所有相邻顶点,保证不漏掉顶点,使用深度搜索法代码func isBipartite(graph [][]int) bool { type color int const ( blue原创 2020-07-16 14:50:38 · 347 阅读 · 0 评论