博客
关于我
leetcode做题记录0006
阅读量:354 次
发布时间:2019-03-04

本文共 1438 字,大约阅读时间需要 4 分钟。

问题描述

给定一个字符串和一个整数numRows,要求将字符串中的字符按照特定规则排列成numRows行。每行的字符数等于numRows,字符顺序是从下到上,每次移动一行,遇到边界时改变方向。

思路

  • 问题分析:我们需要将字符串中的字符分成多行,每行包含numRows个字符,字符顺序是从下到上移动,每次移动一行,遇到顶部或底部时改变方向。

  • 解决思路

    • 创建一个数组来保存每一行的字符列表。
    • 使用方向变量来控制当前移动方向(上或下)。
    • 遍历字符串中的每个字符,根据当前方向和当前所在行来决定下一个字符的位置。
    • 当到达顶部或底部时,改变方向继续移动。
  • 算法选择:使用循环遍历字符串中的每个字符,并根据当前状态更新每一行的字符列表。

  • 复杂度分析:时间复杂度为O(n),其中n是字符串的长度,空间复杂度为O(n),用于存储每一行的字符列表。

  • 解决代码

    public class Solution {    public String convert(String s, int numRows) {        if (numRows == 1) {            return s;        }        StringBuilder[] sbs = new StringBuilder[numRows];        for (int i = 0; i < numRows; i++) {            sbs[i] = new StringBuilder();        }        int currentRow = 0;        int direction = 1; // 1表示向下,-1表示向上        for (char c : s.toCharArray()) {            sbs[currentRow].append(c);            if (currentRow == 0) {                direction = 1;            } else if (currentRow == numRows - 1) {                direction = -1;            }            currentRow += direction;        }        StringBuilder result = new StringBuilder();        for (int i = 0; i < numRows; i++) {            result.append(sbs[i]);        }        return result.toString();    }}

    代码解释

  • 初始化:检查numRows是否为1,如果是,直接返回原字符串。否则,创建一个大小为numRows的StringBuilder数组。

  • 循环填充字符:遍历字符串中的每个字符,根据当前行和方向决定字符的位置。当前行从0开始,方向初始为向下(1)。

  • 方向调整:当当前行达到顶部(0)或底部(numRows - 1)时,改变方向。

  • 构建结果:将每一行的StringBuilder内容合并到一个结果StringBuilder中,最后返回结果字符串。

  • 这个方法高效且直接,能够处理各种情况,包括字符串长度不足和行数变化。

    转载地址:http://krhe.baihongyu.com/

    你可能感兴趣的文章
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>
    no1
    查看>>
    NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
    查看>>
    NOAA(美国海洋和大气管理局)气象数据获取与POI点数据获取
    查看>>
    NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
    查看>>
    node exporter完整版
    查看>>
    Node JS: < 一> 初识Node JS
    查看>>
    Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime(72)
    查看>>