C++ bits operation solution

  • 0

    I'm sharing my C++ bits operation solution. It is not very fast, only 148ms, anybody know why? Probably I use the insert() function of string ?

        vector<string> generateAbbreviations(string word) {
            vector<string> result;
            if(word.size()==0) return vector<string>(1, string());
            int size=word.size();
            for(int i=0; i<(1<<size); ++i){
                string tmp;
                for(int j=0; j<size; ){
                    if(i>>j & 1){ // check every bits, if it is 1, find consecutive 1 s, then replace with number
                        int k=j;
                        while(i>>k & 1) ++k;
                        int consecutive = k-j;
                        string num = to_string(consecutive);
                        tmp.insert(tmp.end(), num.begin(), num.end());
                    }else{ // not bit 1, copy to tmp string
            return result;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.