【累计24天】LeetCode 的每日一题

LeetCode 的每日一题

1694. 重新格式化电话号码


1694. 重新格式化电话号码

class Solution {
public:
    string reformatNumber(string number) {
        string s;
        for (auto c : number) {
            if (c == ' ' || c == '-') continue;
            s += c;
        }
        int n = s.size();
        string ans;
        int pos = 0;
        while (n) {
            if (n > 4) {
                ans += s.substr(pos, 3) + '-';
                pos += 3;
                n -= 3;
            } else {
                if (n == 4) {
                    ans += s.substr(pos, 2) + '-' + s.substr(pos + 2, 2);
                } else {
                    ans += s.substr(pos, n);
                }
                break;
            }
        }
        return ans;
    }
};

1784. 检查二进制字符串字段

1784. 检查二进制字符串字段

class Solution {
public:
    bool checkOnesSegment(string s) {
        bool flag = false;
        for (int i = 0; i < s.size(); i++) {
            if (flag && s[i] == '1') return false;
            if (s[i] == '0') flag = true;
        }
        return true;
    }
};

921. 使括号有效的最少添加

921. 使括号有效的最少添加

class Solution {
public:
    stack<char> a;
    int minAddToMakeValid(string s) {
        a.push(s[0]);
        for (int i = 1; i < s.size(); i++) {
            if (s[i] == '(') a.push(s[i]);
            if (s[i] == ')') {
                if (a.empty() || a.top() != '(') a.push(s[i]);
                else a.pop();
            }
        }
        return a.size();
    }
};

811. 子域名访问计数

811. 子域名访问计数

class Solution {
public:
    unordered_map<string, int> map;
    vector<string> subdomainVisits(vector<string>& cpdomains) {
        for (auto s : cpdomains) {
            int space = s.find(' ');
            int count = stoi(s.substr(0, space));
            map[s.substr(space+1)] += count;
            for (int i = space + 1; i < s.size(); i++) {
                if (s[i] == '.') {
                    map[s.substr(i+1)] += count;
                }
            }
        }
        vector<string> ans;
        for (auto [domain, count] : map) {
            ans.push_back(to_string(count) + ' ' + domain);
        }
        return ans;
    }
};

927. 三等分

927. 三等分

class Solution {
public:
    vector<int> threeEqualParts(vector<int>& arr) {
        int sum = 0;
        for (auto x : arr) {
            if (x == 1) sum++;
        }
        if (sum % 3 != 0) return {-1, -1};
        if (sum == 0) return {0, 2};

        int p = sum / 3;
        int first = 0, second = 0, third = 0, cur = 0;
        for (int i = 0; i < arr.size(); i++) {
            if (arr[i] == 1) {
                cur++;
                if (cur == 1) first = i;
                if (cur == p + 1) second = i;
                if (cur == 2 * p + 1) third = i;
            }
        }
        int len = arr.size() - third;
        if (first + len <= second && second + len <= third) {
            for (int i = 0; i < len; i++) {
                if (arr[first+i] != arr[third+i] || arr[second+i] != arr[third+i]) {
                    return {-1, -1};
                }
            }
            return {first + len - 1, second + len};
        }
        return {-1, -1};
    }
};

1800. 最大升序子数组和

1800. 最大升序子数组和

class Solution {
public:
    int maxAscendingSum(vector<int>& nums) {
        int cur = 0, l = 0, ans = 0;
        while (l < nums.size()) {
            cur = nums[l++];
            while (l < nums.size() && nums[l] > nums[l-1]) {
                cur += nums[l++];
            }
            ans = max(ans, cur);
        }
        return ans;
    }
};

870优势洗牌

870优势洗牌

class Solution {
public:
    vector<int> advantageCount(vector<int>& nums1, vector<int>& nums2) {
        vector<int> ans(nums1.size(), 0);
        vector<int> ids(nums1.size(), 0);
        sort(nums1.begin(), nums1.end());
        sort(ids.begin(), ids.end(), [&](int i, int j) {return nums2[i] < nums2[j];});
        int l = 0, r = nums1.size() - 1;
        for (int x : nums1) {
            ans[x > nums2[ids[l]] ? ids[l++] : ids[r--]] = x;
        }
        return ans;
    }
};

