Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2467e78

Browse files
committed
[Function add]
1. Add leetcode solutions.
1 parent3a98c92 commit2467e78

File tree

4 files changed

+293
-0
lines changed

4 files changed

+293
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
##250. Count Univalue Subtrees
2+
3+
###Question
4+
Given a binary tree, count the number of uni-value subtrees.
5+
6+
A Uni-value subtree means all nodes of the subtree have the same value.
7+
8+
```
9+
Example :
10+
11+
Input: root = [5,1,5,5,5,null,5]
12+
13+
5
14+
/ \
15+
1 5
16+
/ \ \
17+
5 5 5
18+
19+
Output: 4
20+
```
21+
22+
23+
###Solutions
24+
* Method 1: Recursion from bottom to top.
25+
```Java
26+
/**
27+
* Definition for a binary tree node.
28+
* public class TreeNode {
29+
* int val;
30+
* TreeNode left;
31+
* TreeNode right;
32+
* TreeNode(int x) { val = x; }
33+
* }
34+
*/
35+
classSolution {
36+
privateint res=0;
37+
privateTreeNode root;
38+
publicintcountUnivalSubtrees(TreeNoderoot) {
39+
if(root==null)return0;
40+
this.root= root;
41+
subTreeValue(root);
42+
return res;
43+
}
44+
// return a value
45+
// if -1: means current tree is not unival
46+
// otherwise will return the univalue
47+
privateintsubTreeValue(TreeNodenode){
48+
if(node.left==null&& node.right==null){// leaf is always true.
49+
res+=1;
50+
return node.val;
51+
}
52+
int left=-1, right=-1;
53+
if(node.left!=null) left= subTreeValue(node.left);
54+
if(node.right!=null) right= subTreeValue(node.right);
55+
if(node.left!=null&& node.right!=null&& left!=-1&& right!=-1){
56+
if(left== right&& left== node.val){
57+
res+=1;
58+
return left;
59+
}
60+
}elseif(node.right==null&& left!=-1){// right subtree is empty
61+
if(left== node.val){
62+
res+=1;
63+
return left;
64+
}
65+
}elseif(node.left==null&& right!=-1){
66+
if(right== node.val){
67+
res+=1;
68+
return right;
69+
}
70+
}
71+
return-1;
72+
}
73+
}
74+
```

