#力扣 LeetCode860. 柠檬水找零 @FDDLC
发布日期:2021-06-30 21:01:38 浏览次数:2 分类:技术文章

本文共 1129 字,大约阅读时间需要 3 分钟。

题目描述:

 

Java代码:

class Solution { //bills[i] 不是 5 就是 10 或是 20    public boolean lemonadeChange(int[] bills) {        int[] count=new int[21];        for(int bill:bills){            if(bill==5)count[5]++;            else if(bill==10){                if(count[5]==0)return false;                count[5]--;                count[10]++;            }else{ //20                if(count[10]*count[5]>0){                    count[10]--;                    count[5]--;                }else if(count[5]>=3)count[5]-=3;                else return false;            }        }        return true;    }}

 

Java代码二:进行了优化,核心思路同上

class Solution { //bills[i] 不是 5 就是 10 或是 20    public boolean lemonadeChange(int[] bills) {        int five=0,ten=0;        for(int bill:bills){            if(bill==5)five++;            else if(bill==10){                if(five==0)return false;                five--;                ten++;            }else{ //20                if(five*ten>0){                    ten--;                    five--;                }else if(five>=3)five-=3;                else return false;            }        }        return true;    }}

 

 

转载地址:https://liuxingchang.blog.csdn.net/article/details/111996716 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:#力扣 LeetCode414. 第三大的数 @FDDLC
下一篇:#力扣 LeetCode1512. 好数对的数目 @FDDLC

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月13日 15时15分43秒