zigzag conversion

public class Solution {
    public String convert(String s, int numRows) {
        String res = "";
        if (s.length() < numRows || numRows == 1) {
            return s;
        }

        for (int i = 0; i < numRows; i++) {
            for (int j = i; j < s.length(); j += (2*numRows - 2)) {
                res += s.charAt(j);
                if (i != 0 && i != numRows - 1 && (j + 2*numRows - 2 - 2*i) < s.length()) {
                    res += s.charAt(j + 2*numRows - 2 - 2*i);
                }
            }
            
        }
        return res;
    }
}

 

posted on 2015-05-13 05:31  kikiUr  阅读(74)  评论(0编辑  收藏  举报