LeetCode
月神2019
这个作者很懒,什么都没留下…
展开
-
赵二的刷题日记053《合并两个有序链表》
Merge Two Sorted ListsMerge two sorted linked lists and return it as asortedlist. The list should be made by splicing together the nodes of the first two lists.Example 1:Input: l1 = [1,2,4], l2 = [1,3,4]Output: [1,1,2,3,4,4]Example 2:..原创 2021-08-01 09:17:53 · 123 阅读 · 0 评论 -
赵二的刷题日记052《爬台阶》
Climbing StairsYou are climbing a staircase. It takesnsteps to reach the top.Each time you can either climb1or2steps. In how many distinct ways can you climb to the top?Example 1:Input: n = 2Output: 2Explanation: There are two ways to ...原创 2021-07-30 08:42:11 · 149 阅读 · 0 评论 -
赵二的刷题日记051《二叉树中的搜索》
Search in a Binary Search TreeYou are given therootof a binary search tree (BST) and an integerval.Find the node in the BST that the node's value equalsvaland return the subtree rooted with that node. If such a node does not exist, returnnull....原创 2021-07-28 08:52:24 · 138 阅读 · 0 评论 -
赵二的刷题日记050《反转链表》
Reverse Linked ListGiven theheadof a singly linked list, reverse the list, and returnthe reversed list.Example 1:Input: head = [1,2,3,4,5]Output: [5,4,3,2,1]Example 2:Input: head = [1,2]Output: [2,1]Example 3:Input: head = [...原创 2021-07-28 08:51:06 · 118 阅读 · 0 评论 -
赵二的刷题日记049《两两交换链表节点》
Swap Nodes in PairsGiven alinked list, swap every two adjacent nodes and return its head. You must solve the problem withoutmodifying the values in the list's nodes (i.e., only nodes themselves may be changed.)Example 1:Input: head = [1,2,3,..原创 2021-07-27 08:28:26 · 51 阅读 · 0 评论 -
赵二的刷题日记048《字符串反转》
Reverse StringWrite a function that reverses a string. The input string is given as an array of characterss.Example 1:Input: s = ["h","e","l","l","o"]Output: ["o","l","l","e","h"]Example 2:Input: s = ["H","a","n","n","a","h"]Output: ["h.原创 2021-07-27 08:25:25 · 56 阅读 · 0 评论 -
赵二的刷题日记047《DFS模板》
递归实现:/* * Return true if there is a path from cur to target. */boolean DFS(Node cur, Node target, Set<Node> visited) { return true if cur is target; for (next : each neighbor of cur) { if (next is not in visited) { a原创 2021-07-26 08:10:55 · 82 阅读 · 0 评论 -
赵二的刷题日记046《钥匙和房间》
Keys and RoomsThere arenrooms labeled from0ton - 1and you start in the room0. All the rooms labeled from1tonare initially locked and you cannot enter a locked room without having its key.When you visit a room, you may find a set ofdistinc...原创 2021-07-26 08:07:58 · 105 阅读 · 0 评论 -
赵二的刷题日记045《01矩阵》
01 MatrixGiven anm x nbinary matrixmat, returnthe distance of the nearest0for each cell.The distance between two adjacent cells is1.Example 1:Input: mat = [[0,0,0],[0,1,0],[0,0,0]]Output: [[0,0,0],[0,1,0],[0,0,0]]Example 2:In...原创 2021-07-24 08:30:55 · 102 阅读 · 0 评论 -
赵二的刷题日记043《BFS模板》
广度优先搜索使用队列,把还没访问过的结点依次放进队列,再拿出队头结点作为当前的遍历节点模板如下://1. 当不需要记录当前遍历到第几层时while queue 不空: cur = queue.pop() for 节点 in cur的所有相邻节点: if 该节点有效且未访问过: queue.push(该节点)//2. 加入了level用于记录层数level = 0while queue 不空: size = queue.siz...原创 2021-07-23 09:00:15 · 83 阅读 · 0 评论 -
赵二的刷题日记044《洪水填充》
An image is represented by anm x ninteger gridimagewhereimage[i][j]represents the pixel value of the image.You are also given three integerssr,sc, andnewColor. You should perform aflood fillon the image starting from the pixelimage[sr][sc]....原创 2021-07-23 08:13:30 · 196 阅读 · 0 评论 -
赵二的刷题日记042《用栈实现队列》
Implement Queue using StacksImplement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push,peek,pop, andempty).Implement theMyQueueclass:void push(int x)Pushes...原创 2021-07-22 08:07:25 · 131 阅读 · 0 评论 -
赵二的刷题日记041《字符串解码》
Decode StringGiven an encoded string, return its decoded string.The encoding rule is:k[encoded_string], where theencoded_stringinside the square brackets is being repeated exactlyktimes. Note thatkis guaranteed to be a positive integer.You m...原创 2021-07-22 08:05:37 · 62 阅读 · 0 评论 -
赵二的刷题日记040《中序遍历二叉树栈实现》
Binary Tree Inorder TraversalGiven therootof a binary tree, returnthe inorder traversal of its nodes' values.Example 1:Input: root = [1,null,2,3]Output: [1,3,2]Example 2:Input: root = []Output: []Example 3:Input: root = [1]Ou...原创 2021-07-21 08:21:52 · 65 阅读 · 0 评论 -
赵二的刷题日记039《目标和》
Target SumYou are given an integer arraynumsand an integertarget.You want to build anexpressionout of nums by adding one of the symbols'+'and'-'before each integer in nums and then concatenate all the integers.For example, ifnums = [2, 1]...原创 2021-07-21 08:14:15 · 70 阅读 · 0 评论 -
赵二的刷题日记038《克隆图》
Clone GraphGiven a reference of a node in aconnectedundirected graph.Return adeep copy(clone) of the graph.Each node in the graph contains a value (int) and a list (List[Node]) of its neighbors.class Node { public int val; public Lis...原创 2021-07-20 08:12:45 · 63 阅读 · 0 评论 -
赵二的刷题日记037《岛屿数量》
Number of IslandsGiven anm x n2D binary gridgridwhich represents a map of'1's (land) and'0's (water), returnthe number of islands.Anislandis surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may a...原创 2021-07-20 08:10:57 · 126 阅读 · 0 评论 -
赵二的刷题日记036《逆波兰表达式》
Evaluate Reverse Polish NotationEvaluate the value of an arithmetic expression inReverse Polish Notation.Valid operators are+,-,*, and/. Each operand may be an integer or another expression.Notethat division between two integers should trunca...原创 2021-07-20 08:09:07 · 74 阅读 · 0 评论 -
赵二的刷题日记035《每日温度》
Daily TemperaturesGiven an array of integerstemperaturesrepresents the daily temperatures, returnan arrayanswersuch thatanswer[i]is the number of days you have to wait after theithday to get a warmer temperature. If there is no future day for ...原创 2021-07-19 08:29:36 · 100 阅读 · 0 评论 -
赵二的刷题日记034《有效的括号》
Valid ParenthesesGiven a stringscontaining 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 brackets must be c...原创 2021-07-19 08:28:46 · 84 阅读 · 0 评论 -
赵二的刷题日记033《最小栈》
Min StackDesign a stack that supports push, pop, top, and retrieving the minimum element in constant time.Implement theMinStackclass:MinStack()initializes the stack object. void push(val)pushes the elementvalonto the stack. void pop()remov...原创 2021-07-19 08:27:37 · 51 阅读 · 0 评论 -
赵二的刷题日记032《打开转盘锁》
Open the LockYou have a lock in front of you with 4 circular wheels. Each wheel has 10 slots:'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'. The wheels can rotate freely and wrap around: for example we can turn'9'to be'0', or'0'to be'9'. Each ...原创 2021-07-18 08:06:40 · 57 阅读 · 0 评论 -
赵二的刷题日记031《岛屿数量》
Number of IslandsGiven anm x n2D binary gridgridwhich represents a map of'1's (land) and'0's (water), returnthe number of islands.Anislandis surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may a...原创 2021-07-17 09:24:30 · 142 阅读 · 0 评论 -
赵二的刷题日记030《最小公共父节点》
Lowest Common Ancestor of a Binary TreeGiven a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to thedefinition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodespandqas the ...原创 2021-07-16 08:37:37 · 52 阅读 · 0 评论 -
赵二的刷题日记029《设计循环队列》
Design Circular QueueDesign your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the fir..原创 2021-07-16 08:34:45 · 78 阅读 · 0 评论 -
赵二的刷题日记028《填充每个节点的下一个右侧结点指针 II》
Populating Next Right Pointers in Each Node IIGiven a binary treestruct Node { int val; Node *left; Node *right; Node *next;}Populate each next pointer to point to its next right node. If there is no next right node, the next pointer sho原创 2021-07-15 07:39:59 · 69 阅读 · 0 评论 -
赵二的刷题日记027《填充每个节点的下一个右侧节点指针》
Populating Next Right Pointers in Each NodeYou are given aperfect binary treewhereall leaves are on the same level, and every parent has two children. The binary tree has the following definition:struct Node { int val; Node *left; Node *r...原创 2021-07-15 07:34:37 · 75 阅读 · 0 评论 -
赵二的刷题日记026《从前序与中序遍历构造二叉树》
Construct Binary Tree from Preorder and Inorder TraversalGiven two integer arrayspreorderandinorderwherepreorderis the preorder traversal of a binary tree andinorderis the inorder traversal of the same tree, construct and returnthe binary tr...原创 2021-07-13 16:28:13 · 68 阅读 · 0 评论 -
赵二的刷题日记025《从后序与中序遍历构造二叉树》
Construct Binary Tree from Inorder and Postorder TraversalGiven two integer arraysinorderandpostorderwhereinorderis the inorder traversal of a binary tree andpostorderis the postorder traversal of the same tree, construct and returnthe binary ...原创 2021-07-13 16:26:11 · 56 阅读 · 0 评论 -
赵二的刷题日记024《路径总和》
Path SumGiven therootof a binary tree and an integertargetSum, returntrueif the tree has aroot-to-leafpath such that adding up all the values along the path equalstargetSum.Aleafis a node with no children.Example 1:Input: root = [5...原创 2021-07-12 11:15:06 · 69 阅读 · 0 评论 -
赵二的刷题日记023《对称链表》
Symmetric TreeGiven therootof a binary tree,check whether it is a mirror of itself(i.e., symmetric around its center).Example 1:Input: root = [1,2,2,3,4,4,3]Output: trueExample 2:Input: root = [1,2,2,null,3,null,3]Output: false...原创 2021-07-11 21:56:36 · 54 阅读 · 0 评论 -
赵二的刷题日记022《二叉树的最大深度》
Maximum Depth of Binary TreeGiven therootof a binary tree, returnits maximum depth.A binary tree'smaximum depthis the number of nodes along the longest path from the root node down to the farthest leaf node.Example 1:Input: root = [3,9,...原创 2021-07-11 21:54:32 · 79 阅读 · 0 评论 -
赵二的刷题日记021《层序遍历二叉树》
Binary Tree Level Order TraversalGiven therootof a binary tree, returnthe level order traversal of its nodes' values. (i.e., from left to right, level by level).Example 1:Input: root = [3,9,20,null,null,15,7]Output: [[3],[9,20],[15,7]]E...原创 2021-07-11 11:19:45 · 85 阅读 · 0 评论 -
赵二的刷题日记《020》前、中、后序遍历二叉树
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { .原创 2021-07-11 10:49:48 · 62 阅读 · 0 评论 -
赵二的刷题日记019《反转k位链表》
Rotate ListGiven theheadof a linkedlist, rotate the list to the right bykplaces.Example 1:Input: head = [1,2,3,4,5], k = 2Output: [4,5,1,2,3]Example 2:Input: head = [0,1,2], k = 4Output: [2,0,1]/** * Definition for singly...原创 2021-07-10 19:25:51 · 74 阅读 · 0 评论 -
赵二的刷题日记018《两数相加II》
Add Two NumbersYou are given twonon-emptylinked lists representing two non-negative integers. The digits are stored inreverse order, and each of their nodes contains a single digit. Add the two numbers and return the sumas a linked list.You may a...原创 2021-07-08 21:11:41 · 50 阅读 · 0 评论 -
赵二的刷题日记017《合并两个有序链表》
Merge Two Sorted ListsMerge two sorted linked lists and return it as asortedlist. The list should be made by splicing together the nodes of the first two lists.Example 1:Input: l1 = [1,2,4], l2 = [1,3,4]Output: [1,1,2,3,4,4]Example 2:..原创 2021-07-08 19:46:24 · 83 阅读 · 0 评论 -
赵二的刷题日记016《回文链表》
Palindrome Linked ListGiven theheadof a singly linked list, returntrueif it is a palindrome.Example 1:Input: head = [1,2,2,1]Output: trueExample 2:Input: head = [1,2]Output: false/** * Definition for singly-linked list. * ...原创 2021-07-08 18:43:33 · 74 阅读 · 0 评论 -
赵二的刷题日记《反转链表》
Reverse Linked ListGiven theheadof a singly linked list, reverse the list, and returnthe reversed list.Example 1:Input: head = [1,2,3,4,5]Output: [5,4,3,2,1]Example 2:Input: head = [1,2]Output: [2,1]Example 3:Input: head = []...原创 2021-07-07 10:12:47 · 62 阅读 · 0 评论 -
赵二的刷题日记014《移除链表元素》
Remove Linked List ElementsGiven theheadof a linked list and an integerval, remove all the nodes of the linked list that hasNode.val == val, and returnthe new head.Example 1:Input: head = [1,2,6,3,4,5,6], val = 6Output: [1,2,3,4,5]Examp...原创 2021-07-07 10:10:08 · 65 阅读 · 0 评论