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. C#使用ServiceController控制windows服务

    C#在,使用ServiceController控制类windows服务,添加首次使用前引文:System.ServiceProcess,空间中引用:using System.ServiceProces ...

  2. Doug Lea

    如果IT的历史,是以人为主体串接起来的话,那么肯定少不了Doug Lea.这个鼻梁挂着眼镜,留着德王威廉二世的胡子,脸上永远挂着谦逊腼腆笑容,服务于纽约州立大学Oswego分校计算机科学系的老大爷. ...

  3. 关于 pthread_cond_wait 和 pthread_cond_signal , signal 无效的问题

    关于一个消费者模式,,,引起的问题.. 我在io线程里不断的把一个函数调用放到队列里 然后ruby线程就不断的从这个队列里取出函数之争并运行. 典型的 消费者模式. 我曾经以为是这样... 这是wor ...

  4. unity3d 依据指定的Assets下的目录路径 返回这个路径下的全部文件名称

    using UnityEngine; using System.Collections; using System.Collections.Generic; using System.IO; < ...

  5. Java8 Lambda表达应用 -- 单线程游戏server+异步数据库操作

    前段时间我们游戏server升级到开发环境Java8,这些天,我再次server的线程模型再次设计了一下,耗费Lambda表情. LambdaJava代码.特别是丑陋不堪的匿名内部类,这篇文章主要就是 ...

  6. JNDI数据源配置注意事项

    假设是在原有project上改动 1,applicationContext.xml中改动当中的value值 <bean id="dataSource" class=" ...

  7. nefu117 素数个数的位数,素数定理

    素数个数的位数 Time Limit 1000ms Memory Limit 65536K description 小明是一个聪明的孩子,对数论有着非常浓烈的兴趣.他发现求1到正整数10n 之间有多少 ...

  8. 批处理命令 For循环命令具体解释!

    批处理for命令具体解释FOR这条命令基本上都被用来处理文本,但还有其它一些好用的功能!看看他的基本格式(这里我引用的是批处理中的格式,直接在命令行仅仅须要一个%号)FOR 參数 %%变量名 IN ( ...

  9. oracle当需要commit

    今天oracle的SQL plus 运行该删除和查询操作,然后PL/SQL还运行查询.相同的句子,结果是不一样的.我郁闷很大,然后,突然想到这可能对双方造成由数据不一致,为什么不一致呢.就是没用com ...

  10. 编程算法 - 求1+2+...+n(函数指针) 代码(C++)

    求1+2+...+n(函数指针) 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 求1+2+...+n, 要求不能使用乘除法\for\whi ...