Sunday, November 29, 2015

[leetcode]Zigzag conversion

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Solution {
    public String convert(String s, int numRows) {
        if (numRows == 1) return s;
        String[] temp = new String[numRows];
        Arrays.fill(temp, "");
        int updown = 1;
        int current = 0;
        int curRow = 0;
        while (current < s.length()){
            temp[curRow] += s.charAt(current++);
            if (curRow == numRows-1){
                updown = -1;
            }else if (curRow == 0){
                updown = 1;
            }
            curRow += updown;
        }
        String result = "";
        for (int i = 0; i < temp.length; i++) result+=temp[i];
        return result;
    }
}

No comments:

Post a Comment