LeetCode题解之Lemonade Change

1、题目描述

2、问题分析

使用贪心算法。

3、代码

 1 class Solution {
 2 public:
 3     
 4     bool lemonadeChange(vector<int>& bills) {
 5         int five = 0, ten = 0;
 6         for (int &x : bills) {
 7             if (x == 5)
 8                 five++;
 9             else if (x == 10)
10                 five--, ten++;
11             else  {
12                 if (ten > 0)
13                     ten--, five--;
14                 else 
15                     five -= 3;
16             }
17             
18             if (five < 0)
19                 return false;
20         }
21         return true;
22     }
23     
24     
25     
26     
27 };

 

posted @ 2019-03-17 12:02  山里的小勇子  阅读(141)  评论(0编辑  收藏  举报