我现在在做一个叫《leetbook》的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看
书的地址:https://hk029.gitbooks.io/leetbook/

006.ZigZag Conversion[E]


题目

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)

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 text, int nRows);

convert(“PAYPALISHIRING”, 3) should return “PAHNAPLSIIGYIR”.

思路1——用字符串数组

我能说我一开始完全没看懂吗?我是根据Custom Testcase自己慢慢测试摸索出来的。
其实,应该是这样的
2行:
A _ C _ E _
_ B _ D _ F

3行:
A _ _ _ E _ _ _ I _ _ _
_ B _ D _ F _ H _ J _
_ _ C _ _ _ G _ _ _ K

所以有个简单的思路:
- 每行弄个string。
- 对原始字符串进行扫描,从上往下,从下往上,依次加入每行的string
- 最后把所有的string拼接起来

 class Solution {
public:
string convert(string s, int numRows) {
string str[numRows],tmp;
if(numRows == 1)
return s;
int flag;
for(int i = 0,j = 0;i < s.length(); i++)
{
if(j == 0)
flag = 1;
if(j == numRows-1)
flag = -1;
str[j] += s[i];
j += flag;
}
for(int i = 0; i < numRows;i++){
tmp += str[i];
}
return tmp;
}
};

思路2——观察规律

2行:
A _ C _ E _
_ B _ D _ F

3行:
A _ _ _ E _ _ _ I _ _ _
_ B _ D _ F _ H _ J _
_ _ C_ _ _ G _ _ _ K

4行:
A _ _ _ _ _ G _ _ _ _ _
_ B _ _ _ F _ H _ _ _
_ _ C _ E _ _ _ I _ K
_ _ _ D _ _ _ _ _ J

观察规律后,以每行的元素作为轴,可以发现,下面的字母都是对称排列的
换成对应的index后,规律更明显
0 _ _ _ 4 _ _ _ 8 _ _ _
_ 1 _ 3 _ 5 _ 7 _ 9 _
_ _ 2 _ _ _ 6 _ _ _ 10

第2层的元素就是以第一行的元素为轴,+1,-1
第三层的元素就是以第一行的元素为轴,+2,-2
……
但是最后一层的元素,由于其特殊性,我们可以只考虑+k

Ps.所有过界的元素都不考虑

轴也有规律:除了首尾两层,其他都是2个,所以第一层每隔2n-2出现一次。

class Solution {
public:
string convert(string s, int numRows)
{
string tmp;
if(numRows == 1)
return s;
int inc = 2*numRows-2; //每次轴增加的步长
int len = s.length();
for(int i = 0; i < numRows;i++)
{
for(int j = 0;j < s.length()+numRows; j += inc)
{
if(j - i > 0 && j - i < s.length()
&& i != 0 && i != numRows -1) //首,尾只考虑+不考虑-
tmp+= s[j-i];
if(j + i < s.length())
tmp += s[j+i];
}
}
return tmp;
}
};

《LeetBook》leetcode题解(6): ZigZag Conversion[E]的更多相关文章

  1. leetcode题解 6.ZigZag Conversion

    6.ZigZag Conversion 题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a gi ...

  2. 【LeetCode】6. ZigZag Conversion Z 字形变换

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字形变换,ZigZag,题解,Leetcode, 力扣,P ...

  3. 【一天一道LeetCode】#6 ZigZag Conversion

    一天一道LeetCode系列 (一)题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given ...

  4. 【LeetCode】006. ZigZag Conversion

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

  5. 【LeetCode】6 - ZigZag Conversion

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

  6. leetcode problem 6 ZigZag Conversion

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

  7. LeetCode OJ:ZigZag Conversion(字符串的Z字型转换)

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

  8. LeetCode(6) - ZigZag Conversion

    这个题的要求是给你一个字符串,和一个行数,例如(s = "mysisteristhemostlovelygirl" , row = 4),每一行一个字符串,但是s却得按照zigza ...

  9. 【LeetCode】6. ZigZag Conversion 锯齿形转换

    题目: 思路: 以图为例:s={'A','B','C','D','E','F','G','H'.....} 1.先不考虑中间元素F.G.H.N...,每一行前后元素在数组中对应下标相差size=2*n ...

随机推荐

  1. angularjs之事件绑定、解除事件绑定

    今天在开发时,遇到一个坑,花了一下午时间也没找到原因,无奈小菜鸟只能寻求公司里大牛的帮助,果然,大牛就是大牛,对比了几个输出结果,就看出问题所在.所以小菜鸟当然不会错过这个分享的时机啦~废话不多说进入 ...

  2. POJ2031 Building a Space Station 2017-04-13 11:38 48人阅读 评论(0) 收藏

    Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8572   Accepte ...

  3. 试题 E: 迷宫 第十届蓝桥杯

    [问题描述]下图给出了一个迷宫的平面图,其中标记为 1 的为障碍,标记为 0 的为可以通行的地方.010000000100001001110000迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个 ...

  4. java中的四种代码块

    一.普通代码块 直接在一个方法中出现的{}就称为普通代码块,例子程序如下: public class CodeDemo01{ public static void main(String[] args ...

  5. Thread in depth 1: The basic

    Every single thread has the follow elements: Execution Context:Every thread has a execution context ...

  6. 浅谈 温故知新——HTML5!

    古人有云:温故而知新.活到老,学到老,作为一枚前端的程序猿,不停的学习能够让我们对我们的技术有一个更加丰富的认识.这几天,项目已经完成,但我发现自己的知识体系存在一些短板,特别是在H5方面,所以我又回 ...

  7. js 倒计时,转义

    function leftTimer(time) { var leftTime = (new Date(time)) - (new Date()); //计算剩余的毫秒数 var days = par ...

  8. SQL Union 和Union All 的区别

    Union与Union All的区别 如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或称为联合)的作用是将多个结果合并 ...

  9. golang plugin的依赖问题

    golang plugin的依赖问题 此文中涉及的plugin运行环境为mac 10.14,go版本为1.11 主要是想讨论一下插件依赖的第三方库的问题. 例子是在https://github.com ...

  10. kolla-ansible 重新部署 ceph 遇到的问题

    问题 TASK [ceph : Fetching Ceph keyrings] ******************************************* fatal: [controll ...