Solution in C#


  • 0
    P
    static public string Convert(string s, int numRows)
            {
                if (numRows == 0)
                    return string.Empty;
    
                string[] str = new string[numRows];
                int strNext = 0;
                bool increasing = true;
                for (int i = 0; i < s.Length; i++)
                {
                    str[strNext] = str[strNext] + s[i].ToString();
    
                    if (increasing && strNext == str.Count() - 1)
                    {
                        strNext--;
                        increasing = false;
                    }
                    else if (increasing && strNext < str.Count() - 1)
                    {
                        strNext++;
                    }
                    else if (!increasing && strNext > 0)
                    {
                        strNext--;
                    }
                    else if (!increasing && strNext == 0)
                    {
                        strNext++;
                        increasing = true;
                    }
    
                    // extreme case, when the rows count is 1
                    if (numRows == 1)
                    {
                        strNext = 0;
                    }
                }
    
                StringBuilder output = new StringBuilder();
                for (int i = 0; i < numRows; i++)
                {
                    output.Append(str[i]);
                }
                return output.ToString();
            }
        }
    

Log in to reply
 

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