my java solution beating 99.4%


  • 0
    public class Solution {
        public String convert(String s, int numRows) {
            char[] start = s.toCharArray();
            char[] res = new char[start.length];
            int len = 0;
            if(numRows == 1)
                return s;
            for(int i = 0; i < numRows; i++){
                for(int j = i; j < start.length; j += 2 * (numRows - 1)){
                    if(i != 0 && i != numRows - 1 && (j + 2 * (numRows - 1) - 2 * i) < start.length){
                        res[len++] = start[j];
                        res[len++] = start[j + 2 * (numRows - 1) - 2 * i];
                    }
                    else
                        res[len++] = start[j];
                    
                }
            }
            String res2 = new String(res);
            return res2;
        }
    }
    

Log in to reply
 

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