题目链接

  题目要求:

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

  为了解决这道题,我们再举一个例子:

  

  如果我们把上图稍微转变一下:

  

  也就是说,其实我们可以把中间斜线处的字母移到他们左边竖线字母下边,这样就更方便我们用二维矩阵来表示了。因此,我们可以将输入字符串分为等长的几个部分,不足的补充特别字符,如下图:

  

  根据这种想法写出来的程序如下:

 class Solution {
public:
string convert(string s, int numRows) {
int szS = s.size();
if(szS == || numRows < || szS <= numRows)
return s; int nRows = numRows + numRows - ;
int nCols = szS / nRows + ;
vector<vector<char> > matrix(nRows, vector<char>(nCols, '#'));
int k = ;
for(int j = ; j < nCols; j++)
for(int i = ; i < nRows; i++)
{
if(k < szS)
{
matrix[i][j] = s[k];
k++;
}
} string retStr;
for(int i = ; i < numRows; i++)
for(int j = ; j < nCols; j++)
if(i == || i == numRows - )
{
if(matrix[i][j] != '#')
retStr += matrix[i][j];
}
else
{
if(matrix[i][j] != '#')
retStr += matrix[i][j];
if(matrix[nRows - i][j] != '#')
retStr += matrix[nRows - i][j];
} return retStr;
}
};

  虽然没超时,但它的运行时间达到了49ms,另外占用的内存也比较多。

  其实,我们有必要再构造一个二维数组吗?是没必要的。修改之后的程序如下:

 class Solution {
public:
string convert(string s, int numRows) {
int szS = s.size();
if(szS == || numRows < || szS <= numRows)
return s; int nRows = numRows + numRows - ;
int nCols = szS / nRows + ;
string retStr;
for(int i = ; i < numRows; i++)
{
for(int j = ; j < nCols; j++)
{
if((i == || i == numRows - ) && (i + j * nRows < szS))
retStr += s[i + j * nRows];
else
{
if(i + j * nRows < szS)
retStr += s[i + j * nRows];
if(nRows - i + j * nRows < szS)
retStr += s[nRows - i + j * nRows];
}
}
} return retStr;
}
};

  修改后的程序运行时间仅有16ms,是原来的1/3。

  

LeetCode之“字符串”:ZigZag Conversion的更多相关文章

  1. leetcode题解 6.ZigZag Conversion

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

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

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

  3. 字符串按照Z旋转90度然后上下翻转的字形按行输出字符串--ZigZag Conversion

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

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

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

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

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

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

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

  7. 【LeetCode】6 - ZigZag Conversion

    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 problem 6 ZigZag Conversion

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

  10. 【LeetCode】006. ZigZag Conversion

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

随机推荐

  1. 查看4k对齐,激活.net framework 3.5

    查看是否4k对齐 Win+R,打开运行窗口,在窗口中输入“msinfo32",组件”--“存储”--“磁盘”.然后可以在右边栏看到“分区起始偏移”,我们图例中有2个数值,分别是:32256字 ...

  2. Chrome 内存和CPU消耗量双料冠军

    今天统计了下某个时刻各进程的内存和CPU使用概况.结果发现,Chrome消耗量真是不一般的大.比Windows主进程都还猛! 另外发现百度安全卫士占用CPU也比较猛. powershell下输入: p ...

  3. JAVA面向对象-----局部内部类

    局部内部类 局部内部类概述:包含在外部类的函数中的内部类称之为局部内部类. 访问:可以在包含局部内部类的方法中直接创建局部内部类的对象调用局部内部类的成员. 注意:局部内部类只能访问所在函数的fana ...

  4. Dynamics CRM EXCEL导入数据字段类型为选项集时的注意事项

    在开始先展示下CRM的导入数据涉及选项集字段时的一个问题 下图是选项集字段的属性 下图是我要导入的excel中的列值,可以看出列明和字段名是一致的,而列值却不是选项集中已有的选项 在导入校验时,只要字 ...

  5. 实现memmove函数

    分析:memmove函数是<string.h>的标准函数,其作用是把从source开始的num个字符拷贝到destination.最简单的方法是直接复制,但是由于它们可能存在内存的重叠区, ...

  6. 安卓6.0新特性在Fragment申请运行时权限

    今天在Fragment申请权限时代码如下: public void getContacts(){ int flag = ActivityCompat.checkSelfPermission(getAc ...

  7. Java基础----Java---集合框架---泛型、泛型方法、静态方法泛型、泛型接口、泛型限定、泛型类

    泛型:jdk1.5后的新特性,用于解决安全问题,是一个安全机制. 好处: 1.将运行时的异常出现问题classcastException.转移到了编译时期.方便程序员调试解决问题,让运行事情问题减少, ...

  8. Ruby开发入门

    开发环境搭建 首先安装Ruby SDK,我安装的版本是2.0.之后安装IDE,这里用的是Jetbrain的RubyMine 5.4.3,注意是否支持对应版本的Ruby SDK. 一段神奇的注册码... ...

  9. DB2数据库代码页和实例代码页的区别(解决DB2乱码问题)

    DB2CODEPAGE:     DB2 实例级别的代码页设置,它会影响DB2相关应用程序对代码页转换时做出代码页判定.     可以通过 db2set DB2CODEPAEG= 命令将 DB2COD ...

  10. 1033. To Fill or Not to Fill (25) -贪心算法

    题目如下: With highways available, driving a car from Hangzhou to any other city is easy. But since the ...