Why I get runtime error for input "AB", 2


  • 1
    R

    It works locally.

    class Solution {
    public:
        string convert(string s, int n) {
            vector<string> result(n);
            int ind = 0;
            int step = 1;
            for(const char ch: s){
                result[ind] += ch;
                if(ind == n-1){
                    step = -1;
                }
                if(ind == 0){
                    step = 1;
                }
                ind += step;
            }
            return concat(result);
        }
    private:
        string concat(vector<string>& results){
            string sum="";
            for(const string& result: results){
                sum = sum+result;
            }
            return sum;
        }
    };
    

Log in to reply
 

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