Hi, this is Shunchi!

  • Home

  • Tags0

  • Archives267

  • Categories0

  • Curricula

  • DSA

  • LeetCode_Notes

  • Interviews

  • General

  • Resume

6. ZigZag Conversion

Posted on 2020-06-12 | Edited on 2021-01-22

LeetCode

Approach: Sort by Row, Time Complexity: O(n), where n == len(s)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public String convert(String s, int numRows) {
if(numRows==1) return s;
List<StringBuilder> rows=new ArrayList<>();
for(int i=0;i<Math.min(numRows,s.length());i++) rows.add(new StringBuilder());

int curRow=0;
boolean goingDown=false;
for(char ch:s.toCharArray()){
rows.get(curRow).append(ch);
if(curRow==0||curRow==numRows-1) goingDown=!goingDown;
curRow+=goingDown?1:-1;
}

StringBuilder sb=new StringBuilder();
for(StringBuilder row:rows) sb.append(row);
return sb.toString();
}
}

<1…166167168…267>
ShunchiZhou

ShunchiZhou

267 posts
RSS
GitHub E-Mail Gitbook Linkedin
© 2024 ShunchiZhou
Powered by Hexo v5.4.0
|
0%