按照Z型排列字符串并且按行读取
思路:存每行的字符串,最后再组合到一起
因为不习惯c++的string操作,还是用java了
class Solution {
public String convert(String s, int numRows) {
String[] builder = new String[numRows];
int len = s.length();
if (numRows>=len || numRows==1) return s;
int t = 2*numRows - 2;
int max=t/2+1;
int j=0;
for(int i=1;i<=len;i++){
int temp=i%t;
if(temp==0) j=2;
else if(temp <= max) j=temp;
else j=2*max - temp;
if(builder[j-1]==null) builder[j-1] = "";
builder[j-1] += s.charAt(i-1);
}
StringBuilder res = new StringBuilder();
for(int i=0;i<numRows;i++) res.append(builder[i]);
return res.toString();
}
}
很慢,待优化