LeetCode每日一题:2月19日~2月23日

煎饼排序

每一次翻转将前n个数最大值放到序列最后,保证可以在2*n次数下翻转完成

class Solution {
public:
    vector<int> pancakeSort(vector<int>& a) {
        vector<int> res;
        for (int n = a.size(); n >= 2; n -- ) {
            int k = max_element(a.begin(), a.begin() + n) - a.begin();
            if (k == n - 1) continue;

            reverse(a.begin(), a.begin() + k + 1);
            reverse(a.begin(), a.begin() + n);
            res.push_back(k + 1);
            res.push_back(n);
        }
        return res;
    }
};

717. 1 比特与 2 比特字符

class Solution {
public:
    bool isOneBitCharacter(vector<int>& bits) {
        int idx = 0, n = bits.size();
        while (idx < n - 1) {
            idx += bits[idx] + 1;
        }
        return idx == n - 1;
    }
};

838. 推多米诺

用两个数组分别存每一个倒下的多米诺骨牌影响范围,比较该骨牌收到哪边的影响大,收到力大的反方向倒

class Solution {
public:
    string pushDominoes(string d) {
        string res;
        int n = d.size();
        vector<int> l(n, 100000), r(n, 100000);
        for (int i = 0; i < n; i ++ ) {
            if (d[i] != 'R') continue;
            int j = i + 1;
            r[i] = 1;
            while (j < n && d[j] == '.')
                r[j ++ ] = j - i + 1;
            i = j - 1;
        }

        for (int i = n - 1; i >= 0; i -- ) {
            if (d[i] != 'L') continue;
            int j = i - 1;
            l[i] = 1;
            while (j >= 0 && d[j] == '.')
                l[j -- ] = i - j + 1;
            i = j + 1;
        }
        
        for (int i = 0; i < n; i ++ ) {
            if (l[i] > r[i]) res.push_back('R');
            else if (l[i] < r[i]) res.push_back('L');
            else res.push_back('.');
        }
        return res;
    }
};

好子集的数目

还没学

仅仅翻转字母

双指针模板题

class Solution {
public:
    bool is_al(char c) {
        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
    }
    string reverseOnlyLetters(string s) {
        int n = s.size();
        string a = s;
        for (int i = 0, j = n - 1; i < j; i ++ ) {
            if (!is_al(s[i])) continue;
            while (j > i && !is_al(s[j])) j -- ;
            swap(s[i], s[j]);
            j -- ;
        }
        return s;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Shirandexiaowo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值