题解 | #牛牛的旗语传递#
牛牛的旗语传递
https://www.nowcoder.com/practice/810b1c80a9c341c4af69facac350d6bc
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param s string字符串
* @param numRows int整型
* @return string字符串
*/
public String decodeFlag (String s, int numRows) {
StringBuilder sb = new StringBuilder("") ;
int[][] a = new int[numRows][] ;
a[0] = new int[]{0} ;
for(int i = 1;i < numRows-1;i++){
a[i] = new int[]{i,numRows+(numRows-2)-i} ;
}
a[numRows-1] = new int[]{numRows-1} ;
int add = (numRows+(numRows-2)) ;
int cols = s.length()/add ;
for(int i = 0;i < numRows;i++){
for(int j = 0;j <= cols;j++){
for(int dd:a[i]){
if(dd+j*add < s.length()){
sb.append(s.charAt(dd+j*add)) ;
}
}
}
}
return sb.toString() ;
// write code here
}
}
不模拟打印zigzag字符串,直接利用numRows+(numRows-2)为一个循环计算
