题目链接

  题目要求:

  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. Redis 学习笔记4: Redis 3.2.1 集群搭建

    在CenOS 6.7 linux环境下搭建Redis 集群环境 1.下载最新的Redis版本 本人下载的Redis版本是3.2.1版本,下载之后,解压,编译(make): 具体操作可以参考我的博文:R ...

  2. activiti bpmnModel使用

    bpmnModel对象,是activiti动态部署钟很重要的一个对象,如果bpmnModel对象不能深入的理解,那可能如果自己需要开发一套流程设计器,就显得力不从心,之前我们公司自己开发了一套acti ...

  3. JDBC-数据库的更新操作编程(三)

    首先建立一个静态方法,代码如下: public static Statement getStatement(){ Statement st = null; try { Class.forName(&q ...

  4. windows下实现win32俄罗斯方块练手,编程的几点心得

    编程珠玑2阅读笔记: 1.使用c语言性能监视器,完成对代码的调优工作 2.关联数组:  拓扑排序算法,可以用于当存在遮挡的时候决定三维场景的绘制顺序. 3.小型算法中的测试与调试工具 脚手架程序:&l ...

  5. parcel和parcelable

    Parcel 在英文中有两个意思,其一是名词,为包裹,小包的意思: 其二为动词,意为打包,扎包.邮寄快递中的包裹也用的是这个词.Android采用这个词来表示封装消息数据.这个是通过IBinder通信 ...

  6. MyBatis Generator For Eclipse 插件安装

    由于在ORM框架MyBatis中,实现数据表于JavaBean映射时,配置的代码比较的复杂,所以为了加快开发的效率,MyBatis官方提供了一个Eclipse的插件, 我izuoyongjiushis ...

  7. (一一四)使用FMDB操作SQLite数据库

    上节介绍了用系统自带的C语言库操作SQLite的方法,比较繁琐,使用FMDB会大幅度简化,并且是面向对象的,使用十分方便. 使用步骤如下: 先从github下载FMDB框架,然后把它导入工程. ①导入 ...

  8. Mybatis简单入门

    MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以使用简单的XML或注解用 ...

  9. 数据库隔离级别(mysql+Spring)与性能分析

     数据库隔离级别与Spring配置事务的联系及性能影响,以下是个人理解,如果有瑕疵请及时指正.   这里以mysql为例,先明确以下几个问题: 一.一般项目如果不自己配置事务的话,一般默认的是au ...

  10. FND Debug Log(FND_LOG_MESSAGES)

    之前每个模块记录日志的方式都不同,都会把日志写到不同的文件中,对于User来说很麻烦,需要记住很多的配置,现在越来越多的模块使用FND Logging来存储日志,比如WIP,RCV,OAF...FND ...