LeetCode之“字符串”:ZigZag Conversion
题目要求:
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的更多相关文章
- leetcode题解 6.ZigZag Conversion
6.ZigZag Conversion 题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a gi ...
- 《LeetBook》leetcode题解(6): ZigZag Conversion[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 字符串按照Z旋转90度然后上下翻转的字形按行输出字符串--ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode OJ:ZigZag Conversion(字符串的Z字型转换)
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【LeetCode】6. ZigZag Conversion Z 字形变换
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字形变换,ZigZag,题解,Leetcode, 力扣,P ...
- 【一天一道LeetCode】#6 ZigZag Conversion
一天一道LeetCode系列 (一)题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given ...
- 【LeetCode】6 - ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode(6) - ZigZag Conversion
这个题的要求是给你一个字符串,和一个行数,例如(s = "mysisteristhemostlovelygirl" , row = 4),每一行一个字符串,但是s却得按照zigza ...
- leetcode problem 6 ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【LeetCode】006. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
随机推荐
- ROS探索总结(十八)——重读tf
在之前的博客中,有讲解tf的相关内容,本篇博客重新整理了tf的介绍和学习内容,对tf的认识会更加系统. 1 tf简介 1.1 什么是tf tf是一个让用户随时间跟踪多个参考系的功能包,它使用一种树型数 ...
- emysql add_poop() 超时出错
emysql add_poop() 超时出错(金庆的专栏)sample/a_hello.erl 连接本机更改为连接局域网内的MySql服务器: emysql:add_pool(hello_poo ...
- Cocos2D中节点Z序的计算规则
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交 ...
- UE4读取本地XML文件
其实这里读取XML也是利用了Tinyxml来读取xml,主要是讲Tinyxml放在UE4中,遇到的一点点坑 1.先给出Tinyxml链接:http://www.grinninglizard.com/t ...
- 【一天一道LeetCode】#172. Factorial Trailing Zeroes
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- shell入门之函数应用
最近在学习shell编程,文中若有错误的地方还望各位批评指正. 先来看一个简单的求和函数 #!/bin/bash #a test about function f_sum 7 8 function f ...
- Xdoclet + Ant自动生成Hibernate配置文件
在使用Hibernate的时候,过多的Hibernate配置文件是一个让人头疼的问题.最近接触了Xdoclet这个工具.它实际上就是一个自动代码生成的工具,Xdoclet不能单独运行,必须搭配其他工具 ...
- Google会思考的深度学习系统
上周五在旧金山举行的机器学习会议上,Google软件工程师Quoc V. Le讲解了Google的"深度学习"系统是如何运作的. "深度学习"需要用到大型计算机 ...
- Android开发工具下载地址
Android Studio: http://zdz.la/iq4zSa
- Linux:ssh_config快速访问服务器
在当前用户的根目录下: cd ~/.ssh vi config 编辑config内容为下面: ForwardAgent yes Host 1 Hostname 192.168.1.1 User roo ...