[关闭]
@Lin-- 2020-02-05T05:22:57.000000Z 字数 830 阅读 334

lemonadeChange

Leetcode


题目分析:一个柠檬5元,有5,10,20的货币,一次售出一个柠檬,商家初始无货币若是。
若商家能找零钱,为true;否则,为false;
那么,题目有三种变量,5,10,20的货币;初始化值皆为0;
遍历数组,若是5元,则5元货币+1;若是10元,则10元货币+1,5元货币-1;若无5元货币,返回false;
若是20,则先10元货币-1,5元货币-1;若无10元货币,则5元货币-3;若无5元货币,返回false。
其余为true。

  1. /*
  2. *File:lemonadeChange.c:
  3. *Author:0HP
  4. *Date:20200202
  5. *Purpose:to solve the problem in Leetcode
  6. *https://leetcode.com/problems/lemonade-change/
  7. */
  8. /**
  9. *input a array, which is the money, only include 5,10,20
  10. *Output whether we can return the charge
  11. **/
  12. #include<stdio.h>
  13. #include<stdbool.h>
  14. bool lemonadeChange(int* bills, int billsSize){
  15. int five=0,ten=0;
  16. for(int i=0;i<billsSize;++i)
  17. {
  18. if(bills[i]==5)
  19. {++five;}
  20. else if(bills[i]==10)
  21. {
  22. --five;
  23. if(five<0){return false;}
  24. else{++ten;}
  25. }
  26. else
  27. {
  28. if(ten>0){--ten;--five;}
  29. else{five-=3;}
  30. if(ten<0||five<0){return false;}
  31. }
  32. }
  33. return true;
  34. }
  35. //test
  36. int main ()
  37. {
  38. int a[5]={5,5,10,10,20};
  39. printf("%d",lemonadeChange(a,5));
  40. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注