一道简单的贪心算法题,没啥可说的,跟常识一样,找零钱的时候,先从大的面额找,然后才用小面额的
记录下第一次用四种语言AC

python

class Solution:                                          
    def lemonadeChange(self, bills):                     
        """                                              
        :type bills: List[int]                           
        :rtype: bool                                     
        """                                              
        money_5  =0                                      
        money_10 =0                                      
        money_20 =20                                     
        for i in range(len(bills)):                      
            get_in =bills[i]                             
            if  get_in ==5:                              
                money_5+=1                               
            elif get_in==10:                             
                if money_5>0:                            
                    money_5-=1                           
                    money_10+=1                          
                else:                                    
                    return  False                        
            else:                                        
                if money_10>0 and money_5>0:             
                    money_10-=1                          
                    money_5-=1                           
                elif money_10==0 and money_5>=3:         
                    money_5-=3                           
                else:                                    
                    return False                         
        return True                                      
                                                         
                                                                                                     

Java

public class Solution {
    public boolean lemonadeChange(int[] bills) {
          int money_5  = 0;
          int money_10 =0;
          int money_20 =0;
          int get_in;
          for(int i=0;i<bills.length;i++){
              get_in =bills[i];
              if(get_in==5){
                  money_5+=1;
              }
              else if(get_in==10){
                  if(money_5>=1) {
                      money_5 -= 1;
                      money_10 += 1;
                  }
                  else
                    return false;
              }
              else {
                  if (money_10>=1 && money_5 >=1){
                      money_5-=1;
                      money_10-=1;
                  }
                  else if (money_10==0 && money_5>=3)
                      money_5 -=3;
                  else return false;
              }
          }
          return  true;
    }
}

scala

object Solution {
  def lemonadeChange(bills: Array[Int]): Boolean = {
        var money_5:Int =0
        var money_10:Int =0
        var money_20:Int =0
        for(x:Int <- bills){
          var get_in:Int =x
          if (get_in==5)  money_5+=1
          else if (get_in==10){
              if(money_5>=1){
                money_5-=1
                money_10+=1
              }
              else return false
          }
          else{
            if (money_10>=1 && money_5>=1 ){
                  money_10-=1
                  money_5-=1
            }
            else if (money_10==0 && money_5>=3){
                  money_5-=3
            }
            else return false
          }

        }
     true;
  }

}

c

bool lemonadeChange(int* bills, int billsSize) {
     int i =0;
    int money_5 =0;
    int money_10=0;
    int money_15=0;
    int get_in;
    for (i;i<billsSize;i++){
          get_in = bills[i];
          if(get_in==5){
              money_5+=1;
          }
          else if(get_in==10){
              if(money_5>=1){
                  money_5-=1;
                  money_10+=1;
              }
              else return false;     
          }
         else{
               if(money_10>=1 && money_5>=1){
                   money_10-=1;
                   money_5-=1;
               }
               else if (money_10==0 && money_5>=3){
                   money_5-=3;
               }
               else return false;
         }}
        return true; 
}
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