C# O(n) solution


  • 1
    J
    public string Convert(string s, int numRows) {
        if(s==null||s.Length==0||numRows==0) { return ""; }
        if(numRows==1) { return s; }
        string[] ss=new string[numRows];
        for(int i=0;i<=numRows-1;i++) { ss[i]=""; }
        int row=0; bool down=true;
        for(int i=0;i<=s.Length-1;i++)
        {
            ss[row]+=s[i].ToString();
            if(row==numRows-1) { row--; down=false; }
            else if(row==0) { row++; down=true; }
            else if(down) { row++; }
            else if(!down) { row--; }
        }
        string r="";
        foreach(string str in ss)
        { r+=str; }
        return r;
    }

Log in to reply
 

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