diff --git a/rustgym/desc/leetcode/d1/100.md b/rustgym/desc/leetcode/d1/100.md new file mode 100644 index 00000000..2cf564c0 --- /dev/null +++ b/rustgym/desc/leetcode/d1/100.md @@ -0,0 +1,31 @@ +
Given 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.
+ ++
Example 1:
+ +Input: p = [1,2,3], q = [1,2,3] +Output: true ++ +
Example 2:
+ +Input: p = [1,2], q = [1,null,2] +Output: false ++ +
Example 3:
+ +Input: p = [1,2,1], q = [1,1,2] +Output: false ++ +
+
Constraints:
+ +[0, 100]
.-104 <= Node.val <= 104
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
+ +For example, this binary tree [1,2,2,3,4,4,3]
is symmetric:
1 + / \ + 2 2 + / \ / \ +3 4 4 3 ++ +
+ +
But the following [1,2,2,null,3,null,3]
is not:
1 + / \ + 2 2 + \ \ + 3 3 ++ +
+ +
Follow up: Solve it both recursively and iteratively.
+Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
+ +
+For example:
+Given binary tree [3,9,20,null,null,15,7]
,
+
3 + / \ + 9 20 + / \ + 15 7 ++ +
+return its level order traversal as:
+
[ + [3], + [9,20], + [15,7] +] ++
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).
+ +
+For example:
+Given binary tree [3,9,20,null,null,15,7]
,
+
3 + / \ + 9 20 + / \ + 15 7 ++ +
+return its zigzag level order traversal as:
+
[ + [3], + [20,9], + [15,7] +] ++
Given 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.
+ ++
Example 1:
+ +Input: root = [3,9,20,null,null,15,7] +Output: 3 ++ +
Example 2:
+ +Input: root = [1,null,2] +Output: 2 ++ +
Example 3:
+ +Input: root = [] +Output: 0 ++ +
Example 4:
+ +Input: root = [0] +Output: 1 ++ +
+
Constraints:
+ +[0, 104]
.-100 <= Node.val <= 100
Given preorder and inorder traversal of a tree, construct the binary tree.
+ +Note:
+You may assume that duplicates do not exist in the tree.
For example, given
+ +preorder = [3,9,20,15,7] +inorder = [9,3,15,20,7]+ +
Return the following binary tree:
+ +3 + / \ + 9 20 + / \ + 15 7+
Given inorder and postorder traversal of a tree, construct the binary tree.
+ +Note:
+You may assume that duplicates do not exist in the tree.
For example, given
+ +inorder = [9,3,15,20,7] +postorder = [9,15,7,20,3]+ +
Return the following binary tree:
+ +3 + / \ + 9 20 + / \ + 15 7 ++
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
+ +
+For example:
+Given binary tree [3,9,20,null,null,15,7]
,
+
3 + / \ + 9 20 + / \ + 15 7 ++ +
+return its bottom-up level order traversal as:
+
[ + [15,7], + [9,20], + [3] +] ++
Given an array 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 the depth of the two subtrees of every node never differ by more than 1.
+ +Example:
+ +Given the sorted array: [-10,-3,0,5,9], + +One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST: + + 0 + / \ + -3 9 + / / + -10 5 ++
Given 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 the depth of the two subtrees of every node never differ by more than 1.
+ ++
Example 1:
+ +Input: head = [-10,-3,0,5,9] +Output: [0,-3,9,-10,null,5] +Explanation: One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST. ++ +
Example 2:
+ +Input: head = [] +Output: [] ++ +
Example 3:
+ +Input: head = [0] +Output: [0] ++ +
Example 4:
+ +Input: head = [1,3] +Output: [3,1] ++ +
+
Constraints:
+ +head
is in the range [0, 2 * 104]
.-10^5 <= Node.val <= 10^5
Given 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.
+
+
Example 1:
+ +Input: root = [3,9,20,null,null,15,7] +Output: true ++ +
Example 2:
+ +Input: root = [1,2,2,3,3,null,null,4,4] +Output: false ++ +
Example 3:
+ +Input: root = [] +Output: true ++ +
+
Constraints:
+ +[0, 5000]
.-104 <= Node.val <= 104
Given 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.
+ ++
Example 1:
+ +Input: root = [3,9,20,null,null,15,7] +Output: 2 ++ +
Example 2:
+ +Input: root = [2,null,3,null,4,null,5,null,6] +Output: 5 ++ +
+
Constraints:
+ +[0, 105]
.-1000 <= Node.val <= 1000
Given 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.
+ ++
Example 1:
+ +Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 +Output: true ++ +
Example 2:
+ +Input: root = [1,2,3], targetSum = 5 +Output: false ++ +
Example 3:
+ +Input: root = [1,2], targetSum = 0 +Output: false ++ +
+
Constraints:
+ +[0, 5000]
.-1000 <= Node.val <= 1000
-1000 <= targetSum <= 1000
Given 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.
+ ++
Example 1:
+ +Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 +Output: [[5,4,11,2],[5,8,4,5]] ++ +
Example 2:
+ +Input: root = [1,2,3], targetSum = 5 +Output: [] ++ +
Example 3:
+ +Input: root = [1,2], targetSum = 0 +Output: [] ++ +
+
Constraints:
+ +[0, 5000]
.-1000 <= Node.val <= 1000
-1000 <= targetSum <= 1000
Given the root
of a binary tree, flatten the tree into a "linked list":
TreeNode
class where the right
child pointer points to the next node in the list and the left
child pointer is always null
.+
Example 1:
+ +Input: root = [1,2,5,3,4,null,6] +Output: [1,null,2,null,3,null,4,null,5,null,6] ++ +
Example 2:
+ +Input: root = [] +Output: [] ++ +
Example 3:
+ +Input: root = [0] +Output: [0] ++ +
+
Constraints:
+ +[0, 2000]
.-100 <= Node.val <= 100
+Follow up: Can you flatten the tree in-place (with
O(1)
extra space)?Given 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 relative positions of the remaining characters. (i.e., "ACE"
is a subsequence of "ABCDE"
while "AEC"
is not).
It's guaranteed the answer fits on a 32-bit signed integer.
+ ++
Example 1:
+ +Input: s = "rabbbit", t = "rabbit" +Output: 3 +Explanation: +As shown below, there are 3 ways you can generate "rabbit" from S. ++ +rabbbit
+rabbbit
+rabbbit
+
Example 2:
+ +Input: s = "babgbag", t = "bag" +Output: 5 +Explanation: +As shown below, there are 5 ways you can generate "bag" from S. ++ +babgbag
+babgbag
+babgbag
+babgbag
+babgbag
+
Constraints:
+ +0 <= s.length, t.length <= 1000
s
and t
consist of English letters.Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
+ +
+In Pascal's triangle, each number is the sum of the two numbers directly above it.
Example:
+ +Input: 5 +Output: +[ + [1], + [1,1], + [1,2,1], + [1,3,3,1], + [1,4,6,4,1] +] ++
Given a triangle
array, return the minimum path sum from top to bottom.
For each step, you may move to an adjacent number of the row below. More formally, if you are on index i
on the current row, you may move to either index i
or index i + 1
on the next row.
+
Example 1:
+ +Input: triangle = [[2],[3,4],[6,5,7],[4,1,8,3]] +Output: 11 +Explanation: The triangle looks like: + 2 + 3 4 + 6 5 7 +4 1 8 3 +The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above). ++ +
Example 2:
+ +Input: triangle = [[-10]] +Output: -10 ++ +
+
Constraints:
+ +1 <= triangle.length <= 200
triangle[0].length == 1
triangle[i].length == triangle[i - 1].length + 1
-104 <= triangle[i][j] <= 104
+Follow up: Could you do this using only
O(n)
extra space, where n
is the total number of rows in the triangle?You are given an array prices
where prices[i]
is the price of a given stock on the ith
day.
You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
+ +Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0
.
+
Example 1:
+ +Input: prices = [7,1,5,3,6,4] +Output: 5 +Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. +Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell. ++ +
Example 2:
+ +Input: prices = [7,6,4,3,1] +Output: 0 +Explanation: In this case, no transactions are done and the max profit = 0. ++ +
+
Constraints:
+ +1 <= prices.length <= 105
0 <= prices[i] <= 104
Say you have an array prices
for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).
+ +Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
+ +Example 1:
+ +Input: [7,1,5,3,6,4] +Output: 7 +Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4. + Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3. ++ +
Example 2:
+ +Input: [1,2,3,4,5] +Output: 4 +Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. + Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are + engaging multiple transactions at the same time. You must sell before buying again. ++ +
Example 3:
+ +Input: [7,6,4,3,1] +Output: 0 +Explanation: In this case, no transaction is done, i.e. max profit = 0.+ +
+
Constraints:
+ +1 <= prices.length <= 3 * 10 ^ 4
0 <= prices[i] <= 10 ^ 4
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
+ +Note: For the purpose of this problem, we define empty string as valid palindrome.
+ +Example 1:
+ +Input: "A man, a plan, a canal: Panama" +Output: true ++ +
Example 2:
+ +Input: "race a car" +Output: false ++ +
+
Constraints:
+ +s
consists only of printable ASCII characters.Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
+ +Note: For the purpose of this problem, we define empty string as valid palindrome.
+ +Example 1:
+ +Input: "A man, a plan, a canal: Panama" +Output: true ++ +
Example 2:
+ +Input: "race a car" +Output: false ++ +
+
Constraints:
+ +s
consists only of printable ASCII characters.Given a 2D board containing 'X'
and 'O'
(the letter O), capture all regions surrounded by 'X'
.
A region is captured by flipping all 'O'
s into 'X'
s in that surrounded region.
Example:
+ +X X X X +X O O X +X X O X +X O X X ++ +
After running your function, the board should be:
+ +X X X X +X X X X +X X X X +X O X X ++ +
Explanation:
+ +Surrounded regions shouldn’t be on the border, which means that any 'O'
on the border of the board are not flipped to 'X'
. Any 'O'
that is not on the border and it is not connected to an 'O'
on the border will be flipped to 'X'
. Two cells are connected if they are adjacent cells connected horizontally or vertically.
Given a string s
, partition s
such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s
.
A palindrome string is a string that reads the same backward as forward.
+ ++
Example 1:
+Input: s = "aab" +Output: [["a","a","b"],["aa","b"]] +
Example 2:
+Input: s = "a" +Output: [["a"]] ++
+
Constraints:
+ +1 <= s.length <= 16
s
contains only lowercase English letters.Given a string s
, partition s
such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s
.
+
Example 1:
+ +Input: s = "aab" +Output: 1 +Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut. ++ +
Example 2:
+ +Input: s = "a" +Output: 0 ++ +
Example 3:
+ +Input: s = "ab" +Output: 1 ++ +
+
Constraints:
+ +1 <= s.length <= 2000
s
consists of lower-case English letters only.There are n
gas stations along a circular route, where the amount of gas at the ith
station is gas[i]
.
You have a car with an unlimited gas tank and it costs cost[i]
of gas to travel from the ith
station to its next (i + 1)th
station. You begin the journey with an empty tank at one of the gas stations.
Given two integer arrays gas
and cost
, return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1
. If there exists a solution, it is guaranteed to be unique
+
Example 1:
+ +Input: gas = [1,2,3,4,5], cost = [3,4,5,1,2] +Output: 3 +Explanation: +Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4 +Travel to station 4. Your tank = 4 - 1 + 5 = 8 +Travel to station 0. Your tank = 8 - 2 + 1 = 7 +Travel to station 1. Your tank = 7 - 3 + 2 = 6 +Travel to station 2. Your tank = 6 - 4 + 3 = 5 +Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3. +Therefore, return 3 as the starting index. ++ +
Example 2:
+ +Input: gas = [2,3,4], cost = [3,4,3] +Output: -1 +Explanation: +You can't start at station 0 or 1, as there is not enough gas to travel to the next station. +Let's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4 +Travel to station 0. Your tank = 4 - 3 + 2 = 3 +Travel to station 1. Your tank = 3 - 3 + 3 = 3 +You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3. +Therefore, you can't travel around the circuit once no matter where you start. ++ +
+
Constraints:
+ +gas.length == n
cost.length == n
1 <= n <= 104
0 <= gas[i], cost[i] <= 104
Given a non-empty array of integers nums
, every element appears twice except for one. Find that single one.
Follow up: Could you implement a solution with a linear runtime complexity and without using extra memory?
+ ++
Example 1:
+Input: nums = [2,2,1] +Output: 1 +
Example 2:
+Input: nums = [4,1,2,1,2] +Output: 4 +
Example 3:
+Input: nums = [1] +Output: 1 ++
+
Constraints:
+ +1 <= nums.length <= 3 * 104
-3 * 104 <= nums[i] <= 3 * 104
Given an integer array nums
where every element appears three times except for one, which appears exactly once. Find the single element and return it.
+
Example 1:
+Input: nums = [2,2,3,2] +Output: 3 +
Example 2:
+Input: nums = [0,1,0,1,0,1,99] +Output: 99 ++
+
Constraints:
+ +1 <= nums.length <= 3 * 104
-231 <= nums[i] <= 231 - 1
nums
appears exactly three times except for one element which appears once.+
Follow up: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
+Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
+ +Note:
+ +Example 1:
+ +Input: s = "leetcode", wordDict = ["leet", "code"] +Output: true +Explanation: Return true because+ +"leetcode"
can be segmented as"leet code"
. +
Example 2:
+ +Input: s = "applepenapple", wordDict = ["apple", "pen"] +Output: true +Explanation: Return true because+ +"
applepenapple"
can be segmented as"
apple pen apple"
. + Note that you are allowed to reuse a dictionary word. +
Example 3:
+ +Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"] +Output: false ++
Given a singly linked list L: L0→L1→…→Ln-1→Ln,
+reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
You may not modify the values in the list's nodes, only nodes itself may be changed.
+ +Example 1:
+ +Given 1->2->3->4, reorder it to 1->4->2->3.+ +
Example 2:
+ +Given 1->2->3->4->5, reorder it to 1->5->2->4->3. ++
Given the root
of a binary tree, return the preorder traversal of its nodes' values.
+
Example 1:
+ +Input: root = [1,null,2,3] +Output: [1,2,3] ++ +
Example 2:
+ +Input: root = [] +Output: [] ++ +
Example 3:
+ +Input: root = [1] +Output: [1] ++ +
Example 4:
+ +Input: root = [1,2] +Output: [1,2] ++ +
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 the root
of a binary tree, return the postorder traversal of its nodes' values.
+
Example 1:
+ +Input: root = [1,null,2,3] +Output: [3,2,1] ++ +
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: [2,1] ++ +
+
Constraints:
+ +[0, 100]
.-100 <= Node.val <= 100
+ +
Follow up:
+ +Recursive solution is trivial, could you do it iteratively?
+ ++
Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.
+ +Implement the LRUCache
class:
LRUCache(int capacity)
Initialize the LRU cache with positive size capacity
.int get(int key)
Return the value of the key
if the key exists, otherwise return -1
.void put(int key, int value)
Update the value of the key
if the key
exists. Otherwise, add the key-value
pair to the cache. If the number of keys exceeds the capacity
from this operation, evict the least recently used key.Follow up:
+Could you do get
and put
in O(1)
time complexity?
+
Example 1:
+ +Input +["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"] +[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]] +Output +[null, null, null, 1, null, -1, null, -1, 3, 4] + +Explanation +LRUCache lRUCache = new LRUCache(2); +lRUCache.put(1, 1); // cache is {1=1} +lRUCache.put(2, 2); // cache is {1=1, 2=2} +lRUCache.get(1); // return 1 +lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3} +lRUCache.get(2); // returns -1 (not found) +lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3} +lRUCache.get(1); // return -1 (not found) +lRUCache.get(3); // return 3 +lRUCache.get(4); // return 4 ++ +
+
Constraints:
+ +1 <= capacity <= 3000
0 <= key <= 3000
0 <= value <= 104
3 * 104
calls will be made to get
and put
.Sort a linked list using insertion sort.
+ +
+A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list.
+With each iteration one element (red) is removed from the input data and inserted in-place into the sorted list
+
Algorithm of Insertion Sort:
+ +
+Example 1:
Input: 4->2->1->3 +Output: 1->2->3->4 ++ +
Example 2:
+ +Input: -1->5->3->4->0 +Output: -1->0->3->4->5 ++
Given the head
of a linked list, return the list after sorting it in ascending order.
Follow up: Can you sort the linked list in O(n logn)
time and O(1)
memory (i.e. constant space)?
+
Example 1:
+ +Input: head = [4,2,1,3] +Output: [1,2,3,4] ++ +
Example 2:
+ +Input: head = [-1,5,3,4,0] +Output: [-1,0,3,4,5] ++ +
Example 3:
+ +Input: head = [] +Output: [] ++ +
+
Constraints:
+ +[0, 5 * 104]
.-105 <= Node.val <= 105
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
+ +Valid operators are +
, -
, *
, /
. Each operand may be an integer or another expression.
Note:
+ +Example 1:
+ +Input: ["2", "1", "+", "3", "*"] +Output: 9 +Explanation: ((2 + 1) * 3) = 9 ++ +
Example 2:
+ +Input: ["4", "13", "5", "/", "+"] +Output: 6 +Explanation: (4 + (13 / 5)) = 6 ++ +
Example 3:
+ +Input: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"] +Output: 22 +Explanation: + ((10 * (6 / ((9 + 3) * -11))) + 17) + 5 += ((10 * (6 / (12 * -11))) + 17) + 5 += ((10 * (6 / -132)) + 17) + 5 += ((10 * 0) + 17) + 5 += (0 + 17) + 5 += 17 + 5 += 22 ++
Given an input string s
, reverse the order of the words.
A word is defined as a sequence of non-space characters. The words in s
will be separated by at least one space.
Return a string of the words in reverse order concatenated by a single space.
+ +Note that s
may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.
+
Example 1:
+ +Input: s = "the sky is blue" +Output: "blue is sky the" ++ +
Example 2:
+ +Input: s = " hello world " +Output: "world hello" +Explanation: Your reversed string should not contain leading or trailing spaces. ++ +
Example 3:
+ +Input: s = "a good example" +Output: "example good a" +Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string. ++ +
Example 4:
+ +Input: s = " Bob Loves Alice " +Output: "Alice Loves Bob" ++ +
Example 5:
+ +Input: s = "Alice does not even like bob" +Output: "bob like even not does Alice" ++ +
+
Constraints:
+ +1 <= s.length <= 104
s
contains English letters (upper-case and lower-case), digits, and spaces ' '
.s
.+ +
Follow up:
+ +O(1)
extra space?+
Given an integer array nums
, find the contiguous subarray within an array (containing at least one number) which has the largest product.
Example 1:
+ +Input: [2,3,-2,4]
+Output: 6
+Explanation: [2,3] has the largest product 6.
+
+
+Example 2:
+ +Input: [-2,0,-1] +Output: 0 +Explanation: The result cannot be 2, because [-2,-1] is not a subarray.+
Suppose an array of length n
sorted in ascending order is rotated between 1
and n
times. For example, the array nums = [0,1,2,4,5,6,7]
might become:
[4,5,6,7,0,1,2]
if it was rotated 4
times.[0,1,2,4,5,6,7]
if it was rotated 7
times.Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]]
1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]]
.
Given the sorted rotated array nums
, return the minimum element of this array.
+
Example 1:
+ +Input: nums = [3,4,5,1,2] +Output: 1 +Explanation: The original array was [1,2,3,4,5] rotated 3 times. ++ +
Example 2:
+ +Input: nums = [4,5,6,7,0,1,2] +Output: 0 +Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times. ++ +
Example 3:
+ +Input: nums = [11,13,15,17] +Output: 11 +Explanation: The original array was [11,13,15,17] and it was rotated 4 times. ++ +
+
Constraints:
+ +n == nums.length
1 <= n <= 5000
-5000 <= nums[i] <= 5000
nums
are unique.nums
is sorted and rotated between 1
and n
times.Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
+ +(i.e., [0,1,2,4,5,6,7]
might become [4,5,6,7,0,1,2]
).
Find the minimum element.
+ +The array may contain duplicates.
+ +Example 1:
+ +Input: [1,3,5] +Output: 1+ +
Example 2:
+ +Input: [2,2,2,0,1] +Output: 0+ +
Note:
+ +Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
+ ++
Example 1:
+ +Input +["MinStack","push","push","push","getMin","pop","top","getMin"] +[[],[-2],[0],[-3],[],[],[],[]] + +Output +[null,null,null,null,-3,null,0,-2] + +Explanation +MinStack minStack = new MinStack(); +minStack.push(-2); +minStack.push(0); +minStack.push(-3); +minStack.getMin(); // return -3 +minStack.pop(); +minStack.top(); // return 0 +minStack.getMin(); // return -2 ++ +
+
Constraints:
+ +pop
, top
and getMin
operations will always be called on non-empty stacks.Given the root
of a binary tree, turn the tree upside down and return the new root.
You can turn a binary tree upside down with the following steps:
+ +The mentioned steps are done level by level, it is guaranteed that every node in the given tree has either 0 or 2 children.
+ ++
Example 1:
+ +Input: root = [1,2,3,4,5] +Output: [4,5,2,null,null,3,1] ++ +
Example 2:
+ +Input: root = [] +Output: [] ++ +
Example 3:
+ +Input: root = [1] +Output: [1] ++ +
+
Constraints:
+ +[0, 10]
.1 <= Node.val <= 10
Every node has either 0 or 2 children.
Given a string s , find the length of the longest substring t that contains at most 2 distinct characters.
+ +Example 1:
+ +Input: "eceba" +Output: 3 +Explanation: t is "ece" which its length is 3. ++ +
Example 2:
+ +Input: "ccaabbb" +Output: 5 +Explanation: t is "aabbb" which its length is 5. ++
Given two strings s
and t
, return true
if they are both one edit distance apart, otherwise return false
.
A string s
is said to be one distance apart from a string t
if you can:
s
to get t
.s
to get t
.s
with a different character to get t
.+
Example 1:
+ +Input: s = "ab", t = "acb" +Output: true +Explanation: We can insert 'c' into s to get t. ++ +
Example 2:
+ +Input: s = "", t = "" +Output: false +Explanation: We cannot get t from s by only one step. ++ +
Example 3:
+ +Input: s = "a", t = "" +Output: true ++ +
Example 4:
+ +Input: s = "", t = "A" +Output: true ++ +
+
Constraints:
+ +0 <= s.length <= 104
0 <= t.length <= 104
s
and t
consist of lower-case letters, upper-case letters and/or digits.A peak element is an element that is strictly greater than its neighbors.
+ +Given an integer array nums
, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks.
You may imagine that nums[-1] = nums[n] = -∞
.
+
Example 1:
+ +Input: nums = [1,2,3,1] +Output: 2 +Explanation: 3 is a peak element and your function should return the index number 2.+ +
Example 2:
+ +Input: nums = [1,2,1,3,5,6,4] +Output: 5 +Explanation: Your function can return either index number 1 where the peak element is 2, or index number 5 where the peak element is 6.+ +
+
Constraints:
+ +1 <= nums.length <= 1000
-231 <= nums[i] <= 231 - 1
nums[i] != nums[i + 1]
for all valid i
.+Follow up: Could you implement a solution with logarithmic complexity?
You are given an inclusive range [lower, upper]
and a sorted unique integer array nums
, where all elements are in the inclusive range.
A number x
is considered missing if x
is in the range [lower, upper]
and x
is not in nums
.
Return the smallest sorted list of ranges that cover every missing number exactly. That is, no element of nums
is in any of the ranges, and each missing number is in one of the ranges.
Each range [a,b]
in the list should be output as:
"a->b"
if a != b
"a"
if a == b
+
Example 1:
+ +Input: nums = [0,1,3,50,75], lower = 0, upper = 99 +Output: ["2","4->49","51->74","76->99"] +Explanation: The ranges are: +[2,2] --> "2" +[4,49] --> "4->49" +[51,74] --> "51->74" +[76,99] --> "76->99" ++ +
Example 2:
+ +Input: nums = [], lower = 1, upper = 1 +Output: ["1"] +Explanation: The only missing range is [1,1], which becomes "1". ++ +
Example 3:
+ +Input: nums = [], lower = -3, upper = -1 +Output: ["-3->-1"] +Explanation: The only missing range is [-3,-1], which becomes "-3->-1". ++ +
Example 4:
+ +Input: nums = [-1], lower = -1, upper = -1 +Output: [] +Explanation: There are no missing ranges since there are no missing numbers. ++ +
Example 5:
+ +Input: nums = [-1], lower = -2, upper = -1 +Output: ["-2"] ++ +
+
Constraints:
+ +-109 <= lower <= upper <= 109
0 <= nums.length <= 100
lower <= nums[i] <= upper
nums
are unique.Given an unsorted array, find the maximum difference between the successive elements in its sorted form.
+ +Return 0 if the array contains less than 2 elements.
+ +Example 1:
+ +Input: [3,6,9,1] +Output: 3 +Explanation: The sorted form of the array is [1,3,6,9], either + (3,6) or (6,9) has the maximum difference 3.+ +
Example 2:
+ +Input: [10] +Output: 0 +Explanation: The array contains less than 2 elements, therefore return 0.+ +
Note:
+ +Given two version numbers, version1
and version2
, compare them.
Version numbers consist of one or more revisions joined by a dot '.'
. Each revision consists of digits and may contain leading zeros. Every revision contains at least one character. Revisions are 0-indexed from left to right, with the leftmost revision being revision 0, the next revision being revision 1, and so on. For example 2.5.33
and 0.1
are valid version numbers.
To compare version numbers, compare their revisions in left-to-right order. Revisions are compared using their integer value ignoring any leading zeros. This means that revisions 1
and 001
are considered equal. If a version number does not specify a revision at an index, then treat the revision as 0
. For example, version 1.0
is less than version 1.1
because their revision 0s are the same, but their revision 1s are 0
and 1
respectively, and 0 < 1
.
Return the following:
+ +version1 < version2
, return -1
.version1 > version2
, return 1
.0
.+
Example 1:
+ +Input: version1 = "1.01", version2 = "1.001" +Output: 0 +Explanation: Ignoring leading zeroes, both "01" and "001" represent the same integer "1". ++ +
Example 2:
+ +Input: version1 = "1.0", version2 = "1.0.0" +Output: 0 +Explanation: version1 does not specify revision 2, which means it is treated as "0". ++ +
Example 3:
+ +Input: version1 = "0.1", version2 = "1.1" +Output: -1 +Explanation: version1's revision 0 is "0", while version2's revision 0 is "1". 0 < 1, so version1 < version2. ++ +
Example 4:
+ +Input: version1 = "1.0.1", version2 = "1" +Output: 1 ++ +
Example 5:
+ +Input: version1 = "7.5.2.4", version2 = "7.5.3" +Output: -1 ++ +
+
Constraints:
+ +1 <= version1.length, version2.length <= 500
version1
and version2
only contain digits and '.'
.version1
and version2
are valid version numbers.version1
and version2
can be stored in a 32-bit integer.Given two integers representing the numerator
and denominator
of a fraction, return the fraction in string format.
If the fractional part is repeating, enclose the repeating part in parentheses.
+ +If multiple answers are possible, return any of them.
+ +It is guaranteed that the length of the answer string is less than 104
for all the given inputs.
+
Example 1:
+Input: numerator = 1, denominator = 2 +Output: "0.5" +
Example 2:
+Input: numerator = 2, denominator = 1 +Output: "2" +
Example 3:
+Input: numerator = 2, denominator = 3 +Output: "0.(6)" +
Example 4:
+Input: numerator = 4, denominator = 333 +Output: "0.(012)" +
Example 5:
+Input: numerator = 1, denominator = 5 +Output: "0.2" ++
+
Constraints:
+ +-231 <= numerator, denominator <= 231 - 1
denominator != 0
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
+ +The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.
+ +Note:
+ ++
Example 1:
+ +Input: numbers = [2,7,11,15], target = 9 +Output: [1,2] +Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2. ++ +
Example 2:
+ +Input: numbers = [2,3,4], target = 6 +Output: [1,3] ++ +
Example 3:
+ +Input: numbers = [-1,0], target = -1 +Output: [1,2] ++ +
+
Constraints:
+ +2 <= nums.length <= 3 * 104
-1000 <= nums[i] <= 1000
nums
is sorted in increasing order.-1000 <= target <= 1000
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
+ +For example:
+ +1 -> A + 2 -> B + 3 -> C + ... + 26 -> Z + 27 -> AA + 28 -> AB + ... ++ +
Example 1:
+ +Input: 1 +Output: "A" ++ +
Example 2:
+ +Input: 28 +Output: "AB" ++ +
Example 3:
+ +Input: 701 +Output: "ZY" +
Given an array nums
of size n
, return the majority element.
The majority element is the element that appears more than ⌊n / 2⌋
times. You may assume that the majority element always exists in the array.
+
Example 1:
+Input: nums = [3,2,3] +Output: 3 +
Example 2:
+Input: nums = [2,2,1,1,1,2,2] +Output: 2 ++
+
Constraints:
+ +n == nums.length
1 <= n <= 5 * 104
-231 <= nums[i] <= 231 - 1
+Follow-up: Could you solve the problem in linear time and in
O(1)
space?Design a data structure that accepts a stream of integers and checks if it has a pair of integers that sum up to a particular value.
+ +Implement the TwoSum
class:
TwoSum()
Initializes the TwoSum
object, with an empty array initially.void add(int number)
Adds number
to the data structure.boolean find(int value)
Returns true
if there exists any pair of numbers whose sum is equal to value
, otherwise, it returns false
.+
Example 1:
+ +Input +["TwoSum", "add", "add", "add", "find", "find"] +[[], [1], [3], [5], [4], [7]] +Output +[null, null, null, null, true, false] + +Explanation +TwoSum twoSum = new TwoSum(); +twoSum.add(1); // [] --> [1] +twoSum.add(3); // [1] --> [1,3] +twoSum.add(5); // [1,3] --> [1,3,5] +twoSum.find(4); // 1 + 3 = 4, return true +twoSum.find(7); // No two integers sum up to 7, return false ++ +
+
Constraints:
+ +-105 <= number <= 105
-231 <= value <= 231 - 1
5 * 104
calls will be made to add
and find
.Given a column title as appear in an Excel sheet, return its corresponding column number.
+ +For example:
+ +A -> 1 + B -> 2 + C -> 3 + ... + Z -> 26 + AA -> 27 + AB -> 28 + ... ++ +
Example 1:
+ +Input: "A" +Output: 1 ++ +
Example 2:
+ +Input: "AB" +Output: 28 ++ +
Example 3:
+ +Input: "ZY" +Output: 701 ++
+
Constraints:
+ +1 <= s.length <= 7
s
consists only of uppercase English letters.s
is between "A" and "FXSHRXW".Given an integer n
, return the number of trailing zeroes in n!
.
Follow up: Could you write a solution that works in logarithmic time complexity?
+ ++
Example 1:
+ +Input: n = 3 +Output: 0 +Explanation: 3! = 6, no trailing zero. ++ +
Example 2:
+ +Input: n = 5 +Output: 1 +Explanation: 5! = 120, one trailing zero. ++ +
Example 3:
+ +Input: n = 0 +Output: 0 ++ +
+
Constraints:
+ +0 <= n <= 104
Implement the BSTIterator
class that represents an iterator over the in-order traversal of a binary search tree (BST):
BSTIterator(TreeNode root)
Initializes an object of the BSTIterator
class. The root
of the BST is given as part of the constructor. The pointer should be initialized to a non-existent number smaller than any element in the BST.boolean hasNext()
Returns true
if there exists a number in the traversal to the right of the pointer, otherwise returns false
.int next()
Moves the pointer to the right, then returns the number at the pointer.Notice that by initializing the pointer to a non-existent smallest number, the first call to next()
will return the smallest element in the BST.
You may assume that next()
calls will always be valid. That is, there will be at least a next number in the in-order traversal when next()
is called.
+
Example 1:
+ +Input +["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"] +[[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []] +Output +[null, 3, 7, true, 9, true, 15, true, 20, false] + +Explanation +BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]); +bSTIterator.next(); // return 3 +bSTIterator.next(); // return 7 +bSTIterator.hasNext(); // return True +bSTIterator.next(); // return 9 +bSTIterator.hasNext(); // return True +bSTIterator.next(); // return 15 +bSTIterator.hasNext(); // return True +bSTIterator.next(); // return 20 +bSTIterator.hasNext(); // return False ++ +
+
Constraints:
+ +[1, 105]
.0 <= Node.val <= 106
105
calls will be made to hasNext
, and next
.+
Follow up:
+ +next()
and hasNext()
to run in average O(1)
time and use O(h)
memory, where h
is the height of the tree?Given a list of non-negative integers nums
, arrange them such that they form the largest number.
Note: The result may be very large, so you need to return a string instead of an integer.
+ ++
Example 1:
+ +Input: nums = [10,2] +Output: "210" ++ +
Example 2:
+ +Input: nums = [3,30,34,5,9] +Output: "9534330" ++ +
Example 3:
+ +Input: nums = [1] +Output: "1" ++ +
Example 4:
+ +Input: nums = [10] +Output: "10" ++ +
+
Constraints:
+ +1 <= nums.length <= 100
0 <= nums[i] <= 109
Given an input string , reverse the string word by word.
+ +Example:
+ +Input: ["t","h","e"," ","s","k","y"," ","i","s"," ","b","l","u","e"] +Output: ["b","l","u","e"," ","i","s"," ","s","k","y"," ","t","h","e"]+ +
Note:
+ +Follow up: Could you do it in-place without allocating extra space?
+All DNA is composed of a series of nucleotides abbreviated as 'A'
, 'C'
, 'G'
, and 'T'
, for example: "ACGAATTCCG"
. When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.
Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.
+ ++
Example 1:
+Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT" +Output: ["AAAAACCCCC","CCCCCAAAAA"] +
Example 2:
+Input: s = "AAAAAAAAAAAAA" +Output: ["AAAAAAAAAA"] ++
+
Constraints:
+ +0 <= s.length <= 105
s[i]
is 'A'
, 'C'
, 'G'
, or 'T'
.You are given an integer array prices
where prices[i]
is the price of a given stock on the ith
day.
Design an algorithm to find the maximum profit. You may complete at most k
transactions.
Notice that you may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
+ ++
Example 1:
+ +Input: k = 2, prices = [2,4,1] +Output: 2 +Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2. ++ +
Example 2:
+ +Input: k = 2, prices = [3,2,6,5,0,3] +Output: 7 +Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4. Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3. ++ +
+
Constraints:
+ +0 <= k <= 100
0 <= prices.length <= 1000
0 <= prices[i] <= 1000
Given an array, rotate the array to the right by k steps, where k is non-negative.
+ +Follow up:
+ ++
Example 1:
+ +Input: nums = [1,2,3,4,5,6,7], k = 3 +Output: [5,6,7,1,2,3,4] +Explanation: +rotate 1 steps to the right: [7,1,2,3,4,5,6] +rotate 2 steps to the right: [6,7,1,2,3,4,5] +rotate 3 steps to the right: [5,6,7,1,2,3,4] ++ +
Example 2:
+ +Input: nums = [-1,-100,3,99], k = 2 +Output: [3,99,-1,-100] +Explanation: +rotate 1 steps to the right: [99,-1,-100,3] +rotate 2 steps to the right: [3,99,-1,-100] ++ +
+
Constraints:
+ +1 <= nums.length <= 2 * 104
-231 <= nums[i] <= 231 - 1
0 <= k <= 105
Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).
+ +Note:
+ +-3
.Follow up: If this function is called many times, how would you optimize it?
+ ++
Example 1:
+ +Input: n = 00000000000000000000000000001011 +Output: 3 +Explanation: The input binary string 00000000000000000000000000001011 has a total of three '1' bits. ++ +
Example 2:
+ +Input: n = 00000000000000000000000010000000 +Output: 1 +Explanation: The input binary string 00000000000000000000000010000000 has a total of one '1' bit. ++ +
Example 3:
+ +Input: n = 11111111111111111111111111111101 +Output: 31 +Explanation: The input binary string 11111111111111111111111111111101 has a total of thirty one '1' bits. ++ +
+
Constraints:
+ +32
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
+ +Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
+ ++
Example 1:
+ +Input: nums = [1,2,3,1] +Output: 4 +Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). + Total amount you can rob = 1 + 3 = 4. ++ +
Example 2:
+ +Input: nums = [2,7,9,3,1] +Output: 12 +Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). + Total amount you can rob = 2 + 9 + 1 = 12. ++ +
+
Constraints:
+ +0 <= nums.length <= 100
0 <= nums[i] <= 400
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
+ +Example:
+ +Input: [1,2,3,null,5,null,4] +Output: [1, 3, 4] +Explanation: + + 1 <--- + / \ +2 3 <--- + \ \ + 5 4 <--- +