1. 程式人生 > >*Leetcode 763. Partition Labels

*Leetcode 763. Partition Labels

const int SIZE = 256;
class Solution {
public:
    
    bool judge( int cnt[], int hash[] ) {
        // bool ret = true;
        for (int i = 0; i < SIZE; i++) {
            if (hash[i] == cnt[i] || !hash[i])continue;
            return false;
        }
        
        return true;
    }
    
    vector<int> partitionLabels(string s) {
        int mem[SIZE] = {0}, hash[SIZE] = {0};
        
        vector<int> ret;
        int cnt = 0;
        for (auto c : s) mem[c] ++;
        for (auto c : s) {
            cnt ++;
            hash[c] ++;
            if (judge(mem, hash)) {
                ret.push_back(cnt);
                cnt = 0;
            }  
        }
        
        return ret;
    }
};