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).
Example 1:
!https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg
123Input: root = [1,2,2,3,4,4,3]Output: true
Example 2:
!https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg
123Input: root = [1,2,2,null,3,null,3]Output: false
Constraints:
The number of nodes in the tree is in the range [1, 1000].
100 <= Node.val <= 100
Follow up:
Could you solve it both r ...
Leetcode#322. Coin Change
ProblemYou are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
1234Input: coins = [1,2,5], amount = 11Output: 3Explanation: 11 = 5 + 5 + 1
Example 2:
123Input: coins = [2], ...
Leetcode#104. Maximum Depth of Binary Tree
ProblemGiven 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:
!https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg
123Input: root = [3,9,20,null,null,15,7]Output: 3
Example 2:
123Input: root = [1,null,2]Output: 2
Constraints:
The number of nodes in the tree is in the range [0, 104].
100 <= Node.val <= 100
Solve解11234567891011121314151617 ...
Leetcode#380. Insert Delete GetRandom O(1)
ProblemImplement the RandomizedSet class:
RandomizedSet() Initializes the RandomizedSet object.
bool insert(int val) Inserts an item val into the set if not present. Returns true if the item was not present, false otherwise.
bool remove(int val) Removes an item val from the set if present. Returns true if the item was present, false otherwise.
int getRandom() Returns a random element from the current set of elements (it’s guaranteed that at least one element exists when this method is called). ...
Leetcode#274. H-Index
ProblemGiven an array of integers citations where citations[i] is the number of citations a researcher received for their ith paper, return the researcher’s h-index.
According to the definition of h-index on Wikipedia: The h-index is defined as the maximum value of h such that the given researcher has published at least h papers that have each been cited at least h times.
Example 1:
12345Input: citations = [3,0,6,1,5]Output: 3Explanation: [3,0,6,1,5] means the researcher has 5 papers in total an ...
Leetcode#122. Best Time to Buy and Sell Stock II
ProblemYou are given an integer array prices where prices[i] is the price of a given stock on the ith day.
On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.
Find and return the maximum profit you can achieve.
Example 1:
123456Input: prices = [7,1,5,3,6,4]Output: 7Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.Then buy ...
Leetcode#189. Rotate Arra
ProblemGiven an integer array nums, rotate the array to the right by k steps, where k is non-negative.
Example 1:
1234567Input: nums = [1,2,3,4,5,6,7], k = 3Output: [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:
123456Input: nums = [-1,-100,3,99], k = 2Output: [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]
...
Leetcode#169. Majority Element
ProblemGiven 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:
123Input: nums = [3,2,3]Output: 3
Example 2:
123Input: nums = [2,2,1,1,1,2,2]Output: 2
Constraints:
n == nums.length
1 <= n <= 5 * 104
109 <= nums[i] <= 109
Follow-up:
Could you solve the problem in linear time and in
1O(1)
space?
Solve解112345678910class Solutio ...
Leetcode#80. Remove Duplicates from Sorted Array II
ProblemGiven an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same.
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result ...
Leetcode#2924. Find Champion II
ProblemThere are n teams numbered from 0 to n - 1 in a tournament; each team is also a node in a DAG.
You are given the integer n and a 0-indexed 2D integer array edges of length m representing the DAG, where edges[i] = [ui, vi] indicates that there is a directed edge from team ui to team vi in the graph.
A directed edge from a to b in the graph means that team a is stronger than team b and team b is weaker than team a.
Team a will be the champion of the tournament if there is no team b that is ...