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. ASP.Net中上传文件的几种方法

    在做Web项目时,上传文件是经常会碰到的需求.ASP.Net的WebForm开发模式中,封装了FileUpload控件,可以方便的进行文件上传操作.但有时,你可能不希望使用ASP.Net中的服务器控件 ...

  2. STL源代码分析——STL算法merge合并算法

    前言 因为在前文的<STL算法剖析>中.源代码剖析许多.不方便学习.也不方便以后复习,这里把这些算法进行归类.对他们单独的源代码剖析进行解说.本文介绍的STL算法中的merge合并算法. ...

  3. cocos2d-x3.0 windows 环境配置

    cocos2d-x3.0 windows 环境配置 参考Oo泡泡糖oO的CSDN博文 :http://blog.csdn.net/u010296979/article/details/24273393 ...

  4. js控制文本框仅仅能输入中文、英文、数字与指定特殊符号

    JS 控制文本框仅仅能输入数字 <input onkeyup="value=value.replace(/[^0-9]/g,'')"onpaste="value=v ...

  5. NSIS:设置文件属性的方法

    原文 NSIS:设置文件属性的方法 在NSIS中,有以下方法可以对文件的属性进行设置.   一是通过NSIS的指令SetFileAttributes.   多重属性可用 ' | ' 隔开,有效的属性为 ...

  6. EnumMap demo

    优点:常量做为Key,在编译期就确定了.Enum做为key,在运行时也可以改变 package enumdemo; import java.util.EnumMap; import java.util ...

  7. ListView单选的实现总结(转)

    今天在智能停车场项目中需要实现PullToRefreshListView的单选功能,考虑到分页,刷新等,以前的实现方式是采用自己维护一个集合保存选中位置的选中状态,但这个方式比较繁琐,今天采用了lis ...

  8. log4j+logback+slf4j+commons-logging的关系与调试(转)

    背景     由于现在开源框架日益丰富,好多开源框架使用的日志组件不尽相同.存在着在一个项目中,不同的版本,不同的框架共存.导致日志输出异常混乱.虽然也不至于对系统造成致命伤害,但是明显可以看出,架构 ...

  9. AngularJS 课程

    AngularJS 教程(点我) AngularJS 通过新的属性和表达式扩展了 HTML. AngularJS 能够构建一个单一页面应用程序(SPAs:Single Page Application ...

  10. hdu 1251(字典树)

    题目链接:http://acm.hdu.edu.cn/status.php?user=NYNU_WMH&pid=1251&status=5 Trie树的基本实现 字母树的插入(Inse ...