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".

详见:https://leetcode.com/problems/zigzag-conversion/description/

解题思路:
1. 每一组 V 字形的长度为 size = 2 * line - 2 (斜向上的部分没有第一行和最后一样的元素),line 是行数
2. 对于垂直向下的一列元素来说,每一组向下的列之间间隔 size 大小,即一组元素的个数。
3. 对于斜向上的元素来说,它们的位置位于当前组 size - i(i 为该元素所在的行数,一组有 size 个字符,倒数第 i 个,位置为 size -i)。当前组的第一个字符所在位置为 j - i (j 为与斜向上的元素同在一行的,垂直向下的列的元素在字符串中的序号,i 为它们共同的行号)。
即:j-i就是zigzag的起始字符,然后我们是要倒数第i个,因为一个zigzag有size个字符,所以倒数第i个就是size-i,我们要赋值的字符就是(j-i)+(size-i)=j+size-2*i

class Solution {
public:
string convert(string s, int numRows) {
if(s.empty()||numRows==1)
{
return s;
}
string res="";
int len=s.size();
int size=numRows*2-2;
for(int i=0;i<numRows;++i)
{
for(int j=i;j<len;j+=size)
{
res+=s[j];
if(i!=0&&i!=numRows-1&&j+size-2*i<len)
{
res+=s[j+size-2*i];
}
}
}
return res;
}
};

006 ZigZag Conversion的更多相关文章

  1. No.006 ZigZag Conversion

    6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...

  2. LeetCode--No.006 ZigZag Conversion

    6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...

  3. 【LeetCode】006. ZigZag Conversion

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

  4. 【JAVA、C++】LeetCode 006 ZigZag Conversion

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

  5. [Leetcode]006. ZigZag Conversion

    public class Solution { public String convert(String s, int nRows) { if (s == null || s.isEmpty() || ...

  6. 《LeetBook》leetcode题解(6): ZigZag Conversion[E]

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

  7. 【leetcode❤python】 6. ZigZag Conversion

    #-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object):    def convert(self, s, numRow ...

  8. 64. ZigZag Conversion

    ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...

  9. leetcode第六题 ZigZag Conversion (java)

    ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...

随机推荐

  1. scrollHeight

    scrollHeight=显示内容高度+隐藏内容高度 参考: https://developer.mozilla.org/en-US/docs/Web/API/Element.scrollHeight ...

  2. BZOJ3401:[USACO2009MAR]Look Up

    浅谈栈:https://www.cnblogs.com/AKMer/p/10278222.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?id ...

  3. python构造一个http请求

    我们经常会用python来进行抓包,模拟登陆等等, 势必要构造http请求包. http的request通常有4个方法get,post,put,delete,分别对应于查询,更新,添加,删除.我们经常 ...

  4. iOS获取设备型号的方法

    1. [UIDevice currentDevice].model   自己写的看只抓到模拟器和iPhone.暂时不推荐. 2.自己写的找的方法再添加.直接  NSString * deviceMod ...

  5. HDOJ2141(map在二分搜索中的应用)

    #include<iostream> #include<cstdio> #include<map> #include<algorithm> using ...

  6. SQL常用语法及规则-表格的操作

    一.规则和标准 1)每一行SQL语句结尾,加分号: 2)所创建的对象,名字用反引号(不是引号,与~同一个键): 3)一般关键字或保留字要大写: 4)两个中划线 + 空格(-- ),后面的语句为注释语句 ...

  7. web攻击之四:DOS攻击

    DDOS是DOS攻击中的一种方法. DoS:是Denial of Service的简称,即拒绝服务,不是DOS操作系统,造成DoS的攻击行为被称为DoS攻击,其目的是使计算机或网络无法提供正常的服务. ...

  8. 前端框架:template

    ylbtech-前端框架: 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http://ylbtech.cn ...

  9. ng2-升级脚手架到最新版本

    //先卸载,后安装 npm uninstall -g @angular/cli npm cache clean npm install -g @angular/cli@latest

  10. shell批量创建文件及改名

    批量创建文件及改名企业面试题2:使用for循环在/usr/sunzy目录下通过随机小写10个字母,批量创建10个html文件. #!/bin/bash Path=/usr/sunzy [ -d $Pa ...