leetcode200题之扫尾部分(一)

1. 超级丑数

思路:与丑数Ⅱ想法一致。

让我们从数组中只包含一个丑数数字 1 开始,使用K个指针 pos[j] ,标记所指向丑数要乘以的因子。算法很简单:在dp[pos[j]]×primes[j] 选出最小的丑数。并将该丑数对应的因子指针往前走一步.

此题质因子为数组,丑数Ⅱ中为固定的3个数,可以直接3指针p1,p2,p5;

class Solution {
public:
    int nthSuperUglyNumber(int n, vector<int>& primes) {
        /*动态规划*/
        int k=primes.size();
        vector<int> dp(n);
        vector<int> pos(k,0);
        dp[0]=1;
        for(int i=1; i<n; i++){
            int min=INT_MAX;
            for(int j=0; j<k; j++){
                if(dp[pos[j]]*primes[j]<min){
                    min=dp[pos[j]]*primes[j];
                }
                dp[i]=min;                
            }
            for(int j=0; j<k; j++){
                if(dp[pos[j]]*primes[j]==min){
                    pos[j]++;
                }
            }
        }
        return dp[n-1];
    }
};

2. 最大数

class Solution {
public:
    string largestNumber(vector<int>& nums) {
        if(nums.empty())  return "";
        if(nums.size()==1)  return to_string(nums[0]);

        sort(nums.begin(), nums.end(),[](int& a, int &b){
            string s1=to_string(a)+to_string(b);
            string s2=to_string(b)+to_string(a);
            return s1 > s2;
        }
        );
        string res="";
        for(auto num: nums){
            res += to_string(num);
        }
        if(res[0]=='0')  return "0";
        return res;
    }
};

3. Fizz Buzz

  1.  把所有的映射关系放在散列表 fizzBuzzHash 中,这个散列表形如 { 3: 'Fizz', 5: 'Buzz' }。
  2. 遍历 1 ... N1...N。
  3. 对于每个数字,遍历 fizzBuzzHash 中的键,检查是否能被它整除。
  4. 如果这个数能被键整除,就把当前键映射的值加到到答案字符串后面去。对于散列表的每个键值对,都这样操作。
  5. 最后将答案字符串加入答案列表。
class Solution {
public:
    vector<string> fizzBuzz(int n) {
        vector<string> ans;

        //Hash map to store all fizzbuzz mappings.
        map<int, string> fizzBuzzDict = {
            {3, "Fizz"},
            {5, "Buzz"} 
        };
    
        for (int num = 1; num <= n; ++num)
        {
            string numAnsStr = "";

            for (auto key : fizzBuzzDict)
            {
                //If the num is divisible by key,
                //then add the corressponding string mapping to current numAnsStr
                if (num % key.first == 0)
                    numAnsStr += key.second;
            }
            //Not divisible by 3 or 5, add the number
            if (numAnsStr == "")
                numAnsStr += to_string(num);
            // Append the current answer str to the ans list
            ans.push_back(numAnsStr);
        }
        return ans;
    }
};

4. Nim游戏

1 true
2 true
3 true
4 false
5 true 不难想到,你先拿走一个,问题变为:对手先手拿4块石头,由子问题4知对手必败,你必胜。为true
6 true 同理 可以拿走两个 转化为子问题4的非
7 true 同理
8 false 不论拿走几个都将转变为5 6 7 的非
...

class Solution {
public:
    bool canWinNim(int n) {
        return (n%4)!=0;
    }
};

5. 3的幂

class Solution {
public:
    bool isPowerOfThree(int n) {
        /*常规思维
        if(n<=0)  return false;
        if(n==1)  return true;
        long long res=3;
        while(res<n){
            res *= 3;
        }
        return res==n;
        */
        //换底公式
        if(n<=0)  return false;
        double res=log10(n) / log10(3);
        return res-(int)res >0 ? false : true;
    }
};

6.自除数

class Solution {
public:
    vector<int> selfDividingNumbers(int left, int right) {
        vector<int> res;
        for(int i=left; i<=right; i++){
            if(check(i)){
                res.push_back(i);
            }
        }
        return res;
    }
    bool check(int n){
        int x=n;
        while(n){
            int t=n%10;
            if(t==0 || x%t!=0)  return false;
            n /=10;
        }
        return true;
    }
};

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值