题目描述是从上到下,从左到右Z字形排列。

找规律.这种形式一般都是mod x 余数有规律。然后写的时候围绕x构造,而非判断,代码会简单一些。

设行数为r

先观察r=5的情况

发现第0行的字符原始index mod 8 ==0

第1行 mod 8 ==1 或7(-1)

...

第4行 mod 8 == 4  (-4)

也就是mod 2r-2

余0,±1,±2...±r-1

然后直接写就行。

简单写法在于,不判断index,而是直接构造,0+mod,0+mod-1;1+mod,1+mod-1,1+2*mod,1+2*mod-1;...

时间 O(n)

class Solution {
public:
string convert(string s, int numRows) { if (numRows == 1) return s; string ret;
int len = s.length();
int mod = 2 * numRows - 2; for (int i = 0; i < numRows; i++) {
for (int j = 0; j + i < len; j += mod) {
ret += s[j + i];
if (i != 0 && i != numRows - 1 && j + mod - i < len)
ret += s[j + mod - i];
}
}
return ret;
}
};

然后看了看题解,还有一种办法是分别构造每行,然后加起来。巧妙之处是设置了up和down,一个个字符判断。但是空间复杂度高一点,然后时间级别虽然一样但是会多一点。

使用当前行和当前方向这两个变量对合适的行进行跟踪。

只有当我们向上移动到最上面的行或向下移动到最下面的行时,当前方向才会发生改变。

class Solution {
public:
string convert(string s, int numRows) { if (numRows == 1) return s; vector<string> rows(min(numRows, int(s.size())));
int curRow = 0;
bool goingDown = false; for (char c : s) {
rows[curRow] += c;
if (curRow == 0 || curRow == numRows - 1) goingDown = !goingDown;
curRow += goingDown ? 1 : -1;
} string ret;
for (string row : rows) ret += row;
return ret;
}
};

LeetCode6 Z字形排列的更多相关文章

  1. [Swift]LeetCode6. Z字形变换 | ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  2. LeetCode6.Z字形变换 JavaScript

    将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L C I R E T ...

  3. Leetcode6. Z 字形变换

    > 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,按下右上下右上排列后输出字符串![在这里插入图片描述](https://img-blog.csdnimg.cn/4578280a7c1848c ...

  4. LeetCode6. Z字形变换

    描述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

  5. 将字符串中的字符按Z字形排列,按行输出

    示例1: Input: s = "PAYPALISHIRING", numRows = 3 Output: "PAHNAPLSIIGYIR" 示例2: Pyth ...

  6. leetcode刷题六<z字形变换>

    将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 时,排列如下: L C I R E T O E S I I G E D H N 之后,你的输出需要从左往右逐 ...

  7. [LeetCode] 6. Z 字形变换

    题目链接:(https://leetcode-cn.com/problems/zigzag-conversion/) 题目描述: 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列 ...

  8. Z 字形变换

    将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L C I R E T ...

  9. LeetCode(6):Z字形转换

    Medium! 题目描述: 将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数:(下面这样的形状) P A H N A P L S I I G Y I R 之后按 ...

随机推荐

  1. 使用memory_profiler异常

    在使用memory_profiler模块0.55.0版本执行命令诊断程序内存用量时,遇到下面错误: C:\Users\Chen\Desktop\python_doc\第四模块课件>python ...

  2. 针对Linux系统主机,进入修复模式,解决开机报错问题

    1.让主机重启,进入开机时的内核选择界面,按e进入编辑界面 2.找到linux16那一行,将光标移动到最前面,按下End键,到这一行的末尾,然后空格 rd.break console=tty0 3.第 ...

  3. DOI技术扫盲一

    DOI:  desktop office intergration   桌面办公软件集成简单的将,就是我们在Windows桌面中打开的办公软件(如:word,excel,pdf等等)可以在SAP系统进 ...

  4. 。SLI,Service Level Indicator,服务等级指标,其实就是我们选择哪些指标来衡量我们的稳定性。而 SLO,Service Level Objective,服务等级目标,指的就是我们设定的稳定性目标,比如“几个 9”这样的目标。

    .SLI,Service Level Indicator,服务等级指标,其实就是我们选择哪些指标来衡量我们的稳定性.而 SLO,Service Level Objective,服务等级目标,指的就是我 ...

  5. (Oracle)取当前日期的最近工作日

      描述:现有一需求,日期表中存放了日期和是否节假日(0-工作日,1-节假日),现在需要取日期表中的最近的工作日.如2017/07/23(周日)最近的工作日应该是2017/07/21(周五).     ...

  6. 济南学校D1T3_hahaha

    [问题描述] 小Q对计算几何有着浓厚的兴趣.他经常对着平面直角坐标系发呆,思考一些有趣的问题.今天,他想到了一个十分有意思的题目: 首先,小Q会在轴正半轴和轴正半轴分别挑选个点.随后,他将轴的点与轴的 ...

  7. Spring5源码,Spring Web中的处理程序执行链

    一.什么是Spring中的处理程序执行链? 二.HandlerExecutionChain类 三.自定义处理程序执行链 Spring的DispatcherServlet假如缺少几个关键元素将无法分派请 ...

  8. SpringApplication.run

    SpringApplication.run一共做了两件事,分别是 创建SpringApplication对象 利用创建好的SpringApplication对象,调用run方法 1.创建SpringA ...

  9. H3C交换机端口聚合配置

    1.接入交换机: interface Ten-GigabitEthernet1/0/21 port link-mode bridge port link-type trunk port trunk p ...

  10. HarmonyOS三方件开发指南(8)——RoundedImage

    [小年答谢,新春送礼]免费抽取1000元京东卡+更多新春好礼~查看详情>>> 目录: 1. RoundedImage组件功能介绍 2. RoundedImage使用方法 3. Rou ...