Leetcode#120. Triangle
leetcode62
ProblemGiven a triangle array, return the minimum path sum from top to bottom.
For each step, you may move to an adjacent number of the row below. More formally, if you are on index i on the current row, you may move to either index i or index i + 1 on the next row.
Example 1:
123456789Input: triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]Output: 11Explanation: The triangle looks like: 2 3 4 6 5 74 1 8 3The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
Exa ...
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#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#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 ...