柠檬水找零 - 简单

*************

c++

topic: 860. 柠檬水找零 - 力扣(LeetCode)

*************

Give it a inspection.

I have a really annoying motherfxxk colleague who always disturbs you when you have a snooze in the noon. He 嗑瓜子 when we people are having a snooze. He talks loudly and 吧唧嘴 when eating other things. He ebjoys making noise. Poor he. You will never experience that you are disturbed by more than noise when you are thing why the code goes wrong. He has a wife and two kids. Why does this kind of person have a household. I donot understand.

I need to count how many 5 dollors in the vector. It has to need some mathrmatic tricks. 

if I receive a 10, I will give out a 5.

if I receive a 20, I will give out a 5 and a 10; if I donot have a 10, I will give out three 5. So iterate through the entire vector and count the numbers of 5 and 10.

class Solution {
public:
    bool lemonadeChange(vector<int>& bills) {
        
        int five = 0; // 记录5的数量
        int ten  = 0; // 记录10的数量
        int n    = bills.size();
    }
};

attend to 5.

class Solution {
public:
    bool lemonadeChange(vector<int>& bills) {
        
        int five = 0; // 记录5的数量
        int ten  = 0; // 记录10的数量
        int n    = bills.size();

        for (int i = 0; i < n; i++)
        {
            int bill = bills[i];

            // 记录5的数量
            if (bill == 5)
            {
                five++;
            }
        }
    }
};

attend to 10.

class Solution {
public:
    bool lemonadeChange(vector<int>& bills) {
        
        int five = 0; // 记录5的数量
        int ten  = 0; // 记录10的数量
        int n    = bills.size();

        for (int i = 0; i < n; i++)
        {
            int bill = bills[i];

            // 记录5的数量
            if (bill == 5)
            {
                five++;
            }

            // 记录10的数量
            else if (bill == 10)
            {
                ten++;
                five--;
            }
        }
    }
};

attend to 20. Use the combination of 5 and 10 in preference.

class Solution {
public:
    bool lemonadeChange(vector<int>& bills) {
        
        int five = 0; // 记录5的数量
        int ten  = 0; // 记录10的数量
        int n    = bills.size();

        for (int i = 0; i < n; i++)
        {
            int bill = bills[i];

            // 记录5的数量
            if (bill == 5)
            {
                five++;
            }

            // 记录10的数量
            else if (bill == 10)
            {
                ten++;
                five--;
            }

            // 处理20
            else 
            {
                if (five > 0 && ten > 0)
                {
                    five--;
                    ten--;
                }
                else if (five >= 3)
                {
                    five = five - 3;
                }
                else
                {
                    return false;
                }
            }
        }

        return true;
    }
};

debug everyday.

  • 输入 bills = [10, 10] 的执行流程:
  • 初始状态

    • five = 0(没有5美元)
    • ten = 0(没有10美元)
  • 第1个顾客支付10美元

    • 执行 else if (bill == 10) 分支:
      • ten++ → ten = 1
      • five-- → five = -1这里出错了! )

should add a return here.

class Solution {
public:
    bool lemonadeChange(vector<int>& bills) {
        
        int five = 0; // 记录5的数量
        int ten  = 0; // 记录10的数量
        int n    = bills.size();

        for (int i = 0; i < n; i++)
        {
            int bill = bills[i];

            // 记录5的数量
            if (bill == 5)
            {
                five++;
            }

            // 记录10的数量
            else if (bill == 10)
            {
                if (five == 0) return false;
                ten++;
                five--;
            }

            // 处理20
            else 
            {
                if (five > 0 && ten > 0)
                {
                    five--;
                    ten--;
                }
                else if (five >= 3)
                {
                    five = five - 3;
                }
                else
                {
                    return false;
                }
            }
        }

        return true;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值