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. 【2014 Multi-University Training Contest 2 1002】/【HDU 4873】 ZCC Loves Intersection

    果然,或滥用零件,啥都不说了.我们欣慰地学习阅读.这两天残疾儿童是数学. 这是求所需的问题,不明确.贴上官方的解题报告. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi ...

  2. 高性能mysql主存架构

    原文:高性能mysql主存架构 MySQL Replication(Master与Slave基本原理及配置) 主从mysql工作原理: 1:过程: (1)Mysql的复制(replication)是一 ...

  3. oracle 10g操作和维护手册

    1.    检查数据库基本状况... 1.1.     检查Oracle实例状态... 1.2.     检查Oracle服务进程... 1.3.     检查Oracle监听状态... 2.    ...

  4. 用命令行在github新建一个项目

    Github Repository API中说明了可以通过发送一个请求来认证,之后就能通过命令行自动新建远程仓库了. 认证 curl -u 'username' https://api.github. ...

  5. git寻根——^和~的区别(转)

    一. 引子 在git操作中,我们可以使用checkout命令检出某个状态下文件,也可以使用reset命令重置到某个状态,这里所说的“某个状态”其实对应的就是一个提交(commit). 我们可以把一个g ...

  6. SendRedirect和forward差分

    (1)重定向JSP实现JSP/Servlet跳转到目标资源的方法中,基本的想法是:server目标资源完成URL通过HTTP 在回答本报发client浏览器.收到的浏览器URL更新到地址栏后,而目标资 ...

  7. Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用

    原文:Eval().XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用 “/vs2005”应用程序中的服务器错误.--------------------------- ...

  8. URAL 1404. Easy to Hack! (模拟)

    space=1&num=1404">1404. Easy to Hack! Time limit: 1.0 second Memory limit: 64 MB When Vi ...

  9. 【Leetcode】Sort List (Sorting)

    这个问题需要与归并排序排两个名单,基本思路分为切割与合并 合并后的代码Merge Two Sorted List里已经讲得非常清楚了. 所以这里直接给出代码. public ListNode merg ...

  10. Java数据结构与算法(4) - ch04队列(Queue和PriorityQ)

    队列: 先进先出(FIFO). 优先级队列: 在优先级队列中,数据项按照关键字的值有序,关键字最小的数据项总在对头,数据项插入的时候会按照顺序插入到合适的位置以确保队列的顺序,从后往前将小于插入项的数 ...