LeetCode 6. Z字形变换(ZigZag Conversion)
题目描述
将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数:
P A H N
A P L S I I G
Y I R
之后从左往右,逐行读取字符:"PAHNAPLSIIGYIR"
实现一个将字符串进行指定行数变换的函数:
string convert(string s, int numRows);
示例 1:
输入: s = "PAYPALISHIRING", numRows = 3
输出: "PAHNAPLSIIGYIR"
示例 2:
输入: s = "PAYPALISHIRING", numRows = 4
输出: "PINALSIGYAHRPI"
解释: P I N
A L S I G
Y A H R
P I
解题思路
把字符串按行输出时要考虑首尾与中间情况,所以首先计算出每个Z字形周期内的字符个数,然后求出周期数,接着对每一行的每个周期依次遍历,对于首尾行的字符直接添加;对于中间行的字符要再添加Z字中间的字符。对于最后一个周期,要判断是否走到了字符串末尾。
代码
class Solution {
public:
string convert(string s, int numRows) {
if(numRows == ) return s;
int cycle = * numRows - ;
int numZ = s.length() / cycle;
string res = "";
for(int i = ; i < numRows; i++){
for(int j = ; j <= numZ; j++){
if(j * cycle + i >= s.length()) continue;
if(i == || i == numRows - )
res += s[j * cycle + i];
else{
res += s[j * cycle + i];
int idx = j * cycle + cycle - i;
if(idx < s.length())
res += s[idx];
}
}
}
return res;
}
};
LeetCode 6. Z字形变换(ZigZag Conversion)的更多相关文章
- [Swift]LeetCode6. Z字形变换 | ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- C#版[击败100.00%的提交] - Leetcode 6. Z字形变换 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- Java实现 LeetCode 6 Z字形变换
6. Z 字形变换 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L ...
- [LeetCode] 6. Z 字形变换
题目链接:(https://leetcode-cn.com/problems/zigzag-conversion/) 题目描述: 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列 ...
- Z 字形变换 C++实现 java实现 leetcode系列(六)
Z 字形变换 java实现 C++实现 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 ...
- Leetcode(6)Z字形变换
Leetcode(6)Z字形变换 [题目表述]: 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" ...
- Leetcode题库——6.Z字形变换
@author: ZZQ @software: PyCharm @file: convert.py @time: 2018/9/20 20:12 要求: Z字形变换 将字符串 "PAYPAL ...
- LeetCode Golang 6. Z 字形变换
6. Z 字形变换 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L ...
- 【LeetCode】6. ZigZag Conversion Z 字形变换
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字形变换,ZigZag,题解,Leetcode, 力扣,P ...
随机推荐
- box-sizeing
在大多数情况下我们在设置元素的 border 和 padding 并不希望改变元素的 width,height值,这个时候我们就可以为该元素设置 box-sizing:border-box;. 我不希 ...
- yii2-cache组件第三个参数Dependency $dependency的作用浅析
用法如下: $cache->set($key, $result, Configs::instance()->cacheDuration, new TagDependency([ 'tags ...
- mysql8安装
1.先卸载当前系统中已安装的mariadb rpm -qa | grep mariadb rpm -e --nodeps 文件名 2.安装mysql依赖包 yum install gcc gcc-c+ ...
- JAVA程序员成长路线图
https://www.cnblogs.com/godtrue/p/4283708.html
- 卸载CUDA和cuDNN
卸载CUDA和cuDNN 1.卸载CUDA 本教程只针对对于.run方式安装的,其他的没有进行测试 打开终端,输入sudo /usr/local/cuda-10.0/bin/uninstall_cud ...
- 网络协议相关面试问题-TLS与SSL握手
HTTPS是什么? HTTPS并不是一个单独的协议,而是对工作在一加密连接(SSL / TLS)上的常规HTTP协议.通过在TCP和HTTP之间加入TLS(Transport Layer Securi ...
- Appium简介以及环境安装
官网地址 Appium 是一个自动化测试开源工具,支持多平台上的原生应用,web应用和混合应用,是由appium server和appium Client两部分组成通过json wire protoc ...
- docker下安装运行mysql的过程以mysql5.7为例
一.查找mysql资源 docker search mysql 其实这步顶多是看看有哪些mysql资源,除非你自己commit过一个特定的版本,否则直接执行下一步 二.安装mysql docker p ...
- BZOJ 2836: 魔法树 (树链剖分+线段树)
板题-记得开longlong #include <cstdio> #include <cctype> #include <cstring> #include < ...
- 2 APIView与序列化组件
1.入门 1.1 参考blog 官方文档:http://www.django-rest-framework.org/tutorial/quickstart/#quickstart yuan的Blog: ...