diff --git a/README.md b/README.md index 6a3591f2..39e672f9 100644 --- a/README.md +++ b/README.md @@ -2512,15 +2512,6 @@ All the solutions here are crafted with love and their performance beats 99% of
The n-queens puzzle is the problem of placing n
queens on an n x n
chessboard such that no two queens attack each other.
Given an integer n
, return the number of distinct solutions to the n-queens puzzle.
+
Example 1:
+ +Input: n = 4 +Output: 2 +Explanation: There are two distinct solutions to the 4-queens puzzle as shown. ++ +
Example 2:
+ +Input: n = 1 +Output: 1 ++ +
+
Constraints:
+ +1 <= n <= 9
Given an integer array nums
, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
+
Example 1:
+ +Input: nums = [-2,1,-3,4,-1,2,1,-5,4] +Output: 6 +Explanation: [4,-1,2,1] has the largest sum = 6. ++ +
Example 2:
+ +Input: nums = [1] +Output: 1 ++ +
Example 3:
+ +Input: nums = [0] +Output: 0 ++ +
Example 4:
+ +Input: nums = [-1] +Output: -1 ++ +
Example 5:
+ +Input: nums = [-100000] +Output: -100000 ++ +
+
Constraints:
+ +1 <= nums.length <= 3 * 104
-105 <= nums[i] <= 105
+Follow up: If you have figured out the
O(n)
solution, try coding another solution using the divide and conquer approach, which is more subtle.Given an m x n
matrix
, return all elements of the matrix
in spiral order.
+
Example 1:
+ +Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] +Output: [1,2,3,6,9,8,7,4,5] ++ +
Example 2:
+ +Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] +Output: [1,2,3,4,8,12,11,10,9,5,6,7] ++ +
+
Constraints:
+ +m == matrix.length
n == matrix[i].length
1 <= m, n <= 10
-100 <= matrix[i][j] <= 100
Given an array of non-negative integers nums
, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
+ +Determine if you are able to reach the last index.
+ ++
Example 1:
+ +Input: nums = [2,3,1,1,4] +Output: true +Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. ++ +
Example 2:
+ +Input: nums = [3,2,1,0,4] +Output: false +Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index. ++ +
+
Constraints:
+ +1 <= nums.length <= 3 * 104
0 <= nums[i] <= 105
Given an array of intervals
where intervals[i] = [starti, endi]
, merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
+
Example 1:
+ +Input: intervals = [[1,3],[2,6],[8,10],[15,18]] +Output: [[1,6],[8,10],[15,18]] +Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6]. ++ +
Example 2:
+ +Input: intervals = [[1,4],[4,5]] +Output: [[1,5]] +Explanation: Intervals [1,4] and [4,5] are considered overlapping. ++ +
+
Constraints:
+ +1 <= intervals.length <= 104
intervals[i].length == 2
0 <= starti <= endi <= 104
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
+ +You may assume that the intervals were initially sorted according to their start times.
+ ++
Example 1:
+ +Input: intervals = [[1,3],[6,9]], newInterval = [2,5] +Output: [[1,5],[6,9]] ++ +
Example 2:
+ +Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8] +Output: [[1,2],[3,10],[12,16]] +Explanation: Because the new interval+ +[4,8]
overlaps with[3,5],[6,7],[8,10]
.
Example 3:
+ +Input: intervals = [], newInterval = [5,7] +Output: [[5,7]] ++ +
Example 4:
+ +Input: intervals = [[1,5]], newInterval = [2,3] +Output: [[1,5]] ++ +
Example 5:
+ +Input: intervals = [[1,5]], newInterval = [2,7] +Output: [[1,7]] ++ +
+
Constraints:
+ +0 <= intervals.length <= 104
intervals[i].length == 2
0 <= intervals[i][0] <= intervals[i][1] <= 105
intervals
is sorted by intervals[i][0]
in ascending order.newInterval.length == 2
0 <= newInterval[0] <= newInterval[1] <= 105
Given 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 of non-space characters only.
+ ++
Example 1:
+Input: s = "Hello World" +Output: 5 +
Example 2:
+Input: s = " " +Output: 0 ++
+
Constraints:
+ +1 <= s.length <= 104
s
consists of only English letters and spaces ' '
.Given a positive integer n
, generate an n x n
matrix
filled with elements from 1
to n2
in spiral order.
+
Example 1:
+ +Input: n = 3 +Output: [[1,2,3],[8,9,4],[7,6,5]] ++ +
Example 2:
+ +Input: n = 1 +Output: [[1]] ++ +
+
Constraints:
+ +1 <= n <= 20
The set [1, 2, 3, ..., n]
contains a total of n!
unique permutations.
By listing and labeling all of the permutations in order, we get the following sequence for n = 3
:
"123"
"132"
"213"
"231"
"312"
"321"
Given n
and k
, return the kth
permutation sequence.
+
Example 1:
+Input: n = 3, k = 3 +Output: "213" +
Example 2:
+Input: n = 4, k = 9 +Output: "2314" +
Example 3:
+Input: n = 3, k = 1 +Output: "123" ++
+
Constraints:
+ +1 <= n <= 9
1 <= k <= n!
Given the head
of a linked list, rotate the list to the right by k
places.
+
Example 1:
+ +Input: head = [1,2,3,4,5], k = 2 +Output: [4,5,1,2,3] ++ +
Example 2:
+ +Input: head = [0,1,2], k = 4 +Output: [2,0,1] ++ +
+
Constraints:
+ +[0, 500]
.-100 <= Node.val <= 100
0 <= k <= 2 * 109
A robot is located at the top-left corner of a m x n
grid (marked 'Start' in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
+ +How many possible unique paths are there?
+ ++
Example 1:
+ +Input: m = 3, n = 7 +Output: 28 ++ +
Example 2:
+ +Input: m = 3, n = 2 +Output: 3 +Explanation: +From the top-left corner, there are a total of 3 ways to reach the bottom-right corner: +1. Right -> Down -> Down +2. Down -> Down -> Right +3. Down -> Right -> Down ++ +
Example 3:
+ +Input: m = 7, n = 3 +Output: 28 ++ +
Example 4:
+ +Input: m = 3, n = 3 +Output: 6 ++ +
+
Constraints:
+ +1 <= m, n <= 100
2 * 109
.A robot is located at the top-left corner of a m x n
grid (marked 'Start' in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
+ +Now consider if some obstacles are added to the grids. How many unique paths would there be?
+ +An obstacle and space is marked as 1
and 0
respectively in the grid.
+
Example 1:
+ +Input: obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]] +Output: 2 +Explanation: There is one obstacle in the middle of the 3x3 grid above. +There are two ways to reach the bottom-right corner: +1. Right -> Right -> Down -> Down +2. Down -> Down -> Right -> Right ++ +
Example 2:
+ +Input: obstacleGrid = [[0,1],[0,0]] +Output: 1 ++ +
+
Constraints:
+ +m == obstacleGrid.length
n == obstacleGrid[i].length
1 <= m, n <= 100
obstacleGrid[i][j]
is 0
or 1
.Given a m x n
grid
filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
+ ++
Example 1:
+ +Input: grid = [[1,3,1],[1,5,1],[4,2,1]] +Output: 7 +Explanation: Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum. ++ +
Example 2:
+ +Input: grid = [[1,2,3],[4,5,6]] +Output: 12 ++ +
+
Constraints:
+ +m == grid.length
n == grid[i].length
1 <= m, n <= 200
0 <= grid[i][j] <= 100
Given a non-empty array of decimal digits representing a non-negative integer, increment one to the integer.
+ +The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit.
+ +You may assume the integer does not contain any leading zero, except the number 0 itself.
+ ++
Example 1:
+ +Input: digits = [1,2,3] +Output: [1,2,4] +Explanation: The array represents the integer 123. ++ +
Example 2:
+ +Input: digits = [4,3,2,1] +Output: [4,3,2,2] +Explanation: The array represents the integer 4321. ++ +
Example 3:
+ +Input: digits = [0] +Output: [1] ++ +
+
Constraints:
+ +1 <= digits.length <= 100
0 <= digits[i] <= 9
Given two binary strings a
and b
, return their sum as a binary string.
+
Example 1:
+Input: a = "11", b = "1" +Output: "100" +
Example 2:
+Input: a = "1010", b = "1011" +Output: "10101" ++
+
Constraints:
+ +1 <= a.length, b.length <= 104
a
and b
consist only of '0'
or '1'
characters.Given 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 returned.
+ ++
Example 1:
+ +Input: x = 4 +Output: 2 ++ +
Example 2:
+ +Input: x = 8 +Output: 2 +Explanation: The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 is returned.+ +
+
Constraints:
+ +0 <= x <= 231 - 1
You are climbing a staircase. It takes n
steps to reach the top.
Each time you can either climb 1
or 2
steps. In how many distinct ways can you climb to the top?
+
Example 1:
+ +Input: n = 2 +Output: 2 +Explanation: There are two ways to climb to the top. +1. 1 step + 1 step +2. 2 steps ++ +
Example 2:
+ +Input: n = 3 +Output: 3 +Explanation: There are three ways to climb to the top. +1. 1 step + 1 step + 1 step +2. 1 step + 2 steps +3. 2 steps + 1 step ++ +
+
Constraints:
+ +1 <= n <= 45
Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the canonical path.
+ +In a UNIX-style file system, a period '.'
refers to the current directory. Furthermore, a double period '..'
moves the directory up a level.
Note that the returned canonical path must always begin with a slash '/'
, and there must be only a single slash '/'
between two directory names. The last directory name (if it exists) must not end with a trailing '/'
. Also, the canonical path must be the shortest string representing the absolute path.
+
Example 1:
+ +Input: path = "/home/" +Output: "/home" +Explanation: Note that there is no trailing slash after the last directory name. ++ +
Example 2:
+ +Input: path = "/../" +Output: "/" +Explanation: Going one level up from the root directory is a no-op, as the root level is the highest level you can go. ++ +
Example 3:
+ +Input: path = "/home//foo/" +Output: "/home/foo" +Explanation: In the canonical path, multiple consecutive slashes are replaced by a single one. ++ +
Example 4:
+ +Input: path = "/a/./b/../../c/" +Output: "/c" ++ +
+
Constraints:
+ +1 <= path.length <= 3000
path
consists of English letters, digits, period '.'
, slash '/'
or '_'
.path
is a valid Unix path.Given an m x n
matrix. If an element is 0, set its entire row and column to 0. Do it in-place.
Follow up:
+ ++
Example 1:
+ +Input: matrix = [[1,1,1],[1,0,1],[1,1,1]] +Output: [[1,0,1],[0,0,0],[1,0,1]] ++ +
Example 2:
+ +Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]] +Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]] ++ +
+
Constraints:
+ +m == matrix.length
n == matrix[0].length
1 <= m, n <= 200
-231 <= matrix[i][j] <= 231 - 1
Write an efficient algorithm that searches for a value in an m x n
matrix. This matrix has the following properties:
+
Example 1:
+ +Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3 +Output: true ++ +
Example 2:
+ +Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13 +Output: false ++ +
+
Constraints:
+ +m == matrix.length
n == matrix[i].length
1 <= m, n <= 100
-104 <= matrix[i][j], target <= 104
Given an array nums
with n
objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.
We will use the integers 0
, 1
, and 2
to represent the color red, white, and blue, respectively.
+
Example 1:
+Input: nums = [2,0,2,1,1,0] +Output: [0,0,1,1,2,2] +
Example 2:
+Input: nums = [2,0,1] +Output: [0,1,2] +
Example 3:
+Input: nums = [0] +Output: [0] +
Example 4:
+Input: nums = [1] +Output: [1] ++
+
Constraints:
+ +n == nums.length
1 <= n <= 300
nums[i]
is 0
, 1
, or 2
.+
Follow up:
+ +O(1)
constant space?Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
+ +You may return the answer in any order.
+ ++
Example 1:
+ +Input: n = 4, k = 2 +Output: +[ + [2,4], + [3,4], + [2,3], + [1,2], + [1,3], + [1,4], +] ++ +
Example 2:
+ +Input: n = 1, k = 1 +Output: [[1]] ++ +
+
Constraints:
+ +1 <= n <= 20
1 <= k <= n
Given an integer array nums
of unique elements, return all possible subsets (the power set).
The solution set must not contain duplicate subsets. Return the solution in any order.
+ ++
Example 1:
+ +Input: nums = [1,2,3] +Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] ++ +
Example 2:
+ +Input: nums = [0] +Output: [[],[0]] ++ +
+
Constraints:
+ +1 <= nums.length <= 10
-10 <= nums[i] <= 10
nums
are unique.Given 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 more than once.
+ ++
Example 1:
+ +Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED" +Output: true ++ +
Example 2:
+ +Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE" +Output: true ++ +
Example 3:
+ +Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB" +Output: false ++ +
+
Constraints:
+ +m == board.length
n = board[i].length
1 <= m, n <= 200
1 <= word.length <= 103
board
and word
consists only of lowercase and uppercase English letters.Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.
+ +Do not allocate extra space for another array; you must do this by modifying the input array in-place with O(1) extra memory.
+ +Clarification:
+ +Confused why the returned value is an integer, but your answer is an array?
+ +Note that the input array is passed in by reference, which means a modification to the input array will be known to the caller.
+ +Internally you can think of this:
+ +// nums is passed in by reference. (i.e., without making a copy) +int len = removeDuplicates(nums); + +// any modification to nums in your function would be known by the caller. +// using the length returned by your function, it prints the first len elements. +for (int i = 0; i < len; i++) { + print(nums[i]); +} ++ +
+
Example 1:
+ +Input: nums = [1,1,1,2,2,3] +Output: 5, nums = [1,1,2,2,3] +Explanation: Your function should return length =+ +5
, with the first five elements ofnums
being1, 1, 2, 2
and 3 respectively. It doesn't matter what you leave beyond the returned length. +
Example 2:
+ +Input: nums = [0,0,1,1,1,1,2,3,3] +Output: 7, nums = [0,0,1,1,2,3,3] +Explanation: Your function should return length =+ +7
, with the first seven elements ofnums
being modified to0
, 0, 1, 1, 2, 3 and 3 respectively. It doesn't matter what values are set beyond the returned length. +
+
Constraints:
+ +1 <= nums.length <= 3 * 104
-104 <= nums[i] <= 104
nums
is sorted in ascending order.You are given an integer array nums
sorted in ascending order (not necessarily distinct values), and an integer target
.
Suppose that nums
is rotated at some pivot unknown to you beforehand (i.e., [0,1,2,4,4,4,5,6,6,7]
might become [4,5,6,6,7,0,1,2,4,4]
).
If target
is found in the array return its index, otherwise, return -1
.
+
Example 1:
+Input: nums = [2,5,6,0,0,1,2], target = 0 +Output: true +
Example 2:
+Input: nums = [2,5,6,0,0,1,2], target = 3 +Output: false ++
+
Constraints:
+ +1 <= nums.length <= 5000
-104 <= nums[i] <= 104
nums
is guaranteed to be rotated at some pivot.-104 <= target <= 104
+Follow up: This problem is the same as Search in Rotated Sorted Array, where
nums
may contain duplicates. Would this affect the run-time complexity? How and why?Given the head
of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well.
+
Example 1:
+ +Input: head = [1,2,3,3,4,4,5] +Output: [1,2,5] ++ +
Example 2:
+ +Input: head = [1,1,1,2,3] +Output: [2,3] ++ +
+
Constraints:
+ +[0, 300]
.-100 <= Node.val <= 100
Given the head
of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.
+
Example 1:
+ +Input: head = [1,1,2] +Output: [1,2] ++ +
Example 2:
+ +Input: head = [1,1,2,3,3] +Output: [1,2,3] ++ +
+
Constraints:
+ +[0, 300]
.-100 <= Node.val <= 100
Given 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.
+ ++
Example 1:
+ +Input: head = [1,4,3,2,5,2], x = 3 +Output: [1,2,2,4,3,5] ++ +
Example 2:
+ +Input: head = [2,1], x = 2 +Output: [1,2] ++ +
+
Constraints:
+ +[0, 200]
.-100 <= Node.val <= 100
-200 <= x <= 200
We can scramble a string s to get a string t using the following algorithm:
+ +s
, divide it to x
and y
where s = x + y
.s
may become s = x + y
or s = y + x
.x
and y
.Given two strings s1
and s2
of the same length, return true
if s2
is a scrambled string of s1
, otherwise, return false
.
+
Example 1:
+ +Input: s1 = "great", s2 = "rgeat" +Output: true +Explanation: One possible scenario applied on s1 is: +"great" --> "gr/eat" // divide at random index. +"gr/eat" --> "gr/eat" // random decision is not to swap the two substrings and keep them in order. +"gr/eat" --> "g/r / e/at" // apply the same algorithm recursively on both substrings. divide at ranom index each of them. +"g/r / e/at" --> "r/g / e/at" // random decision was to swap the first substring and to keep the second substring in the same order. +"r/g / e/at" --> "r/g / e/ a/t" // again apply the algorithm recursively, divide "at" to "a/t". +"r/g / e/ a/t" --> "r/g / e/ a/t" // random decision is to keep both substrings in the same order. +The algorithm stops now and the result string is "rgeat" which is s2. +As there is one possible scenario that led s1 to be scrambled to s2, we return true. ++ +
Example 2:
+ +Input: s1 = "abcde", s2 = "caebd" +Output: false ++ +
Example 3:
+ +Input: s1 = "a", s2 = "a" +Output: true ++ +
+
Constraints:
+ +s1.length == s2.length
1 <= s1.length <= 30
s1
and s2
consist of lower-case English letters.Given 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 space to hold additional elements from nums2
.
+
Example 1:
+Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 +Output: [1,2,2,3,5,6] +
Example 2:
+Input: nums1 = [1], m = 1, nums2 = [], n = 0 +Output: [1] ++
+
Constraints:
+ +nums1.length == m + n
nums2.length == n
0 <= m, n <= 200
1 <= m + n <= 200
-109 <= nums1[i], nums2[i] <= 109
The gray code is a binary numeral system where two successive values differ in only one bit.
+ +Given an integer n
representing the total number of bits in the code, return any sequence of gray code.
A gray code sequence must begin with 0
.
+
Example 1:
+ +Input: n = 2 +Output: [0,1,3,2] +Explanation: +00 - 0 +01 - 1 +11 - 3 +10 - 2 +[0,2,3,1] is also a valid gray code sequence. +00 - 0 +10 - 2 +11 - 3 +01 - 1 ++ +
Example 2:
+ +Input: n = 1 +Output: [0,1] ++ +
+
Constraints:
+ +1 <= n <= 16
Given 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.
+ ++
Example 1:
+Input: nums = [1,2,2] +Output: [[],[1],[1,2],[1,2,2],[2],[2,2]] +
Example 2:
+Input: nums = [0] +Output: [[],[0]] ++
+
Constraints:
+ +1 <= nums.length <= 10
-10 <= nums[i] <= 10
Reverse a linked list from position m to n. Do it in one-pass.
+ +Note: 1 ≤ m ≤ n ≤ length of list.
+ +Example:
+ +Input: 1->2->3->4->5->NULL, m = 2, n = 4 +Output: 1->4->3->2->5->NULL ++
Given 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 single dots and cannot have leading zeros. For example, "0.1.2.201" and "192.168.1.1" are valid IP addresses and "0.011.255.245", "192.168.1.312" and "192.168@1.1" are invalid IP addresses.
+
Example 1:
+Input: s = "25525511135" +Output: ["255.255.11.135","255.255.111.35"] +
Example 2:
+Input: s = "0000" +Output: ["0.0.0.0"] +
Example 3:
+Input: s = "1111" +Output: ["1.1.1.1"] +
Example 4:
+Input: s = "010010" +Output: ["0.10.0.10","0.100.1.0"] +
Example 5:
+Input: s = "101023" +Output: ["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"] ++
+
Constraints:
+ +0 <= s.length <= 3000
s
consists of digits only.Given the root
of a binary tree, return the 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] +Output: [1] ++ +
Example 4:
+ +Input: root = [1,2] +Output: [2,1] ++ +
Example 5:
+ +Input: root = [1,null,2] +Output: [1,2] ++ +
+
Constraints:
+ +[0, 100]
.-100 <= Node.val <= 100
+ +
Follow up:
+ +Recursive solution is trivial, could you do it iteratively?
+ ++
Given 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.
+
Example 1:
+ +Input: n = 3 +Output: [[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]] ++ +
Example 2:
+ +Input: n = 1 +Output: [[1]] ++ +
+
Constraints:
+ +1 <= n <= 8
Given 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
.
+
Example 1:
+ +Input: n = 3 +Output: 5 ++ +
Example 2:
+ +Input: n = 1 +Output: 1 ++ +
+
Constraints:
+ +1 <= n <= 19
Given the root
of a binary tree, determine if it is a valid binary search tree (BST).
A valid BST is defined as follows:
+ ++
Example 1:
+ +Input: root = [2,1,3] +Output: true ++ +
Example 2:
+ +Input: root = [5,1,4,null,null,3,6] +Output: false +Explanation: The root node's value is 5 but its right child's value is 4. ++ +
+
Constraints:
+ +[1, 104]
.-231 <= Node.val <= 231 - 1
+ 2021 RUST GYM +
- 2021 RUST GYM -
-