Leetcode#133. Clone Graph
ProblemGiven a reference of a node in a connected undirected graph.
Return a deep copy (clone) of the graph.
Each node in the graph contains a value (int) and a list (List[Node]) of its neighbors.
12345class Node { public int val; public List<Node> neighbors;}
Test case format:
For simplicity, each node’s value is the same as the node’s index (1-indexed). For example, the first node with val == 1, the second node with val == 2, and so on. The graph is represented in the te ...
Leetcode#1319. Number of Operations to Make Network Connected
ProblemThere are n computers numbered from 0 to n - 1 connected by ethernet cables connections forming a network where connections[i] = [ai, bi] represents a connection between computers ai and bi. Any computer can reach any other computer directly or indirectly through the network.
You are given an initial computer network connections. You can extract certain cables between two directly connected computers, and place them between any pair of disconnected computers to make them directly connecte ...
Leetcode#101. Symmetric Tree
ProblemGiven the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
Example 1:
!https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg
123Input: root = [1,2,2,3,4,4,3]Output: true
Example 2:
!https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg
123Input: root = [1,2,2,null,3,null,3]Output: false
Constraints:
The number of nodes in the tree is in the range [1, 1000].
100 <= Node.val <= 100
Follow up:
Could you solve it both r ...
Leetcode#322. Coin Change
ProblemYou are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
1234Input: coins = [1,2,5], amount = 11Output: 3Explanation: 11 = 5 + 5 + 1
Example 2:
123Input: coins = [2], ...
Leetcode#104. Maximum Depth of Binary Tree
ProblemGiven the root of a binary tree, return its maximum depth.
A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Example 1:
!https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg
123Input: root = [3,9,20,null,null,15,7]Output: 3
Example 2:
123Input: root = [1,null,2]Output: 2
Constraints:
The number of nodes in the tree is in the range [0, 104].
100 <= Node.val <= 100
Solve解11234567891011121314151617 ...
Leetcode#909. Snakes and Ladders
ProblemYou are given an n x n integer matrix board where the cells are labeled from 1 to n^2 in a Boustrophedon style starting from the bottom left of the board (i.e. board[n - 1][0]) and alternating direction each row.
You start on square 1 of the board. In each move, starting from square curr, do the following:
Choose a destination square next with a label in the range [curr + 1, min(curr + 6, n^2)].
This choice simulates the result of a standard 6-sided die roll: i.e., there are always at mo ...
Leetcode#684. Redundant Connection
ProblemIn this problem, a tree is an undirected graph that is connected and has no cycles.
You are given a graph that started as a tree with n nodes labeled from 1 to n, with one additional edge added. The added edge has two different vertices chosen from 1 to n, and was not an edge that already existed. The graph is represented as an array edges of length n where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the graph.
Return an edge that can be removed so that ...
Leetcode#200. Number of Islands
ProblemGiven an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.
An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
12345678Input: grid = [ ["1","1","1","1","0"], ["1","1","0","1","0"] ...
Leetcode#841. Keys and Rooms
ProblemThere are n rooms labeled from 0 to n - 1 and all the rooms are locked except for room 0. Your goal is to visit all the rooms. However, you cannot enter a locked room without having its key.
When you visit a room, you may find a set of distinct keys in it. Each key has a number on it, denoting which room it unlocks, and you can take all of them with you to unlock the other rooms.
Given an array rooms where rooms[i] is the set of keys that you can obtain if you visited room i, return true ...
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法一由於有有序性質、且正整 ...