码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]ZigZag Conversion

时间:2015-12-03 07:11:42      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

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;
    }
}

 

[LeetCode]ZigZag Conversion

标签:

原文地址:http://www.cnblogs.com/vision-love-programming/p/5014914.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!