public String convert(String s, int nRows) {
String finalString = "";
char []array = s.toCharArray();
if(nRows == 1){
return s;
}
String []string = new String[nRows];
for(int i = 0; i < nRows; i++){
string[i] = "";
}
int index = -1;
int step = 1;
for(int i = 0; i < array.length; i++){
index += step;
if(index == nRows){
step = -1;
index = nRows - 2;
}else if(index == -1){
step = 1;
index = 1;
}
string[index] += array[i];
}
for(int i = 0; i < nRows; i++){
//System.out.println(string[i]);
finalString += string[i];
}
return finalString;
}
思路:设置好计数器,方便计算所对应数字符串数组的下标。O(n)
leetcode----Zigzag Conversion
最新推荐文章于 2024-08-14 17:00:26 发布