Zheaoli / do-something-right

MIT License
37 stars 3 forks source link

2022-12-07 #444

Open Zheaoli opened 1 year ago

Zheaoli commented 1 year ago

2022-12-07

dreamhunter2333 commented 1 year ago
#include <iostream>
#include <set>
#include <deque>
using namespace std;
/*
 * @lc app=leetcode.cn id=1805 lang=cpp
 *
 * [1805] 字符串中不同整数的数目
 */

// @lc code=start
class Solution
{
public:
    int numDifferentIntegers(string word)
    {
        word += "a";
        set<string> res;
        deque<char> pre;
        for (auto &&lett : word)
        {
            if (isdigit(lett))
            {
                if (!pre.empty() && pre.front() == '0')
                    pre.pop_front();
                pre.push_back(lett);
            }
            else if (!pre.empty())
            {
                string pre_str(pre.begin(), pre.end());
                res.emplace(pre_str);
                pre.clear();
            }
            else
            {
                pre.clear();
            }
        }
        return res.size();
    }
};
// @lc code=end

微信id: 而我撑伞 来自 vscode 插件

dreamhunter2333 commented 1 year ago
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
/*
 * @lc app=leetcode.cn id=1775 lang=cpp
 *
 * [1775] 通过最少操作次数使数组的和相等
 */

// @lc code=start
class Solution
{
public:
    int minOperations(vector<int> &nums1, vector<int> &nums2)
    {
        int delta = accumulate(nums1.begin(), nums1.end(), 0) - accumulate(nums2.begin(), nums2.end(), 0);
        if (delta == 0)
            return 0;
        if (delta < 0)
        {
            swap(nums1, nums2);
            delta = -delta;
        }
        if (nums2.size() * 6 < nums1.size() * 1)
            return -1;
        vector<int> delta_nums;
        for (auto &&num : nums1)
            delta_nums.push_back(num - 1);
        for (auto &&num : nums2)
            delta_nums.push_back(6 - num);
        sort(delta_nums.begin(), delta_nums.end(), [](const int &a, const int &b)
             { return a > b; });

        int res = 0;
        while (delta > 0)
        {
            delta -= delta_nums[res];
            res++;
        }
        return res;
    }
};
// @lc code=end
int main(int argc, char const *argv[])
{
    Solution s;
    vector<int> nums1({1, 2, 3, 4, 5, 6});
    vector<int> nums2({1, 1, 2, 2, 2, 2});
    cout << s.minOperations(nums1, nums2) << endl;
    return 0;
}

微信id: 而我撑伞 来自 vscode 插件