Leetcode#198. House Robber
可以先看看leetcode70
ProblemYou 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 systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without ...
Leetcode#146. LRU Cache
ProblemDesign 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 recent ...
Leetcode#117. Populating Next Right Pointers in Each Node II
leetcode116
ProblemGiven a binary tree
1234567struct Node { int val; Node *left; Node *right; Node *next;}
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
Initially, all next pointers are set to NULL.
Example 1:
1234Input: root = [1,2,3,4,5,null,7]Output: [1,#,2,3,#,4,5,7,#]Explanation:Given the above binary tree (Figure A), your function should populate each next pointer to point to its next righ ...
Leetcode#116. Populating Next Right Pointers in Each Node
ProblemYou are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
1234567struct Node { int val; Node *left; Node *right; Node *next;}
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
Initially, all next pointers are set to NULL.
Example 1:
1234Input: root = [1,2,3,4,5,6,7]Output: [1,#,2,3,#,4,5,6,7,#]E ...
Leetcode#201. Bitwise AND of Numbers Range
ProblemGiven two integers left and right that represent the range [left, right], return the bitwise(位元運算) AND of all numbers in this range, inclusive.
Example 1:
123Input: left = 5, right = 7Output: 4
Example 2:
123Input: left = 0, right = 0Output: 0
Example 3:
123Input: left = 1, right = 2147483647Output: 0
Constraints:
0 <= left <= right <= 2^31 - 1
Solve對區間的數字進行位元運算
12345678根據位元的值(0或1)進行邏輯運算位元 AND(&):對兩個二進制數進行對應位元的 AND 運算。當兩個對應位元都為1時,結果的對應位元為1,否則為0。10101010 (170)& 1100 ...
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: ...