import java.util.ArrayList;
import java.util.List;
public class Solution {
public String convert(String s, int numRows) {
if(s == null || s.trim().isEmpty() || s.length() <= numRows || numRows == 1)
return s;
List<List<Character>> nList = new ArrayList<>();
for(int i = 0; i < numRows; i++){
nList.add(new ArrayList<>());
}
int pos = 0;
int i = 0;
while (i < s.length()) {
System.out.println("pos=" + Math.abs(pos));
nList.get(Math.abs(pos)).add(s.charAt(i++));
if (pos == numRows - 1)
pos = -pos;
pos++;
}
StringBuilder sb = new StringBuilder();
for(List<Character> iList : nList){
for(char c : iList){
sb.append(c);
}
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(new Solution().convert("PAYPALISHIRING", 3));
}
}
之字形字符串转换
最新推荐文章于 2021-12-10 12:21:44 发布