思路:动态规划
class Solution { public: int coinChange(vector<int>& coins, int amount) { //定义变量和初始化 sort(coins.begin(),coins.end(),less<int>()); int len=coins.size(); vector<int> dp(max(amount,len)+1,-1);dp[0]=0; for(int i=1;i<=amount;i++){ for(int j=0;j<len;j++){ if(i-coins[j]>=0&&dp[i-coins[j]]!=-1) { if(dp[i]==-1||dp[i]>dp[i-coins[j]]+1) dp[i]=dp[i-coins[j]]+1; } } } //for(int i=1;i<=amount;i++){cout<<dp[i]<<endl;} return dp[amount]; } };