Java Solution with time of 384ms


  • 0
    A
    public String convert(String s, int numRows) {
        StringBuilder sb = new StringBuilder();
        int delta = 2 * (numRows - 1);
        if(delta == 0)
            delta = 1;
        for(int i = numRows - 1, counter = 0; i >= 0; i--, counter++)
        {
            int delta1 = 2 * i;
            int index = counter;
            while (index < s.length())
            {
                sb.append(s.charAt(index));
                if(i != numRows-1 && i != 0 && (index + delta1) < s.length())
                {
                    sb.append(s.charAt(index + delta1));
                }
                index = index + delta;
            }
        }
        return sb.toString();
    }

Log in to reply
 

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