题目来源:https://leetcode.cn/problems/lemonade-change/description/
C++题解:由于收到的钱币只有5,10,20三种,对于5元直接收,对于10元找零1张5元,对于20元找零15元,可以找零10+5或者3*5,但是5元用处较多,所有优先找零10+5。当5元不够的时候,return false。(其实可以不记录20元的数目。)
class Solution {
public:bool lemonadeChange(vector<int>& bills) {int hav[3] = {0};int len = bills.size();for(int i = 0; i < len; i++) {if(bills[i] == 5) hav[0]++;else if(bills[i] == 10) {hav[1]++;hav[0] = hav[0] - 1;if(hav[0] < 0) return false;}else {hav[2]++;if(hav[1] > 0) {hav[1]--;hav[0]--;}else hav[0] = hav[0] - 3;if(hav[0] < 0) return false;}}return true;}
};