一、题目
二、代码
class Solution
{
//五元是最好的 尽量先用十元
public boolean lemonadeChange(int[] bills)
{
int i;
int length;
length = bills.length;
System.out.println(" 长度 "+length);
//5元不用找零 获得五元零钱
//10元找零5元
//20元找零15元 优先用 1 10元 1 5元 , 其次3个5元
int count_five = 0;
int count_ten = 0;
for(i=0; i<length;i++)
{
if(bills[i]==5)
{
count_five++;
}
else if(bills[i]==10)
{
count_ten++;
count_five--;
if(count_five<0) return false;
}
else
{
if(count_ten>0)
{
count_ten--;
if(count_five<=0) return false;
else count_five--;
}
else
{
if(count_five>=3)
{
count_five = count_five-3;
}
else
{
return false;
}
}
}
}
return true;
}
}
三、运行结果
四、附录
二刷
class Solution
{
//优先用面额大的钞票
public boolean lemonadeChange(int[] bills)
{
int i;
int length = bills.length;
int count_5 = 0;
int count_10 = 0;
for(i=0;i<length;i++)
{
if(bills[i]==5) count_5++;
else if(bills[i] == 10 )
{
count_10++;
count_5--;
}
else
{
if(count_10>0)
{
count_10--;
count_5--;
}
else count_5 = count_5-3;
}
if(count_5<0||count_10<0) return false;
}
//System.out.println("length "+length);
return true;
}
}