Leetcode#70. Climbing Stairs
ProblemYou are climbing a staircase. It takes n steps to reach the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Example 1:
123456Input: n = 2Output: 2Explanation: There are two ways to climb to the top.1. 1 step + 1 step2. 2 steps
Example 2:
1234567Input: n = 3Output: 3Explanation: There are three ways to climb to the top.1. 1 step + 1 step + 1 step2. 1 step + 2 steps3. 2 steps + 1 step
Constraints:
1 <= n <= 45
Solve用一般的**fibona ...
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#87. Scramble String
ProblemWe can scramble a string s to get a string t using the following algorithm:
If the length of the string is 1, stop.
If the length of the string is > 1, do the following:
Split the string into two non-empty substrings at a random index, i.e., if the string is s, divide it to x and y where s = x + y.
Randomly decide to swap the two substrings or to keep them in the same order. i.e., after this step, s may become s = x + y or s = y + x.
Apply step 1 recursively on each of the two substri ...
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#226. Invert Binary Tree
ProblemGiven the root of a binary tree, invert the tree, and return its root.
Example 1:
123Input: root = [4,2,7,1,3,6,9]Output: [4,7,2,9,6,3,1]
Example 2:
123Input: root = [2,1,3]Output: [2,3,1]
Example 3:
123Input: root = []Output: []
Constraints:
The number of nodes in the tree is in the range [0, 100].
100 <= Node.val <= 100
Solve1234567891011121314151617181920# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# se ...
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#121. Best Time to Buy and Sell Stock
ProblemYou 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:
12345Input: prices = [7,1,5,3,6,4]Output: 5Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.Note that buyi ...
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 ...