lc6 ZigZag Conversion
作者:互联网
lc6 ZigZag Conversion
分成两步,
第一步垂直向下,
1
1
1
1
第二步倾斜向上
1 1
1 1
1 1
1
用nRows个StringBuilder 然后将他们合并即可
1 class Solution { 2 public String convert(String s, int numRows) { 3 if(s.length() < numRows) 4 return s; 5 if(numRows < 2) 6 return s; 7 String[] res = new String[numRows]; 8 for(int i=0; i<numRows; i++){ 9 res[i] = Character.toString(s.charAt(i)); 10 } 11 12 for(int i=numRows; i<s.length(); i++){ 13 int tmp = i % (2*numRows - 2); 14 if(tmp >= 0 && tmp < numRows){ 15 res[tmp] += (s.charAt(i)); 16 }else{ 17 res[numRows-tmp%(numRows-1)-1] += (s.charAt(i)); 18 } 19 } 20 StringBuilder sb = new StringBuilder(); 21 for(String i : res) 22 sb.append(i); 23 return sb.toString(); 24 25 } 26 }
标签:lc6,Conversion,return,String,res,ZigZag,numRows,StringBuilder,sb 来源: https://www.cnblogs.com/hwd9654/p/10967028.html