题解 | #牛牛的旗语传递#

牛牛的旗语传递

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) {
        // write code here
        String[] temp = new String[numRows];    //记录每行的字符串
        int index = 0;  //字符串索引
        int flag = -1;  //flag为1时从下至上记录,flag为-1时从上至下记录
        Arrays.fill(temp , "");     //初始化temp
        for (int i = 0; i < s.length(); i++) {
            //当索引到边界时变向
            if(index == 0 || index == numRows - 1)
                flag = -flag;
            temp[index] += s.charAt(i); //记录数据
            index += flag;  //更换索引
        }
        //结果
        String res = "";
        for (String str : temp) {
            res += str;
        }
        return res;
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务