Problem:

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

就是将一个字符串按照“之”字型表示后,按每行组合后输出新字符串。

在白头翁的博客里学习到了O(n)的算法。

就是创建nRows个string,将s对应坐标拷贝到每个string中,利用pushback操作。最后append连接输出。代码如下:

class Solution {
public:
string convert(string s, int nRows)
{
if (nRows <= || s.length() < )
return s; string *s_arr = new string[nRows];
int nCount = * (nRows - ); // 每个循环的轮回 也就是 2倍的nRows - 2 for (int i = ; i < s.length(); ++i)
{
s_arr[nRows - -abs(nRows - - (i%nCount))].push_back(s[i]);
} string str_result;
for (int i = ; i < nRows; ++i)
str_result.append(s_arr[i]); return str_result;
}
};

坐标对应是关键,数学逻辑思维确实要强。可以举个四行的例子试试。

leetcode第六题--ZigZag Conversion的更多相关文章

  1. leetcode第六题 ZigZag Conversion (java)

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

  2. LeetCode第六题—— ZigZag Conversion(字符串的“之”字形转换)

    题目描述: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...

  3. 【leetcode❤python】 6. ZigZag Conversion

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

  4. LeetCode之“字符串”:ZigZag Conversion

    题目链接 题目要求: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ...

  5. LeetCode(6) ZigZag Conversion

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

  6. LeetCode(6)ZigZag Conversion

    题目如下: C++代码: #include <iostream> #include <string> using namespace std; class Solution { ...

  7. Leetcode 6. ZigZag Conversion(找规律,水题)

    6. ZigZag Conversion Medium The string "PAYPALISHIRING" is written in a zigzag pattern on ...

  8. 6.[leetcode] ZigZag Conversion

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

  9. 乘风破浪:LeetCode真题_006_ZigZag Conversion

    乘风破浪:LeetCode真题_006_ZigZag Conversion 一.前言 到这里我们对基本的问题有了一定的理解,其中字符串的操作一直是一个比较困难的问题,这一点我们需要认真对待,采用合理的 ...

随机推荐

  1. Docker简明教程(转)

    Docker自从诞生以来就一直备受追捧,学习Docker是一件很炫酷.很有意思的事情.我希望通过这篇文章能够让大家快速地入门Docker,并有一些学习成果来激发自己的学习兴趣.我也只是一个在Docke ...

  2. C代码分析器(一个 公开赛冠军)

    最近心血来潮,我希望能写一个通用的代码分析工具(其实这个词有点太.事实上为C代码).看到这几天我看到代码头晕眼花,尽管Source Insight救命,仍然没有足够的智慧思考很多地方. 如今主要遇到的 ...

  3. Codeforces 439C Devu and Partitioning of the Array(模拟)

    题目链接:Codeforces 439C Devu and Partitioning of the Array 题目大意:给出n个数,要分成k份,每份有若干个数,可是仅仅须要关注该份的和为奇数还是偶数 ...

  4. chrome扩展第三方浏览器下载安装

    1.使用其他浏览器打开谷歌应用市场,复制扩展详情地址url 2. 粘贴到:http://chrome-extension-downloader.com/中进行扩展的下载. 3. 拖拽到chrome e ...

  5. Oracle常见操作汇总(转)

    前言:Oracle学习也有十几天了,但是呢,接下来还要学习许多其他的东西,并不能提步不前,所以在此总结了以下Oracle中常用的命令和语句,没有语法都是实例,以便以后工作的时候随时翻看,毕竟是自己的东 ...

  6. java自动转型

    /*2015-10-30*/ public class TypeAutoConvert { public static void main(String[] args) { int a = 5; Sy ...

  7. 小工具:内存监视器(SystemMonitor)

    卸了360之后,与之捆绑的加速球也没了.加速球可以查看剩余内存量,清理残留进程,有的时候不觉得这小玩意有多大作用,卸了之后才知道后悔. 加速球的替代方案比比皆是,如Windows自带的任务管理器,窗口 ...

  8. ext panel 它们的定义图像刷新

    从管理发展的近期回报.事实上,它采取了一些努力,以适应,应对来自另一个角度的问题只.外观似良好的效果.阿土,项目用到了EXT js.百度大神里面没找到一个合适的图片组件.自己写了个能够刷新的图片组件. ...

  9. HDU 5281 Senior's Gun (贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5281 贪心题目,但是看看我的博客里边相关贪心的题解实在是少的可怜,这里就写出来供大家一起探讨. 题意还 ...

  10. 网络资源(10) - Eclipse开发项目

    2014_08_26 http://www.360doc.com/content/13/0403/14/11843499_275730704.shtml Eclipse 下搭建struts2开发环境