Modern Java Style


  • 0
    D
    public class Solution {
        public String convert(String s, int numRows) {
            ArrayList<String> ret = new ArrayList<>(Collections.nCopies(numRows, ""));
            int idx = 0;
            int step = 1;
            for (int i = 0; i < s.length(); ++i) {
                ret.set(idx, ret.get(idx) + s.charAt(i));
                if (numRows == 1) {
                    step = 0;
                } else if (idx == numRows - 1) {
                    step = -1;
                } else if (idx == 0) {
                    step = 1;
                }
                idx += step;
            }
            String res = new String("");
            for (String str : ret) {
                res += str;
            }
            return res;
        }
    }
    

Log in to reply
 

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