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

convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".

题解:

  没有任何技巧,就是找规律。我们将列中元素数等于行数的列称为主列。

  由例子可以看出,除了第一行和最后一行,每一行的主列之间都会有一个值,关键就是在于找出这个值的某种规律。

  主列之间的位置差值(L 和 A)为 2*numRows - 2。中间值与同处此行中间值之前的元素(P 和 A)的位置差值为 2*numRows  - 2 - 2 * 此元素所在的行(以0为起始)

  0      6       C

  1   5 7    B D

  2 4   8 A    E

  3      9       F

  对于主列之间的位置差值。只需找到第一行主列之间的位置差值即可,因为主列从第一行到最后一行元素的位置坐标同步增长。对于行数为 n 的情况,某一主列的元素数显然等于行数 n,此主列到下一个主列之间共有 n - 2 个中间元素(减去的2是因为第一行和最后一行没有中间元素),故主列之间的位置差值为 2n - 2

  对于中间元素的位置确定

              x-(2n-2)         x    i = 0  差值    无

        `              x-1  x+1     i = 1                  2

        `           x-2      x+2         i = 2                  4

       `          x-3          x+3           i = 3                  6

        `        ······································

      x-(n)       x-(n-2)        x+(n-2)     i = n-2              2(n-2)

      x-(n-1)                      x+(n-1)     i = n-1              无

  可见对于某一行 i,主列与中间元素的差值随着行数的增加而翻倍,具体为 差值 = 2 * i

 class Solution {
public:
string convert(string s, int numRows) {
if (numRows < )
return s;
string res;
int n = s.size();
int len = * numRows - ;
for (int i = ; i < numRows; ++i) {
for (int j = i; j < n; j += len) {
res += s[j];
int mid = j + len - * i;
if (i != && i != numRows - && mid >= && mid < n)
res += s[mid];
}
} return res;
}
};

【LeetCode】006. ZigZag Conversion的更多相关文章

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

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

  2. 【LeetCode】6 - ZigZag Conversion

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

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

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

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

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

  5. 【LeetCode】281. Zigzag Iterator 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 deque 日期 题目地址:https://leetc ...

  6. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  7. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  8. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  9. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

随机推荐

  1. PHP下使用Redis消息队列发布微博

    phpRedisAdmin :github地址  图形化管理界面 git clone [url]https://github.com/ErikDubbelboer/phpRedisAdmin.git[ ...

  2. UI控件之UITableView的协议方法

    <UITableViewDataSource,UITableViewDelegate> //设置表视图的编辑状态 -(void)setEditing:(BOOL)editing anima ...

  3. 获取电脑连接WiFi的信息

    在cmd中执行如下命令,即可查看到所有连接过的WiFi信息 for /f "skip=9 tokens=1,2 delims=:" %i in ('netsh wlan show ...

  4. INSPIRED启示录 读书笔记 - 第6章 招聘产品经理

    产品经理应有的特质 个人素质和态度:技术可以学习,素质却难以培养,有些素质是成功的产品经理必不可少的 对产品的热情:对产品有一种本能的热爱,是夜以继日克服困难.完善产品的动力 用户立场:能换位思考,能 ...

  5. Python的装饰器实例用法小结

    这篇文章主要介绍了Python装饰器用法,结合实例形式总结分析了Python常用装饰器的概念.功能.使用方法及相关注意事项 一.装饰器是什么 python的装饰器本质上是一个Python函数,它可以让 ...

  6. Android编译系统简要介绍【转】

    本文转载自:http://blog.csdn.net/luoshengyang/article/details/18466779 在Android源码环境中,我们开发好一个模块后,再写一个Androi ...

  7. mongodb 中的Multikey Index Bounds解释$elemMatch

    首先说一下 $elemMatch的用法: { _id: 1, results: [ 82, 85, 88 ] } { _id: 2, results: [ 75, 88, 89 ] } $elemMa ...

  8. http 常见的错误码

    翻译自:https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html 常见错误码 一.信息 1XX (Information 1xx) ——这一类的状 ...

  9. TCP的带宽估计和丢包恢复

    一.带宽估计 TCP的带宽估计主要通过拥塞控制算法实现,用到两个变量: 1.cwnd     TCP对当前链路可用带宽的估计 2.ssthreash   拥塞控制算法“假想”出来的可用带宽值 二.丢包 ...

  10. linux中动态链接库的创建与使用

    LINUX系统中动态链接库的创建与使用 http://www.cnblogs.com/ardar/articles/357321.html 正常C源文件编写,编译时-shared即可得到SO, gcc ...