代码随想录算法训练营第三十天-贪心算法-763. 划分字母区间

  • 标记字符最远位置,这是人能想到的?
  • 定义一个26个字母的数组,下标表示字母的位置,数组值表示当前字母在字符串中遍历过程中所处的位置
  • 算法题目无厘头太多,但解法也是太精彩,可是根本记不住,要每日刷,每日精进
cpp 复制代码
#include <iostream>
#include <vector>

class Solution {
public:
    std::vector<int> partitionLabels(std::string s) {
        int hash[26] {0};
        for (int i = 0; i < s.size(); ++i)
            hash[s[i] - 'a'] = i;
        std::vector<int> result;
        int left = 0, right = 0;
        for (int i = 0; i < s.size(); ++i) {
            right = std::max(right, hash[s[i] - 'a']);
            if (i == right) {
                result.push_back(right - left + 1);
                left = i + 1;
            }
        }
        return result;
    }
};

int main()
{
    Solution s;
    return 0;
}
相关推荐
伊玛目的门徒2 小时前
试用leetcode之典中典 二数之和问题
java·算法·leetcode
Jerry2 小时前
LeetCode 226. 翻转二叉树
算法
想做小南娘,发现自己是女生喵4 小时前
【无标题】
数据结构·算法
Kx_Triumphs5 小时前
HDU4348 To the moon(主席树区间修改模板)
算法·题解
旖-旎6 小时前
《LeetCode647 回文子串 || LeetCode 5 最长回文子串》
c++·算法·leetcode·动态规划·哈希算法
Darkwanderor6 小时前
对Linux的进程控制的研究
linux·运维·c++
云泽8087 小时前
从零吃透 C++ 异常:抛出捕获、栈展开、异常重抛与编码规范详解
开发语言·c++·代码规范
轻颂呀7 小时前
约瑟夫环问题
算法
REDcker7 小时前
libdatachannel 快速入门
c++·webrtc·datachannel