Spend 12ms cpp I guess the time complexity is O(n)


  • 0
    F
    class Solution {
    public:
        string convert(string s, int numRows) {
            int n = 2*numRows-2;
            int l = s.length();
            string temps = "";
            if(numRows==1) return s;
            for(int i=0;i<numRows&&i<l;i++)
            {
                temps+=s[i];
                for(int j=i;j<l;)
                {
                    if(i!=numRows-1)
                    {
                        j+=(n-i*2);
                       if(j<l) temps+=s[j];
                    }
                    if(i!=0)
                    {
                        j+=(i*2);
                        if(j<l) temps+=s[j];
                    }
                }
            }
            return temps;
        }
    };
    

    I guess the time complexity is O(n)


Log in to reply
 

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