- 博客(1471)
- 资源 (2)
- 收藏
- 关注
原创 Leetcode 117. Populating Next Right Pointers in Each Node II
ProblemGiven 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 should be set to NULL.Initially, all next point
2021-08-15 20:12:49 102
原创 Leetcode 116. Populating Next Right Pointers in Each Node
ProblemGiven 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 should be set to NULL.Initially, all next point
2021-08-15 20:10:56 70
原创 Leetcode 115. Distinct Subsequences
ProblemGiven two strings s and t, return the number of distinct subsequences of s which equals t.A string’s subsequence is a new string formed from the original string by deleting some (can be none) of the characters without disturbing the remaining char
2021-08-15 20:07:33 170
原创 Leetcode 114. Flatten Binary Tree to Linked List
ProblemGiven the root of a binary tree, flatten the tree into a “linked list”:The “linked list” should use the same TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null.The “linked l
2021-08-15 19:58:43 139
原创 Leetcode 113. Path Sum II
ProblemGiven the root of a binary tree and an integer targetSum, return all root-to-leaf paths where each path’s sum equals targetSum.A leaf is a node with no children.AlgorithmRecursion. Divide the tree into two sub-trees and find sum-root-val in each
2021-07-23 13:57:29 139
原创 Leetcode 112. Path Sum
ProblemGiven the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.A leaf is a node with no children.AlgorithmRecursion. Divide the tree in
2021-07-23 13:54:46 168
原创 Leetcode 111. Minimum Depth of Binary Tree
ProblemGiven a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.Note: A leaf is a node with no children.AlgorithmRecursion. Divide the tree into two s
2021-07-23 13:48:31 77
原创 Leetcode 110. Balanced Binary Tree
ProblemGiven a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as:a binary tree in which the left and right subtrees of every node differ in height by no more than 1.AlgorithmRecursion. Divid
2021-07-23 13:42:53 81
原创 Leetcode 109 Convert Sorted List to Binary Search Tree
Convert Sorted List to Binary Search TreeProblemGiven the head of a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.For this problem, a height-balanced binary tree is defined as a binary tree in which
2021-06-18 20:00:15 122
原创 Leetcode 108. Convert Sorted Array to Binary Search Tree
Convert Sorted Array to Binary Search TreeProblemGiven an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree.A height-balanced binary tree is a binary tree in which the depth of the tw
2021-06-18 19:54:32 100
原创 Leetcode 107. Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal IIproblemGiven the root of a binary tree, return the bottom-up level order traversal of its nodes’ values. (i.e., from left to right, level by level from leaf to root).Algorithmbfs. Use bfs travel on the tree, which fo
2021-06-18 19:37:36 92
原创 Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal)
Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal)ProblemGiven two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the same tree, construct an
2021-06-18 19:24:26 104
原创 Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder TraversalGiven two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.A
2021-05-26 18:38:00 224
原创 Leetcode 104. Maximum Depth of Binary Tree
Maximum Depth of Binary TreeGiven the root of a binary tree, return its maximum depth.A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.AlgorithmRecursion. Just the max value o
2021-05-26 18:34:30 70
原创 Leetcode 103. Binary Tree Zigzag Level Order Traversal
Binary Tree Zigzag Level Order TraversalGiven the root of a binary tree, return the zigzag level order traversal of its nodes’ values. (i.e., from left to right, then right to left for the next level and alternate between).AlgorithmRecursion. Just use d
2021-05-26 18:29:58 88
原创 Leetcode 102. Binary Tree Level Order Traversal
ProblemGiven the root of a binary tree, return the level order traversal of its nodes’ values. (i.e., from left to right, level by level).AlgorithmRecursion. Just use dfs, do inorder traversal, then use the depth as the level, each item on the left tree
2021-05-26 18:26:57 158
原创 Leetcode 101. Symmetric Tree
ProblemGiven the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).AlgorithmRecursion. If a trees is mirror of itself, the left sub-tree’s left sub-tree is mirror of the right sub-tree’s right sub-tree a
2021-04-29 12:07:31 89
原创 Leetcode 100. Same Tree
ProblemGiven the roots of two binary trees p and q, write a function to check if they are the same or not.Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.AlgorithmRecursion. If two trees ar
2021-04-29 12:01:46 83
原创 Leetcode 99. Recover Binary Search Tree
ProblemYou are given the root of a binary search tree (BST), where exactly two nodes of the tree were swapped by mistake. Recover the tree without changing its structure.Follow up: A solution using O(n) space is pretty straight forward. Could you devise
2021-04-29 11:22:19 107
原创 Leetcode 98. Validate Binary Search Tree
ProblemGiven the root of a binary tree, determine if it is a valid binary search tree (BST).A valid BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node’s key.The right subtree of a node contains only no
2021-04-10 22:24:26 152
原创 Leetcode 97. Interleaving String
ProblemGiven strings s1, s2, and s3, find whether s3 is formed by an interleaving of s1 and s2.An interleaving of two strings s and t is a configuration where they are divided into non-empty substrings such that:s = s1 + s2 + … + snt = t1 + t2 + … + t
2021-03-31 10:34:50 175 1
原创 Leetcode 96. Unique Binary Search Trees
ProblemGiven an integer n, return the number of structurally unique BST’s (binary search trees) which has exactly n nodes of unique values from 1 to n.AlgorithmDynamic Programming (DP).States: dp(i, j) is the list of all the BST’s that can be construct
2021-03-31 10:21:52 97
原创 Leetcode 95. Unique Binary Search Trees II
ProblemGiven an integer n, return all the structurally unique BST’s (binary search trees), which has exactly n nodes of unique values from 1 to n. Return the answer in any order.AlgorithmDynamic Programming (DP).States: dp(i, j) is the list of all the
2021-03-31 10:18:40 187
原创 Leetcode 94. Binary Tree Inorder Traversal
ProblemGiven the root of a binary tree, return the inorder traversal of its nodes’ values.AlgorithmUse dfs to travel on the tree.Code# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):#
2021-03-28 22:35:54 61
原创 Leetcode 93. Restore IP Addresses
ProblemGiven a string s containing only digits, return all possible valid IP addresses that can be obtained from s. You can return them in any order.A valid IP address consists of exactly four integers, each integer is between 0 and 255, separated by sin
2021-02-28 17:31:39 156
原创 Leetcode 92. Reverse Linked List II
ProblemGiven the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.AlgorithmCode# Definition for singly-linked list.# c
2021-02-28 17:28:01 187 1
原创 Leetcode 91. Decode Ways
ProblemA message containing letters from A-Z can be encoded into numbers using the following mapping:‘A’ -> “1”‘B’ -> “2”…‘Z’ -> “26”To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse
2021-02-27 13:49:56 160
原创 Leetcode 90. Subsets II
ProblemGiven an integer array nums that may contain duplicates, return all possible subsets (the power set).The solution set must not contain duplicate subsets. Return the solution in any order.AlgorithmUsing dfs to search the answer and use set to ign
2021-02-27 13:36:33 95
原创 Leetcode 89. Gray Code
ProblemGiven 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 a size equal to m + n such that it has enough
2021-01-24 17:12:52 97
原创 Leetcode 88. Merge Sorted Array
ProblemGiven 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 a size equal to m + n such that it has enough
2021-01-24 16:51:47 116
原创 Leetcode 87. Scramble String
ProblemWe can scramble a string s to get a string t using the following algorithm:If the length of the string is 1, stop.If the length of the string is > 1, do the following:Split the string into two non-empty substrings at a random index, i.e., i
2021-01-24 16:44:31 129
原创 Leetcode 86. Partition List
ProblemGiven the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of the two partitions.AlgorithmUse two
2021-01-24 16:37:10 252 2
原创 Leetcode 85. Maximal Rectangle
ProblemGiven a rows x cols binary matrix filled with 0’s and 1’s, find the largest rectangle containing only 1’s and return its area.AlgorithmDynamic Programming (DP). The same problem as Leetcode 84.We decompose the problem into the largest rectangle,
2020-12-31 11:06:44 126
原创 Leetcode 84. Largest Rectangle in Histogram
ProblemGiven n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.AlgorithmDynamic Programming (DP). The key is to find the maximum continuous length of eac
2020-12-31 11:00:32 119 2
原创 Leetcode 83. Remove Duplicates from Sorted List
ProblemGiven a sorted linked list, delete all duplicates such that each element appear only once.AlgorithmJust delete the same numbers in the list.Codeclass Solution: def deleteDuplicates(self, head: ListNode) -> ListNode: if not head:
2020-12-31 10:53:27 69
原创 Leetcode 82. Remove Duplicates from Sorted List II
ProblemGiven a sorted linked list, delete all duplicates such that each element appear only once.AlgorithmUse lists to save all the number and their appear times, then use the ones that only appeared once to build a new linked list.Codeclass Solution:
2020-12-31 10:50:43 123
原创 Leetcode 81. Search in Rotated Sorted Array II
ProblemSuppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).You are given a target value to search. If found in the array return true, otherwise return false.
2020-11-27 19:16:39 102
原创 Leetcode 80. Remove Duplicates from Sorted Array II
ProblemSuppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).You are given a target value to search. If found in the array return true, otherwise return false.
2020-11-27 10:42:21 132
原创 Leetcode 79. Word Search
ProblemGiven an m x n board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cells, where “adjacent” cells are horizontally or vertically neighboring. The same letter cell may not be used m
2020-11-27 10:37:11 142
原创 Leetcode 78. Subsets
ProblemGiven an integer array nums, return all possible subsets (the power set).The solution set must not contain duplicate subsets.Algorithmdfs.Codeclass Solution: def exist(self, board: List[List[str]], word: str) -> bool: if not boa
2020-11-26 18:08:58 122
SOFA: A Multi-Model Framework for Interactive Physical Simulation
2018-06-24
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人