Accepted C# soluton for ZigZag conversion


  • 0
    R

    My C# solution for ZigZag conversion

    public class Solution {
        public string Convert(string s, int numRows) {
            if (numRows == 1) return s;
    
            var result = "";
    
            for (var currentLine = 0; currentLine < numRows; currentLine++)
            {
                var isFirstOrLastLine = currentLine == 0 || currentLine == numRows - 1 ? true : false;
                var index = currentLine;
    
                while (index < s.Length)
                {
                    if (isFirstOrLastLine)
                    {
                        result += s[index];
                        index += 2 * (numRows - 1);
                    }
                    else
                    {
                        result += s[index];
                        index += 2*(numRows - currentLine - 1);
                        if (index >= s.Length) continue;
                        result += s[index];
                        index += 2 * currentLine;
                    }
                }
            }
            return result;
        }
    }

Log in to reply
 

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