‎leetcode/427. Construct Quad Tree.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
##427. Construct Quad Tree
2+
3+
###Question
4+
We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or false. The root node represents the whole grid. For each node, it will be subdivided into four children nodes until the values in the region it represents are all the same.
5+
6+
Each node has another two boolean attributes : isLeaf and val. isLeaf is true if and only if the node is a leaf node. The val attribute for a leaf node contains the value of the region it represents.
7+
8+
Your task is to use a quad tree to represent a given grid. The following example may help you understand the problem better:
9+
10+
Given the 8 x 8 grid below, we want to construct the corresponding quad tree:
11+
![Imgur](https://i.imgur.com/RQujgQ8.png)
12+
13+
The corresponding quad tree should be as following, where each node is represented as a (isLeaf, val) pair.
14+
![Imgur](https://i.imgur.com/0GegYTc.png)
15+
For the non-leaf nodes, val can be arbitrary, so it is represented as*.
16+
17+
Note:
18+
1. N is less than 1000 and guaranteened to be a power of 2.
19+
2. If you want to know more about the quad tree, you can refer to its wiki.
20+
21+
22+
23+
###Solutions
24+
* Method 1: Recursion from bottom to top.
25+
```Java
26+
/*
27+
// Definition for a QuadTree node.
28+
class Node {
29+
public boolean val;
30+
public boolean isLeaf;
31+
public Node topLeft;
32+
public Node topRight;
33+
public Node bottomLeft;
34+
public Node bottomRight;
35+
36+
public Node() {}
37+
38+
public Node(boolean _val,boolean _isLeaf,Node _topLeft,Node _topRight,Node _bottomLeft,Node _bottomRight) {
39+
val = _val;
40+
isLeaf = _isLeaf;
41+
topLeft = _topLeft;
42+
topRight = _topRight;
43+
bottomLeft = _bottomLeft;
44+
bottomRight = _bottomRight;
45+
}
46+
};
47+
*/
48+
classSolution {
49+
privateint[][] grid;
50+
publicNodeconstruct(int[][]grid) {
51+
this.grid= grid;
52+
int len= grid.length;
53+
return dfs(0, grid.length-1,0, grid.length-1);
54+
}
55+
privateNodedfs(intxs,intxe,intys,intye){
56+
Node node=newNode();
57+
if(xs== xe){
58+
node.val= (grid[xs][ys]==1);
59+
node.isLeaf=true;
60+
return node;
61+
}
62+
Node upLeft= dfs(xs, xs+ (xe- xs)/2, ys, ys+ (ye- ys)/2);
63+
Node upRight= dfs(xs, xs+ (xe- xs)/2, ys+ (ye- ys)/2+1, ye);
64+
Node bottomLeft= dfs(xs+ (xe- xs)/2+1, xe, ys, ys+ (ye- ys)/2);
65+
Node bottomRight= dfs(xs+ (xe- xs)/2+1, xe, ys+ (ye- ys)/2+1, ye);
66+
if(bottomLeft.isLeaf&& bottomRight.isLeaf&& upLeft.isLeaf&& upRight.isLeaf
67+
&& bottomLeft.val== bottomRight.val&& bottomRight.val== upLeft.val&& upLeft.val== upRight.val){
68+
node.isLeaf=true;
69+
node.val= bottomLeft.val;
70+
}else{
71+
node.bottomLeft= bottomLeft;
72+
node.bottomRight= bottomRight;
73+
node.topLeft= upLeft;
74+
node.topRight= upRight;
75+
}
76+
return node;
77+
}
78+
}
79+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
##448. Find All Numbers Disappeared in an Array
2+
3+
###Question
4+
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
5+
6+
Find all the elements of[1, n] inclusive that do not appear in this array.
7+
8+
Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.
9+
10+
```
11+
Example:
12+
13+
Input:
14+
[4,3,2,7,8,2,3,1]
15+
16+
Output:
17+
[5,6]
18+
```
19+
20+
###Solutions
21+
* Method 1: O(n) S(n)
22+
```Java
23+
classSolution {
24+
publicList<Integer>findDisappearedNumbers(int[]nums) {
25+
Set<Integer> set=newHashSet<>();
26+
for(int num: nums) set.add(num);
27+
List<Integer> res=newArrayList<>();
28+
for(int i=1; i<= nums.length; i++)
29+
if(!set.contains(i))
30+
res.add(i);
31+
return res;
32+
}
33+
}
34+
```
35+
36+
*Method2: O(n)+ S(0)
37+
*Since the index range is same as the length of the array, once we read a value, we change the number saved in that index-1 to-1.
38+
*We need to recursively to change the index.
39+
*Finally we check the indices that are not-1.
40+
```Java
41+
classSolution {
42+
publicList<Integer>findDisappearedNumbers(int[]nums) {
43+
for(int i=0; i< nums.length; i++){
44+
if(nums[i]==-1)continue;
45+
int index= nums[i];
46+
while(nums[index-1]!=-1&& nums[index-1]!= index){
47+
int nextIndex= nums[index-1];
48+
nums[index-1]=-1;
49+
index= nextIndex;
50+
}
51+
if(nums[index-1]== index) nums[index-1]=-1;
52+
}
53+
List<Integer> res=newArrayList<>();
54+
for(int i=0; i< nums.length; i++){
55+
if(nums[i]>0) res.add(i+1);
56+
}
57+
return res;
58+
}
59+
}
60+
```
61+
62+
###Reference
63+
1. [LeetCode-448FindAllNumbersDisappeared in anArray (寻找缺失多个数字)](https://blog.csdn.net/Koala_Tree/article/details/78349644)

‎leetcode/518. Coin Change 2.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
##518. Coin Change 2
2+
3+
###Question
4+
You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin.
5+
6+
```
7+
Example 1:
8+
9+
Input: amount = 5, coins = [1, 2, 5]
10+
Output: 4
11+
Explanation: there are four ways to make up the amount:
12+
5=5
13+
5=2+2+1
14+
5=2+1+1+1
15+
5=1+1+1+1+1
16+
17+
Example 2:
18+
19+
Input: amount = 3, coins = [2]
20+
Output: 0
21+
Explanation: the amount of 3 cannot be made up just with coins of 2.
22+
23+
Example 3:
24+
25+
Input: amount = 10, coins = [10]
26+
Output: 1
27+
```
28+
29+
30+
Note:
31+
32+
You can assume that
33+
1. 0 <= amount <= 5000
34+
2. 1 <= coin <= 5000
35+
3. the number of coins is less than 500
36+
4. the answer is guaranteed to fit into signed 32-bit integer
37+
38+
###Solutions
39+
* Method 1: Recursion TLE
40+
```Java
41+
class Solution {
42+
private int amount;
43+
private int[] coins;
44+
private int res;
45+
public int change(int amount, int[] coins) {
46+
this.amount = amount;
47+
this.coins = coins;
48+
dfs(0, 0);
49+
return res;
50+
}
51+
private void dfs(int index, int sum){
52+
if(sum == amount) res++;
53+
else if(sum < amount){
54+
for(int i = index; i < coins.length; i++){
55+
dfs(i, sum + coins[i]);
56+
}
57+
}
58+
}
59+
}
60+
```
61+
62+
* Method 2: dp
63+
```Java
64+
class Solution {
65+
public int change(int amount, int[] coins) {
66+
int[] dp = new int[amount + 1];
67+
dp[0] = 1;
68+
for(int coin :coins){
69+
for(int i = 1; i <= amount; i++){
70+
if(i - coin >= 0)
71+
dp[i] += dp[i - coin];
72+
}
73+
}
74+
return dp[amount];
75+
}
76+
}
77+
```

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp