Leetcode#45. Jump Game II
ProblemYou are given a 0-indexed array of integers nums of length n. You are initially positioned at nums[0].
Each element nums[i] represents the maximum length of a forward jump from index i. In other words, if you are at nums[i], you can jump to any nums[i + j] where:
0 <= j <= nums[i] and
i + j < n
Return the minimum number of jumps to reach nums[n - 1]. The test cases are generated such that you can reach nums[n - 1].
Example 1:
1234Input: nums = [2,3,1,1,4]Output: 2Explanation: T ...
Leetcode#55. Jump Game
ProblemYou are given an integer array nums. You are initially positioned at the array’s first index, and each element in the array represents your maximum jump length at that position.
Return true if you can reach the last index, or false otherwise.
Example 1:
1234Input: nums = [2,3,1,1,4]Output: trueExplanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:
1234Input: nums = [3,2,1,0,4]Output: falseExplanation: You will always arrive at index 3 no matter what. Its m ...
Leetcode#34. Find First and Last Position of Element in Sorted Array
ProblemGiven an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.
If target is not found in the array, return [-1, -1].
You must write an algorithm with O(log n) runtime complexity.
Example 1:
123Input: nums = [5,7,7,8,8,10], target = 8Output: [3,4]
Example 2:
123Input: nums = [5,7,7,8,8,10], target = 6Output: [-1,-1]
Example 3:
123Input: nums = [], target = 0Output: [-1,-1]
Constraints:
0 <= nums.length <= 10^5
10^9 & ...
Leetcode#105. Construct Binary Tree from Preorder and Inorder Traversal
ProblemGiven 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.
Example 1:
!https://assets.leetcode.com/uploads/2021/02/19/tree.jpg
123Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]Output: [3,9,20,null,null,15,7]
Example 2:
123Input: preorder = [-1], inorder = [-1]Output: [-1]
Constraints:
1 <= preorder.length <= 3000
inorder.length == pr ...
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.
Example 1:
!https://assets.leetcode.com/uploads/2021/02/19/rev2ex2.jpg
123Input: head = [1,2,3,4,5], left = 2, right = 4Output: [1,4,3,2,5]
Example 2:
123Input: head = [5], left = 1, right = 1Output: [5]
Constraints:
The number of nodes in the list is n.
1 <= n <= 500
500 <= Node.val &l ...
Leetcode#224. Basic Calculator
ProblemGiven a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation.
Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval().
Example 1:
123Input: s = "1 + 1"Output: 2
Example 2:
123Input: s = " 2-1 + 2 "Output: 3
Example 3:
123Input: s = "(1+(4+5+2)-3)+(6+8)"Output: 23
Constraints:
1 <= s.length <= 3 * 10^5
s consist ...
Leetcode#52. N-Queens II
ProblemThe 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:
![(https://assets.leetcode.com/uploads/2020/11/13/queens.jpg)
!https://assets.leetcode.com/uploads/2020/11/13/queens.jpg
1234Input: n = 4Output: 2Explanation: There are two distinct solutions to the 4-queens puzzle as shown.
Example 2:
123Input: n = 1Output: 1
Constraints:
...
Leetcode#530. Minimum Absolute Difference in BST
ProblemGiven the root of a Binary Search Tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree.
Example 1:
!https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg
123Input: root = [4,2,6,1,3]Output: 1
Example 2:
!https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg
123Input: root = [1,0,48,null,null,12,49]Output: 1
Constraints:
The number of nodes in the tree is in the range [2, 10^4].
0 <= Node.val <= 10^5
Solve法一由於有有序性質、且正整 ...
Leetcode#9. Palindrome Number
ProblemGiven an integer x, return true if x is a
palindrome
, and false otherwise
1234PalindromeAn integer is a palindrome when it reads the same forward and backward.For example, 121 is a palindrome while 123 is not
.
Example 1:
1234Input: x = 121Output: trueExplanation: 121 reads as 121 from left to right and from right to left.
Example 2:
1234Input: x = -121Output: falseExplanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Ex ...
Leetcode#149. Max Points on a Line
ProblemGiven an array of points where points[i] = [xi, yi] represents a point on the X-Y plane, return the maximum number of points that lie on the same straight line.
Example 1:
!https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg
123Input: points = [[1,1],[2,2],[3,3]]Output: 3
Example 2:
!https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg
123Input: points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]Output: 4
Constraints:
1 <= points.length <= 300
points[i].length == 2
10^4 & ...