题目链接

  题目要求:

  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. 【C++】处理CSDN博文源码

    为了简化CSDN写博客的字体问题,给出一段代码,用于处理使用默认格式写完博客后,处理一次来解决字体问题. 代码片段 代码片段如下所示: #include <iostream> #inclu ...

  2. [nginx] 对UA为空的请求返回403

    nginx blocking blank user agent . sometime apps' backgroud request always visit a url, and these req ...

  3. 获取imageView的图和背景图

    img1和img2都是ImageView,要把img1中的图片显示到img2中 前景(对应src属性) img2.setImageDrawable(img1.getDrawable()); 背景(对应 ...

  4. EBS采购(PO)模块常用表

     select * from po_requisition_headers_all 请求头 select * from po_requisition_lines_all 请求行 select * ...

  5. 那些年我们一起用过的Hybrid App

    Hybrid App现状分析 Web App 毫无疑问Web App就是成本最低,最快速地解决方案了.尤其是近两年非常流行的响应式设计,Web App市场提供了非常好的实践场地.最近典型的Web Ap ...

  6. Django 表单校验 表单字段设置 自定义表单校验规则

    今天看到了一篇非常好的博文,拿来和大家分享一下. 内容包括了: 用户注册时输入数据的校验 使用widget进行字段设置 实现自定义的校验规则 参考自下面的这篇文章

  7. 创建银行分行的API

    DECLARE p_api_version NUMBER := 1.0; p_init_msg_list VARCHAR2(1) := 'F'; v_bank_id NUMBER := 530705; ...

  8. UNIX网络编程——客户/服务器心搏函数

    阅读此博客时,可以参考以前的博客<<UNIX网络编程--socket的keep-alive>>和<<UNIX网络编程--套接字选项(心跳检测.绑定地址复用)> ...

  9. window.open 打开子窗口,关闭所有的子窗口

    需求:通过window.open方法打开了子窗口,当关闭主窗口时,子窗口应当也关闭. 实现思路: 1.打开子窗口函数window.open(url,winName)的第二个参数winName可以唯一标 ...

  10. debian 安装jdk

    JDK下载http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-javase6- ...