856 括号分数

856 括号分数

class Solution {
public:
    int scoreOfParentheses(string s) {
        stack<int> sa;
        sa.push(0);
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == '(') sa.push(0);
            else {
                int t = sa.top();
                sa.pop();
                sa.top() += max(2 * t, 1);
            }
        }
        return sa.top();
    }
};

801. 使序列递增的最小交换次数

801. 使序列递增的最小交换次数

class Solution {
public:
    int minSwap(vector<int>& nums1, vector<int>& nums2) {
        int n = nums1.size();
        int f[n][2];
        memset(f, 0x3f, sizeof f);
        f[0][0] = 0, f[0][1] = 1;
        for (int i = 1; i < n; i++) {
            if (nums1[i] > nums1[i-1] && nums2[i] > nums2[i-1]) {
                f[i][0] = f[i-1][0];
                f[i][1] = f[i-1][1] + 1;
            }
            if (nums1[i] > nums2[i-1] && nums2[i] > nums1[i-1]) {
                f[i][0] = min(f[i][0], f[i-1][1]);
                f[i][1] = min(f[i][1], f[i-1][0] + 1);
            }
        }
        return min(f[n-1][0], f[n-1][1]);
    }
};

1790. 仅执行一次字符串交换能否使两个字符串相等

1790. 仅执行一次字符串交换能否使两个字符串相等

class Solution {
public:
    bool areAlmostEqual(string s1, string s2) {
        if (s1 == s2) return true;
        int cnt = 0, n = s1.size(), idx[4];
        for (int i = 0; i < n; i++) {
            if (s1[i] != s2[i]) {
                idx[cnt++] = i;
            }
            if (cnt > 2) return false;
        }
        if (cnt == 1) return false;
        return s1[idx[0]] == s2[idx[1]] && s1[idx[1]] == s2[idx[0]];
    }
};

817. 链表组件

817. 链表组件

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    int numComponents(ListNode* head, vector<int>& nums) {
        unordered_map<int, bool> m;
        int ans = 0;
        for (auto x : nums) m[x] = true;
        ListNode *cur = head;
        while (cur != nullptr) {
            if (!m[cur->val]) {
                cur = cur->next;
                continue;
            }
            int t = cur->val;
            ans++;
            for (int i = t; m[i] && cur != nullptr; i = cur->val) {
                m[t] = false;
                if (cur->next == nullptr) break;
                cur = cur->next;
            }
            cur = cur->next;
        }
        return ans;
    }
};

769. 最多能完成排序的块

769. 最多能完成排序的块

class Solution {
public:
    int maxChunksToSorted(vector<int>& arr) {
        int ans = 0;
        int n = arr.size();
        int m = 0;
        for (int i = 0; i < n; i++) {
            m = max(m, arr[i]);
            if (m == i) ans++;
        }
        return ans;
    }
};

940. 不同的子序列 II

940. 不同的子序列 II

class Solution {
public:
    int distinctSubseqII(string s) {
        int n = s.size();
        vector<int> last(26, -1);
        vector<int> f(n, 1);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 26; j++) {
                if (last[j] != -1) {
                    f[i] = (f[i] + f[last[j]]) % 1000000007;
                }
            }
            last[s[i] - 'a'] = i;
        }

        int ans = 0;
        for (int i = 0; i < 26; i++) {
            if (last[i] != -1) {
                ans = (ans + f[last[i]]) % 1000000007;
            } 
        }
        return ans;
    }
};

1441. 用栈操作构建数组

1441. 用栈操作构建数组

class Solution {
public:
    vector<string> buildArray(vector<int>& target, int n) {
        string push = "Push", pop = "Pop";
        vector<string> operate;
        for (int i = 0, j = 1; i < target.size(); i++) {
            while (j != target[i]) {
                operate.push_back(push);
                operate.push_back(pop);
                j++;
            }
            operate.push_back(push);
            j++;
        }
        return operate;
    }
};

886. 可能的二分法

886. 可能的二分法

