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#53. Maximum Subarray
ProblemGiven an integer array nums, find the
subarray
with the largest sum, and return
its sum
.
Example 1:
1234Input: nums = [-2,1,-3,4,-1,2,1,-5,4]Output: 6Explanation: The subarray [4,-1,2,1] has the largest sum 6.
Example 2:
1234Input: nums = [1]Output: 1Explanation: The subarray [1] has the largest sum 1.
Example 3:
1234Input: nums = [5,4,-1,7,8]Output: 23Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.
Constraints:
1 <= nums.length <= 10^5
10^4 <= nums[i] <= 1 ...
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#190. Reverse Bits
ProblemReverse bits of a given 32 bits unsigned integer.
Note:
Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer’s internal binary representation is the same, whether it is signed or unsigned.
In Java, the compiler represents the signed integers using 2’s complement notation. Therefore, in Example 2 above, the input represents the sig ...
Leetcode#23. Merge k Sorted Lists
ProblemYou are given an array of k linked-lists lists, each linked-list is sorted in ascending order.
Merge all the linked-lists into one sorted linked-list and return it.
Example 1:
1234567891011Input: lists = [[1,4,5],[1,3,4],[2,6]]Output: [1,1,2,3,4,4,5,6]Explanation: The linked-lists are:[ 1->4->5, 1->3->4, 2->6]merging them into one sorted list:1->1->2->3->4->4->5->6
Example 2:
123Input: lists = []Output: []
Example 3:
123Input: lists = [[]]Output: [ ...
Leetcode#427. Construct Quad Tree
ProblemGiven a n * n matrix grid of 0's and 1's only. We want to represent grid with a Quad-Tree.
Return the root of the Quad-Tree representing grid.
A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:
val: True if the node represents a grid of 1’s or False if the node represents a grid of 0’s. Notice that you can assign the val to True or False when isLeaf is False, and both are accepted in the answer.
isLea ...
Leetcode#148. Sort Listk
ProblemGiven the head of a linked list, return the list after sorting it in ascending order.
Example 1:
123Input: head = [4,2,1,3]Output: [1,2,3,4]
Example 2:
123Input: head = [-1,5,3,4,0]Output: [-1,0,3,4,5]
Example 3:
12Input: head = []Output: []
Solve1234567891011121314151617181920212223242526272829303132333435363738394041424344class Solution: def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]: # 沒有 或只只有一個 代表已經排好了 if not head or not head.next: ...