6-7

6 zigzag

1 sort by rows

class Solution {
    public String convert(String s, int numRows) {
        if (numRows == 1) return s;
        
        List rows = new ArrayList<>();
        for (int i=0; i < numRows; i++) rows.add(new StringBuilder());
        int curRows = 0;
        boolean goingDown = false;
        for (char c: s.toCharArray()) {
            rows.get(curRows).append(c);
            if (curRows == 0 || curRows == numRows - 1) goingDown = !goingDown;
            curRows += (goingDown) ? 1:-1;
        }
        
        StringBuilder rec = new StringBuilder();
        for (StringBuilder row : rows) 
            rec.append(row);
        return rec.toString();      
    }
}
  1. 反转
    注意上下逸出
class Solution {
    public int reverse(int x) {
        int rev = 0;
        while (x != 0) {
            int pop = x % 10;
            x /= 10;
            if (rev > Integer.MAX_VALUE/10 ) return 0;
            if (rev < Integer.MIN_VALUE/10 ) return 0;
            rev = rev * 10 + pop;
        }
        return rev;
    }
}

你可能感兴趣的:(6-7)