32.哀家要长脑子了!

1.299. 猜数字游戏 - 力扣(LeetCode)

公牛还是挺好数的,奶牛。。。妈呀,一朝打回解放前

抓本质抓本质,有多少位非公牛数可以通过重新排列转换公牛数字,意思就是,当这个数不是公牛数字时,我们就分别统计它在secret和guess中的数量,然后取数量较少的作为奶牛数就好,因为它是可以调整位置的使它变成公牛数,多余的无法匹配再怎么调整位置也没办法把它变成公牛数。

class Solution {
public:
    vector<string> fizzBuzz(int n) {
        n += 1;
        vector<string> answer(n);
        for(int i = 1 ; i < n; i++){
            if(i % 3 == 0 && i % 5 == 0){
                answer[i] = "FizzBuzz";
            }
            else if(i % 3 == 0){
                answer[i] = "Fizz";
            }
            else if(i % 5 == 0){
                answer[i] = "Buzz";
            }
            else{
                answer[i] = to_string(i);
            }
        }
        return answer;
    }
};
2.412. Fizz Buzz - 力扣(LeetCode)

 匹配就好

class Solution {
public:
    vector<string> fizzBuzz(int n) {
        n += 1;
        vector<string> answer;
        for(int i = 1 ; i < n; i++){
            if(i % 3 == 0 && i % 5 == 0){
                answer.push_back("FizzBuzz");
            }
            else if(i % 3 == 0){
                answer.push_back("Fizz");
            }
            else if(i % 5 == 0){
                answer.push_back("Buzz");
            }
            else{
                answer.push_back(to_string(i));
            }
        }
        return answer;
    }
};
3.506. 相对名次 - 力扣(LeetCode)

 首先自己瞎掰扯了半天 感觉有点冗余 三元运算符还是见样学样的哈哈。。

class Solution {
public:
    vector<string> findRelativeRanks(vector<int>& score) {
        int n = score.size();
        string medal[3] = {"Gold Medal", "Silver Medal", "Bronze Medal"};
        vector<int> map(1000010);
        vector<string> answer(n);
        for(int i  = 0; i < n; i++){
            map[score[i]] = i;            
        }
        sort(score.begin(), score.end());
        reverse(score.begin(), score.end());
        for(int i = 0; i < n; i++){
            int site = map[score[i]];
            answer[site] = i<3 ? medal[i] :  to_string(i+1);
            }
        return answer;
    }
};

后面看了别人的题解(下面这个

class Solution {
public:
    vector<string> findRelativeRanks(vector<int>& score) {
        int n = score.size();
        string medal[3] = {"Gold Medal", "Silver Medal", "Bronze Medal"};
        map<int,int> map;
        vector<string> answer;
        
        vector<int> a(score.begin(), score.end());
        sort(a.begin(), a.end());
        reverse(a.begin(), a.end());
        for(int i  = 0; i < n; i++){
            map[a[i]] = i;            
        }
        
        for(int i = 0; i < n; i++){
            int rank = map[score[i]];
            string res = rank<3 ? medal[rank] : to_string(rank+1);
            answer.push_back(res);
            }
        return answer;
    }
};

发现我跟人家想法不一样的是:我没有复制数组,先记录排序前原来的位置,然后在制造答案数组的时候遍历排序后的数组,根据记录的原来的位置放答案

人家是用一个新数组记录排序之后的数组 然后再遍历原数组的元素 根据排序之后的名次决定这个位置的答案数组中是什么结果

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值