代码随想录算法训练营第三十天-贪心算法-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;
}
相关推荐
ShineWinsu32 分钟前
对于C++:IO流状态的详细解析
c++·面试·io·cin·io流状态·goodbit·failbit
数模竞赛Paid answer1 小时前
2026年中青杯数学建模A题数学建模论文智能评估系统与多智能体优化方法求解全过程论文及程序
算法·数学建模·中青杯
银-豆豆2 小时前
数据结构与算法-动态规划、回溯与贪心
算法·动态规划
Lzg_na2 小时前
constexpr的优势
c++
想吃火锅10053 小时前
【leetcode】200. 岛屿数量
算法·leetcode·职场和发展
王维同学3 小时前
进程模块枚举、映像身份与线程启动地址关联
c++·windows·安全
hold?fish:palm3 小时前
24 回文链表
数据结构·c++·链表
举手4 小时前
Dispatcher模块剖析
linux·c++
Nil2084 小时前
leetcode 54螺旋矩阵
算法·leetcode·矩阵
依然鸣5 小时前
PTA团体程序设计天梯赛L1真题讲解L1-077-080
开发语言·c++·算法·深度优先·pat考试·图论