forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoinChange.java
More file actions
48 lines (43 loc) · 1.14 KB
/
CoinChange.java
File metadata and controls
48 lines (43 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package LeetCode;
import java.util.*;
/**
* @Classname CoinChange
* @Description TODO
* @Date 19-2-28 下午1:38
* @Created by mao<[email protected]>
*/
public class CoinChange {
private HashMap<Integer,Integer> num=new HashMap<>();
public int coinChange(int[] coins, int amount) {
int result=-1;
if(amount<0){
return -1;
}
List<Integer> temp=new ArrayList<>();
for(int i=0;i<coins.length;i++){
if(amount==coins[i]){
return 1;
}
int money=amount-coins[i];
int x=0;
if(num.keySet().contains(money)){
x=num.get(money);
}else {
x=coinChange(coins,money);
num.put(money,x);
}
if(x>0){
temp.add(x);
}
}
if(!temp.isEmpty()){
result= Collections.min(temp)+1;
}
return result;
}
public static void main(String[] args){
CoinChange coinChange=new CoinChange();
int[] coins={2};
System.out.println(coinChange.coinChange(coins,100));
}
}