diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 74ea762..dc1b861 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,10 +1,9 @@ - - - - + + + + @@ -73,13 +73,13 @@ - - - @@ -234,72 +241,73 @@ - - + - - + + - - - + + + - - - + + + - - - + + + - - - + + + - + - + - + - + - + - + - + - + - + - + - + \ No newline at end of file diff --git a/src/q6/Solution.java b/src/q6/Solution.java new file mode 100644 index 0000000..d65b503 --- /dev/null +++ b/src/q6/Solution.java @@ -0,0 +1,45 @@ +package q6; + +import java.util.ArrayList; +import java.util.List; + +/** + * o(n) 可用一boolean变量代替求余操作 + */ +public class Solution { + public String convert(String s, int numRows) { + if (numRows == 1) { + return s; + } + int len = s.length(); + int col = 0; + int n = 0; + List list = new ArrayList<>(); + for (int i = 0; i < numRows; i++) { + StringBuffer temp = new StringBuffer(); + list.add(temp); + } + while (n < len) { + int y = col % (numRows - 1); + if (y == 0) { + for (int i = 0; i < numRows && n < len; i++) { + list.get(i).append(s.charAt(n)); + n++; + } + } else { + list.get(numRows - 1 - y).append(s.charAt(n)); + n++; + } + col++; + } + String rs = ""; + for (int i = 0; i < list.size(); i++) { + rs += list.get(i).toString(); + } + return rs; + } + + public static void main(String[] args) { + System.out.println(new Solution().convert("LEETCODEISHIRING", 4)); + } +}