力扣763. 划分字母区间(双指针,哈希map)
末端标识位end记录当前段的任意字母的最后出现的位置
当右指针大于末端标识位end,更新末端标识位end当左指针等于末端标识位end,即分块完成 // // main.cpp // 763partitionLabels // // Created by MXQ on 2020/10/22. // #include <iostream> #include "string" #include "vector" using namespace std; class Solution { public: vector<int> partitionLabels(string S) { int n=S.size(); vector<int>result; if (n==0) { return result; } int start=0; int end=0; //双指针 for (int i=0; i<n; i++) { for (int j=i; j<n; j++) { if (S[i]==S[j] && j>end) { end=j; } } if (i==end) { result.push_back(end-start+1); start=end+1; } } return result; } }; int main(int argc, const char * argv[]) { // insert code here... Solution s; string str="ababcbacadefegdehijhklij"; auto result=s.partitionLabels(str); for (int i=0; i<result.size(); i++) { cout<<result[i]<<" "; } std::cout << "Hello, World!\n"; std::cout << "Hello, World!\n"; return 0; }
