[LeetCode]ZigZag Conversion

public class Solution {
    public String convert(String s, int numRows) {
        if (numRows == 1) {
            return s;
        }
        int flg = 0;
        boolean dir = true;
        List<StringBuffer> list = new ArrayList<StringBuffer>();
        for (int i = 0; i < numRows; i++) {
            list.add(new StringBuffer(""));
        }
        for (int i = 0; i < s.length(); i++) {
            list.get(flg).append(String.valueOf(s.charAt(i)));
            flg += dir ? 1 : -1;
            if (flg == numRows) {
                dir = !dir;
                flg -= 2;
            } else if (flg == -1) {
                dir = !dir;
                flg += 2;
            }
        }
        String result = "";
        for (StringBuffer str : list) {
            result = result + str;
        }
        return result;
    }
}

 

posted @ 2015-12-03 02:09  Weizheng_Love_Coding  阅读(120)  评论(0编辑  收藏  举报