【leetcode】柠檬水找零

 

bool lemonadeChange(int* bills, int billsSize){
    int hash[11] = {0},i;
    for (i=0; i<billsSize; i++)
    {
        if (bills[i] == 5) hash[5]++;
        else if (bills[i] == 10) 
        {
            hash[5]--;
            hash[10]++;
        }
        else
        {
            if (hash[10])
            {
                hash[5]--;
                hash[10]--;
            }
            else
                hash[5]-=3;
            
        }
        if (hash[5]<0 || hash[10]<0) return false;
    }
    return true;
}

 

posted @ 2020-09-23 10:06  温暖了寂寞  阅读(112)  评论(0编辑  收藏  举报