题解 | #牛牛的旗语传递#
牛牛的旗语传递
https://www.nowcoder.com/practice/810b1c80a9c341c4af69facac350d6bc
import java.util.*;
public class Solution {
/**
* 利用stringbuffer或stringbuilder装数据,用flag不停转换位置
*/
public String decodeFlag (String s, int numRows) {
if(numRows<2) return s;
List<StringBuffer> list=new ArrayList<>();
for(int i=0;i<numRows;i++){
list.add(new StringBuffer());
}
int i=0,flag=-1;
for(char c:s.toCharArray()){
list.get(i).append(c);
if(i==0||i==numRows-1) flag=-flag;
i+=flag;
}
StringBuffer res=new StringBuffer();
for(StringBuffer row :list) res.append(row);
return res.toString();
}
}
查看12道真题和解析