ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode第 279 场周赛

2022-02-06 12:34:13  阅读:112  来源: 互联网

标签:周赛 cnt return idx int f2 279 LeetCode size


A

class Solution {
public:
    vector<int> sortEvenOdd(vector<int>& nums) {
        vector<int> a, b, v;
        for (int i = 0; i < nums.size(); i ++ )
            if (i % 2 == 0) a.push_back(nums[i]);
            else b.push_back(nums[i]);
        sort(a.begin(), a.end());
        sort(b.begin(), b.end());
        reverse(b.begin(), b.end());
        for (int i = 0; i < min(a.size(), b.size()); i ++ )
             v.push_back(a[i]), v.push_back(b[i]);
        if (a.size() > b.size())
             v.push_back(a[a.size() - 1]);
        return v;
    }
};

B

class Solution {
public:
    int cnt[10];
    long long smallestNumber(long long num) {
        if (num >= 0) {
            while (num) {
                cnt[num % 10] ++;
                num /= 10;
            }
            long long res = 0;
            for (int i = 1; i <= 9; i ++ ) {
                if (cnt[i]) {
                    res = i;
                    cnt[i] --;
                    break;
                }
            }
            while (cnt[0] -- )
                res = res * 10;
            for (int i = 1; i <= 9; i ++ ) {
                while (cnt[i]) {
                    res = res * 10 + i;
                    cnt[i] --;
                }
            }
            return res;
        }
        else {
            num *= -1;
            long long res = 0;
            while (num) {
                cnt[num % 10] ++;
                num /= 10;
            }
            for (int i = 9; i >= 0; i -- ) {
                while (cnt[i]) {
                    res = res * 10 + i;
                    cnt[i] --;
                }
            }
            return res * -1;
        }
    }
};

C

class Bitset {
public:
    int len, cnt = 0, tot = 0;
    int a[100010] = {0};
    Bitset(int size) {
        len = size;
    }
    
    void fix(int idx) {
        if (a[idx] == 0 && tot % 2 == 0)    cnt ++, a[idx] = 1;
        if (a[idx] == 1 && tot % 2 == 1)    cnt ++, a[idx] = 0;
    }
    
    void unfix(int idx) {
        if (a[idx] == 1 && tot % 2 == 0)    cnt --, a[idx] = 0;
        if (a[idx] == 0 && tot % 2 == 1)    cnt --, a[idx] = 1;
    }
    
    void flip() {
        cnt = len - cnt;
        tot ++;
    }
    
    bool all() {
        if (cnt == len) return true;
        else    return false;
    }
    
    bool one() {
        if (cnt >= 1)   return true;
        else return false;
    }
    
    int count() {
        return cnt;
    }
    
    string toString() {
        string s;
        for (int i = 0; i < len; i ++ ) {
            if (tot & 1) {
                if (a[i])   s += "0";
                else s += "1";
            }
            else {
                if (a[i])   s += "1";
                else s += "0";
            }
        }
        return s;
    }
};

D
从两个方向DP,分别记作\(f1,f2\),那么答案就为\(min\{f1[i]+f2[i + 1]\}\)

class Solution {
public:
    char s[200010];
    int f1[200010], f2[200010];
    int minimumTime(string ss) {
        int n = ss.size();
        memset(f1, 0x3f, sizeof f1);
        memset(f2, 0x3f, sizeof f2);
        for (int i = 0; i < ss.size(); i ++ )   s[i + 1] = ss[i];
        f1[0] = f2[n + 1] = 0;
        for (int i = 1; i <= n; i ++ ) {
            f1[i] = f1[i - 1];
            if (s[i] == '1') {
                f1[i] = min(i, f1[i] + 2);
            }
        }
        for (int i = n; i >= 1; i -- ) {
            f2[i] = f2[i + 1];
            if (s[i] == '1') {
                f2[i] = min(n - i + 1, f2[i + 1] + 2);
            }
        }
        int res = 0x3f3f3f3f;
        for (int i = 0; i <= n; i ++ ) {
            res = min(res, f1[i] + f2[i + 1]);
        }
        return res;
    }
};

标签:周赛,cnt,return,idx,int,f2,279,LeetCode,size
来源: https://www.cnblogs.com/Angels-of-Death/p/15865429.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有