代码随想录算法训练营第三十天-贪心算法-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;
}
相关推荐
阿洛学长几秒前
汉洛塔结构思维
算法
木子n17 分钟前
第2篇:坐标变换与数学基础:FOC算法的核心数学工具
算法·电机控制·foc
阿Y加油吧20 分钟前
两道经典 DP 题:零钱兑换 & 单词拆分(完全背包 + 字符串 DP)
算法
pearlthriving20 分钟前
c++当中的泛型思想以及c++11部分新特性
java·开发语言·c++
疯狂打码的少年28 分钟前
有序线性表删除一个元素:顺序存储 vs 单链表,平均要移动多少个元素?
数据结构·算法·链表
y = xⁿ42 分钟前
20天速通LeetCode day07:前缀和
数据结构·算法·leetcode
小雅痞1 小时前
[Java][Leetcode hard] 42. 接雨水
java·开发语言·leetcode
t***5441 小时前
Dev-C++中哪些选项可以设置
开发语言·c++
载数而行5201 小时前
算法集训1:模拟,枚举,错误分析,前缀和,差分
算法
hehelm1 小时前
vector模拟实现
前端·javascript·算法