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. OS X 与传统Unix的一点区别

    在传统的Unix系统或者Linux系统中,你是很难在根目录下找到大写开头的文件夹的, 但是看一下OS X: ls / Applications Users etc private var Develo ...

  2. DEC VT100 terminal

  3. Shell编程之Linux信号及信号跟踪

    一.Linux信号 1.什么是信号? Linux信号是由一个整数构成的异步消息,它可以由某个进程发给其他进程,也可以在用户按下特定键发生某种异常事件时,由系统发给某个进程. 2.信号列表 [root@ ...

  4. Hyperledger Fabric1.0环境搭建

    一.准备CentOS系统,本文使用的是CentOS7.0 二.安装Docker 执行命令 yum -y install docker 验证是否安装成功 docker --version 三.安装Doc ...

  5. Golang 高性能UDP Server实现

    通过Goroutine实现UDP消息并发处理 package main import ( "net" "fmt" "os" ) // 限制g ...

  6. java基础(2)-面向对象(1)

    面向对象 面向对象思想 面向对象是相对面向过程而言 面向对象和面向过程都是一种思想 面向过程:强调的是功能行为 面向对象:将功能封装进对象,强调具备了功能的对象 面向对象是基于面向过程的 面向对象举例 ...

  7. UvaLive 5811 概率DP

    题意 : 有54张牌 问抽多少张牌能使每种花色都至少是给定的数字 两张王牌可以被选择为任何花色 高放学长真是太腻害辣! 设置dp[][][][][x][y] 前四维代表四种真的花色的数量 后两维xy代 ...

  8. 比较好的sql写法

    DECLARE @beginTime VARCHAR(20)= '2017-11-13 00:00:00';DECLARE @endTime VARCHAR(20)= '2017-11-13 23:0 ...

  9. QPushButton 响应回车 设置默认按钮

    ui.pushButton->setFocus(); //设置默认焦点 ui.pushButton->setShortcut( QKeySequence::InsertParagraphS ...

  10. jQuery download file

    jQuery.download = function (url, method, p, c, e, i, o, goodsType, reciveUser, suplier) { jQuery('&l ...