class Solution {
public:
    bool dfs(int curnode, int nowcolor, vector<int>& color, const vector<vector<int>>& g) {
        color[curnode] = nowcolor;
        for (auto& nextnode : g[curnode]) {
            if (color[nextnode] && color[nextnode] == color[curnode]) {
                return false;
            }
            if (!color[nextnode] && !dfs(nextnode, 3 ^ nowcolor, color, g)) {
                return false;
            }
        }
        return true;
    }

    bool possibleBipartition(int n, vector<vector<int>>& dislikes) {
        vector<int> color(n+1, 0);
        vector<vector<int>> g(n+1);
        for (auto &p : dislikes) {
            g[p[0]].push_back(p[1]);
            g[p[1]].push_back(p[0]);
        }
        for (int i = 1; i <= n; ++i) {
            if (color[i] == 0 && !dfs(i, 1, color, g)) {
                return false;
            }
        }
        return true;
    }
};

904. 水果成篮

904. 水果成篮

class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        int n = fruits.size();
        unordered_map<int, int> m;
        int l = 0, r = 0, res = 0;
        while (r < n) {
            m[fruits[r]]++;
            while (m.size() > 2) {
                auto it = m.find(fruits[l]);
                it->second--;
                if (it->second == 0) {
                    m.erase(it);
                }
                l++;
            }
            res = max(res, r - l + 1);
            r++;
        }
        return res;
    }
};

1700. 无法吃午餐的学生数量

1700. 无法吃午餐的学生数量

class Solution {
public:
    int countStudents(vector<int>& students, vector<int>& sandwiches) {
        int s0 = 0, s1 = 0;
        for (int i = 0; i < students.size(); i++) {
            if (students[i] == 0) s0++;
            else s1++;
        }
        for (int i = 0; i < sandwiches.size(); i++) {
            if (sandwiches[i] == 0 && s0 > 0) s0--;
            else if (sandwiches[i] == 1 && s1 > 0) s1--;
            else break;
        }
        return s0 + s1;
    }
};

779. 第K个语法符号

779. 第K个语法符号

class Solution {
public:
    int kthGrammar(int n, int k) {
        // 1 0
        // 2 01
        // 4 01 10
        // 8 0110 1001
        // 16 0110 1001 1001 0110
        // ...
        //
        if (n == 1) return 0;
        int t = (1 << (n-2));
        if (k <= t) return kthGrammar(n-1, k);
        return 1 ^ kthGrammar(n-1, k - t);
    }
};

901. 股票价格跨度

901. 股票价格跨度

class StockSpanner {
    stack<pair<int, int>> s;
    int idx;
public:
    StockSpanner() {
        this->idx = -1;
        this->s.emplace(-1, INT_MAX);
    }
    
    int next(int price) {
        idx++;
        while (!s.empty() && s.top().second <= price) s.pop();
        int ret = idx - s.top().first;
        s.emplace(idx, price);
        return ret;
    }
};

/**
 * Your StockSpanner object will be instantiated and called as such:
 * StockSpanner* obj = new StockSpanner();
 * int param_1 = obj->next(price);
 */

1768. 交替合并字符串

1768. 交替合并字符串

class Solution {
public:
    string mergeAlternately(string word1, string word2) {
        string res = "";
        int i = 0, j = 0, flag = 1;
        while (i < word1.size() || j < word2.size()) {
            if (j >= word2.size() || flag && i < word1.size()) {
                res += word1[i++];
                flag = 0;
            } else {
                res += word2[j++];
                flag = 1;
            }
        }
        return res;
    }
};

915. 分割数组

915. 分割数组

class Solution {
public:
    int partitionDisjoint(vector<int>& nums) {
        int n = nums.size();
        vector<int> minR(n);
        minR[n-1] = nums[n-1];
        for (int i = n - 2; i >= 0; i--) {
            minR[i] = min(nums[i], minR[i+1]);
        }
        int maxl = 0, lpos = 0;
        for (int i = 0; i < n - 1; i++) {
            maxl = max(maxl, nums[i]);
            if (maxl <= minR[i+1]) {
                lpos = i;
                break;
            }
        }
        return lpos + 1;
    }
};

