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. Apache commons-io实现多文件读取和写入

    需求: "E:/data/"目录下有四个文件夹,如下: 每个文件夹下有几个.csv文件,如下: 将每个文件夹下的.csv文件合并成一个以该文件夹命名的.csv文件. 做法: 找到& ...

  2. Linux文件系统管理 fdisk分区命令

    概述 我们在安装操作系统的过程中已经对系统硬盘进行了分区,但是如果我新添加了一块硬盘,想要正常使用时,在Linux中有专门的分区命令 fdisk 和 parted.其中 fdisk 命令较为常用,但不 ...

  3. gstreamer-tips-picture-in-picture-compositing

    http://www.oz9aec.net/index.php/gstreamer/347-more-gstreamer-tips-picture-in-picture-compositing htt ...

  4. linux基础三---网络基础&软件包管理

    一 ifconfig:显示所有正在启动的网卡的详细信息或设定系统中网卡的IP地址. ifconfig eno16777736 down/up   关闭/开启 eno16777736 网卡 ifconf ...

  5. Go 字符串相关-标准库

    标准库中有四个包对字符串处理尤为重要: bytes strings strconv unicode strings包提供了许多如字符串的查询.替换.比较.截断.拆分和合并等功能. bytes包也提供了 ...

  6. myEclipse 2014 破解教程

    因为经常在不同电脑里安装配置下载myEclipse,所以干脆记录下来,一直找度娘也是很麻烦的. 此教程仅对myEclipse2014 有效. 破解工具:https://pan.baidu.com/s/ ...

  7. Virtio SCSI设备介绍

    Qemu的存储栈 在KVM虚拟化环境中,当客户机的内核存储系统像在物理机上一样通过页缓存.文件系统.通用块设备层运行到实际设备驱动时,这时驱动对设备寄存器的访问会触发CPU从客户机代码切换到物理机内的 ...

  8. neutron 虚拟机网络问题调试

    1. Security Group全部打开,这是最基本的,但是很多人容易忘记 2. 通过界面查看虚拟机的log,也可以在compute节点上查看console.log文件,看看里面是否有DHCP获取I ...

  9. review11

    public byte[] getBytes()方法使用平台默认的字符编码,将当前字符串转换为一个字节数组.如 byte d[] = "Java你好".getBytes(); 如果 ...

  10. zhly

    5. 百叶002 名字2008 1.新浪   阿里矢量图库账号 15031116087 名字2008 2.acdsee 账号  1173209945  同密码一样 3.zhly   我的名字2016 ...