• 0006. ZigZag Conversion (M)


    ZigZag Conversion (M)

    题目

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

    P   A   H   N
    A P L S I I G
    Y   I   R
    

    And then read line by line: "PAHNAPLSIIGYIR"

    Write the code that will take a string and make this conversion given a number of rows:

    string convert(string s, int numRows);
    

    Example 1:

    Input: s = "PAYPALISHIRING", numRows = 3
    Output: "PAHNAPLSIIGYIR"
    

    Example 2:

    Input: s = "PAYPALISHIRING", numRows = 4
    Output: "PINALSIGYAHRPI"
    
    Explanation:
    
    P     I      N
    A   L S    I G
    Y A   H  R
    P     I
    

    题意

    给定字符串,将其按照指定行数进行之字形的重新排列,按顺序输出新图形的每一行字符。

    思路

    直接模拟字符重排的过程,按顺序将每一个字符存入对应行号的字符串中,注意在第一行和最后一行处要改变行号变化的方向。


    代码实现

    Java

    class Solution {
        public String convert(String s, int numRows) {
            // 特殊情况直接返回字符串
            if (numRows == 1) {
                return s;
            }
            
            // 分别保存重排后的每一行
            List<StringBuilder> sbs = new ArrayList<>();
            for (int i = 0; i < numRows; i++) {
                sbs.add(new StringBuilder());
            }
            
            // row指定当前字符对应的行号
            int row = 0;
            // flag用于判断行号增减方向
            boolean flag = false;
            // 按顺序将字符保存到对应的行字符串中
            for (char c : s.toCharArray()) {
                sbs.get(row).append(c);
                // 在第一行和最后一行要变换行号增减方向
                if (row == 0 || row == numRows - 1) {
                    flag = !flag;
                }
                if (flag) {
                    row++;
                } else {
                    row--;
                }
            }
            
            StringBuilder res = new StringBuilder();
            for (StringBuilder sb : sbs) {
                res.append(sb);
            }
            return res.toString();
        }
    }
    

    JavaScript

    /**
     * @param {string} s
     * @param {number} numRows
     * @return {string}
     */
    var convert = function (s, numRows) {
      if (numRows === 1) {
        return s
      }
    
      let rows = []
      for (let i = 0; i < numRows; i++) {
        rows[i] = []
      }
      let line = 0
      let flag = false
      for (let c of s) {
        rows[line].push(c)
        if (line === 0 || line === numRows - 1) {
          flag = !flag
        }
        line += flag ? 1 : -1
      }
      return rows.map((chars) => chars.join('')).join('')
    }
    
  • 相关阅读:
    JavaScript小技巧总结
    Table边框使用总结 ,只显示你要显示的边框
    连续字符换行及单行溢出点点点显示
    对ThreadLocal的理解个人
    Linux面试题答案解析
    com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class
    21道 Redis 常见面试题,必须掌握!
    MyBatis中的#与$
    Linux下zookeeper的搭建
    web.xml加载顺序与过程
  • 原文地址:https://www.cnblogs.com/mapoos/p/13130252.html
Copyright © 2020-2023  润新知