934. 最短的桥

934. 最短的桥

class Solution {
public:
    int shortestBridge(vector<vector<int>>& grid) {
        int n = grid.size();
        int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
        vector<pair<int, int>> island;
        queue<pair<int, int>> que;

        for (int i = 0; i< n; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1) que.emplace(i, j);
                grid[i][j] = -1;
                while (!que.empty()) {
                    auto [x, y] = que.front();
                    que.pop();
                    island.emplace_back(x, y);
                    for (int k = 0; k < 4; k++) {
                        int xx = x + dir[k][0], yy = y + dir[k][1];
                        if (xx >= 0 && yy >= 0 && xx < n && yy < n && grid[xx][yy] == 1) {
                            que.emplace(xx, yy);
                            grid[xx][yy] = -1;
                        }
                    }
                }

                for (auto &&[x, y] : island) {
                    que.emplace(x, y);
                }
                int step = 0;
                while (!que.empty()) {
                    int sz = que.size();
                    for (int i = 0; i < sz; i++) {
                        auto [x, y] = que.front();
                        que.pop();
                        for (int k = 0; k < 4; k++) {
                            int xx = x + dir[k][0], yy = y + dir[k][1];
                            if (xx >= 0 && yy >= 0 && xx < n && yy < n) {
                                if (grid[xx][yy] == 0) {
                                    que.emplace(xx, yy);
                                    grid[xx][yy] = -1;
                                } else if (grid[xx][yy] == 1) {
                                    return step;
                                }
                            }
                        }
                    }
                    step++;
                }
            }
        }
        return 0;
    }
};

1822. 数组元素积的符号

1822. 数组元素积的符号

class Solution {
public:
    int arraySign(vector<int>& nums) {
        int res = 1;
        for (int x : nums) {
            if (x < 0) res *= -1;
            else if (x == 0) return 0;
        }
        return res;
    }
};

481. 神奇字符串

481. 神奇字符串

class Solution {
public:
    int magicalString(int n) {
        if (n <= 3) return 1;
        string s = "122";
        int res = 1, i = 2;
        char k = '1';
        while (s.size() < n) {
            int cnt = s[i] - '0';
            int cnt2 = cnt;
            while (s.size() < n && cnt) {
                cnt--;
                s += k;
            }
            if (k == '1') {
                res += cnt2 - cnt;
                k = '2';
            } 
            else k = '1';
            i++;
        }
        return res;
    }
};

1662. 检查两个字符串数组是否相等

1662. 检查两个字符串数组是否相等

class Solution {
public:
    bool arrayStringsAreEqual(vector<string>& word1, vector<string>& word2) {
        int p1 = 0, p2 = 0, i = 0, j = 0;
        while (p1 < word1.size() && p2 < word2.size()) {
            if (word1[p1][i] != word2[p2][j]) return false;
            i++, j++;
            if (i == word1[p1].size()) {
                p1++;
                i = 0;
            }
            if (j == word2[p2].size()) {
                p2++;
                j = 0;
            }
        }
        return p1 == word1.size() && p2 == word2.size();
    }
};

1620. 网络信号最好的坐标

1620. 网络信号最好的坐标

class Solution {
public:
    vector<int> bestCoordinate(vector<vector<int>>& towers, int radius) {
        vector<int> ans = {-1, -1};
        int max = -1;
        for (int i = 0; i <= 50; i++) {
            for (int j = 0; j <= 50; j++) {
                int cur = 0;
                for (auto x : towers) {
                    int d2 = pow(i - x[0], 2) + pow(j - x[1], 2);
                    if (d2 <= radius * radius) cur += x[2] / (1 + sqrt(d2));
                }
                if (cur > max) {
                    max = cur;
                    ans = {i, j};
                }
            }
        }
        return ans;
    }
};

1753. 移除石子的最大得分

1753. 移除石子的最大得分

class Solution {
public:
    int maximumScore(int a, int b, int c) {
        int sum = a + b + c;
        int m = max({a, b, c});
        if (sum - m <= m) {
            return sum - m;
        } else {
            return sum / 2;
        }
    }
};

持续更新

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值