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. [linux]scp指令(转)

    Linux scp命令用于Linux之间复制文件和目录,具体如何使用这里好好介绍一下,从本地复制到远程.从远程复制到本地是两种使用方式.这里有具体举例: ================== Linu ...

  2. 准确率和召回率(precision&amp;recall)

    在机器学习.推荐系统.信息检索.自然语言处理.多媒体视觉等领域,常常会用到准确率(precision).召回率(recall).F-measure.F1-score 来评价算法的准确性. 一.准确率和 ...

  3. k8s google sample - guestbook

    Redis读写分离作为存储 PHP网页作为前端 github地址 https://github.com/kubernetes/kubernetes/blob/release-1.1/examples/ ...

  4. Testin一日游实验室发布的行级APP质量报告:在那里拍携程双赢

    Testin实验室公布国庆出行旅途类APP质量报告:携程力压去哪儿夺冠 2014/09/28 · Testin · 实验室报告 一年一度的十一黄金周即将临近,旅游软件成为每外出行人手机必装软件.为此全 ...

  5. 開始开发 Dashboard Widgets,第2章,读书笔记

    文件夹:http://blog.csdn.net/wide288/article/details/40298693 主要内容: widgets 的组成是什么. 怎么创建 info.plist 文件 怎 ...

  6. Jquery 分页插件 Jquery Pagination

    Jquery 分页插件 Jquery Pagination 分页插件来说,我觉得适用就行,尽量简单然后能够根据不同的应用场景能够换肤.展现形式等. 对于初学者想写分页插件的同学,也可以看下源码,代码也 ...

  7. zzu--2014年11月16日月潭赛 C称号

    1230: Magnets Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 24  Solved: 13 [id=1230" style=&q ...

  8. 彩色图像上执行Mean Shift迭代搜索目标 ,维加权直方图 + 巴氏系数 + Mean Shift迭代

    今天要给大家分享的是: 在彩色图像上进行Mean Shift迭代搜索目标 二维加权直方图+巴氏系数+Mean Shift迭代 关于 加权直方图.巴氏系数.Mean Shift迭代 这三者之间的关系请大 ...

  9. windows和linux在建筑python集成开发环境IDE

    http://blog.csdn.net/pipisorry/article/details/39854707 使用的系统及软件 Ubuntu / windows Python 2.7 / pytho ...

  10. 朴素UNIX之-打开历史

    它可以毫不夸张地说,,UNIX模型是现代操作系统的原型.无论是真实的UNIX让我们大系列AIX,Solaris,HP-UX,FreeBSD,NetBSD,...或类别UNIX实例Linux...或基于 ...