My c++ solution using vector, anyway to improve?


  • 0
    T
    class Solution {
    public:
        string convert(string s, int nRows) {
            if (nRows==1) return s;
            vector<string> result(nRows);
            string ret;
            for (int i=0;i<s.length();i++) {
                int offset=i%(nRows-1);
                if (offset==0) {
                    for (int j=0;j<nRows;j++) {
                        if (j!=0) i++;
                        result[j]+=s[i];
                        if (i==(s.length()-1)) break;
                    }
                }else {
                    result[nRows-offset-1]+=s[i];
                }
            }
            for (int j=0;j<nRows;j++) {
                ret+=result[j];
            }
            return ret;
        }
    };

Log in to reply
 

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