从零开始的刷LeetCode生活 第34期 341-350

在这里插入图片描述

/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * class NestedInteger {
 *   public:
 *     // Return true if this NestedInteger holds a single integer, rather than a nested list.
 *     bool isInteger() const;
 *
 *     // Return the single integer that this NestedInteger holds, if it holds a single integer
 *     // The result is undefined if this NestedInteger holds a nested list
 *     int getInteger() const;
 *
 *     // Return the nested list that this NestedInteger holds, if it holds a nested list
 *     // The result is undefined if this NestedInteger holds a single integer
 *     const vector<NestedInteger> &getList() const;
 * };
 */
class NestedIterator {
public:
    vector<int>seq;
    int cnt = 0;
    NestedIterator(vector<NestedInteger> &nestedList) {
        dfs(nestedList);
    }

    void dfs(vector<NestedInteger>&nestedList)
    {
        for(auto &l : nestedList)
        {
            if(l.isInteger()) seq.push_back(l.getInteger());
            else dfs(l.getList());
        }
    }

    int next() {
        return seq[cnt ++];
    }

    bool hasNext() {
        return cnt < seq.size();
    }
};

/**
 * Your NestedIterator object will be instantiated and called as such:
 * NestedIterator i(nestedList);
 * while (i.hasNext()) cout << i.next();
 */

在这里插入图片描述

class Solution {
public:
   bool isPowerOfFour(int num) {
       if(num <= 0) return false;
       int t = sqrt(num);
       return t * t == num && ((1 << 30) % num) == 0;
   }
};

在这里插入图片描述

class Solution {
public:
    int integerBreak(int n) {
        if(n <= 3) return n - 1;
        int res = 1;
        if(n % 3 == 1) res = 4, n -= 4;
        if(n % 3 == 2) res = 2, n -= 2;
        while(n) res *= 3, n-= 3;
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    void reverseString(vector<char>& s) {
        for(int i = 0, j = s.size() - 1; i < j; i ++, j --)
            swap(s[i], s[j]);
    }
};

在这里插入图片描述

class Solution {
public:
    string reverseVowels(string s) {
        char vowels[] = {'a', 'e', 'i', 'o', 'u'};
        unordered_set<char> S;
        for(auto c : vowels)
        {
            S.insert(c);
            S.insert(c -32); // a 97 A 65
        }   
        for(int i = 0, j = s.size() - 1; i < j; )
        {
            while(i < j && !S.count(s[i])) i ++;
            while(i < j && !S.count(s[j])) j --;
            if(i < j) swap(s[i], s[j]);
            i ++, j --;
        }
        return s;
    }
};

在这里插入图片描述

class Solution {
public:
    vector<int> topKFrequent(vector<int>& nums, int k) {
        unordered_map<int, int>hash;
        for(auto x : nums) hash[x] ++;
        //for(auto p : hash) cout << p.first << '=' << p.second << endl;
        int n = nums.size();
        vector<int> s(n + 1, 0); //s[0, 1, 1, 1]
        for(auto p : hash) s[p.second] ++;
        int i = n, t = 0;
        while(t < k) t += s[i--];
        vector<int> res;
        for(auto p : hash)
            if(p.second > i)
                res.push_back(p.first);
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        unordered_set<int>set(nums1.begin(), nums1.end());
        vector<int> res;
        for(int i = 0; i < nums2.size(); i ++)
            if(set.find(nums2[i]) != set.end())
            {
                res.push_back(nums2[i]);
                set.erase(nums2[i]);
            }
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        if(nums1.size() > nums2.size()) return intersect(nums2, nums1);
        unordered_multiset<int> hash; //有重复元素
        for(auto x : nums1) hash.insert(x);
        vector<int> res;
        for(auto x : nums2)
        {
            if(hash.count(x))
            {
                res.push_back(x);
                hash.erase(hash.find(x));
            }
        }
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值