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

Summary: just finds the index in string s of every rows.

     string convert(string s, int nRows) {
string output;
if(nRows <= )
return output;
if(nRows == || nRows == s.size())
return s; for(int i = ; i < nRows; i ++) {
if(i == || i + == nRows){
int j = i;
while(j < s.size()){
output += s[j];
j += nRows + nRows - ;
}
}else{
int j = + i;
while(j < s.size()){
output += s[j];
int next_idx = j + nRows - (i + ) + nRows - (i + );
if(next_idx < s.size())
output += s[next_idx];
j += nRows + nRows - ;
}
}
}
return output;
}

ZigZag Conversion [LeetCode]的更多相关文章

  1. 6. ZigZag Conversion - LeetCode

    Question 6. ZigZag Conversion Solution 题目大意:将字符串按Z字型排列,然后再一行一行按字符输出 思路:按题目中的第一个例子,画出如下图,通过n的不同值,可以找出 ...

  2. ZigZag Conversion leetcode java

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

  3. 6.[leetcode] ZigZag Conversion

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

  4. LeetCode解题报告—— 2 Keys Keyboard & Longest Palindromic Substring & ZigZag Conversion

    1. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...

  5. LeetCode ZigZag Conversion(将字符串排成z字型)

    class Solution { public: string convert(string s, int nRows) { string a=""; int len=s.leng ...

  6. 【leetcode❤python】 6. ZigZag Conversion

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

  7. leetcode第六题 ZigZag Conversion (java)

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

  8. leetcode题解 6.ZigZag Conversion

    6.ZigZag Conversion 题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a gi ...

  9. LeetCode 6. ZigZag Conversion & 字符串

    ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...

随机推荐

  1. GZFramwork快速开发框架演练之会员系统(一)框架源码下载

    GZFramwork框架开发环境为.NET 4.0 (必须)   VS2013+SQL2005+DevExpress v13.2.8+FastReport (推荐)  数据库建模工具PowerDesi ...

  2. 【转载】COM的多线程模型

    原文:COM的多线程模型 COM的多线程模型是COM技术里头最难以理解的部分之一,很多书都有涉及但是都没有很好的讲清楚.很多新人都会在这里觉得很迷惑,google大神能搜到一篇vckbase上的文章, ...

  3. about_Execution_Policies

    https://technet.microsoft.com/en-us/library/hh847748.aspx?f=255&MSPPError=-2147217396 在powershel ...

  4. linux bash shell 流程控制(if/else )

    本文转自:http://blog.csdn.net/flowingflying/article/details/5069646 本文也即<Learning the bash Shell>3 ...

  5. 先来个xmpp学习连接

    http://my.oschina.net/SoulJa/blog?catalog=3340253&temp=1468228088114 http://my.oschina.net/iOSli ...

  6. FJNU 1157 Fat Brother’s ruozhi magic(胖哥的弱智术)

    FJNU 1157 Fat Brother’s ruozhi magic(胖哥的弱智术) Time Limit: 1000MS   Memory Limit: 257792K [Description ...

  7. SQL——存储过程实例 循环

    --循环 create or replace procedure p_xunhuan(input in number,output out number) is ); begin ; ..input ...

  8. Redis 在新浪微博中的应用

    Redis 在新浪微博中的应用 Redis简介 1. 支持5种数据结构 支持strings, hashes, lists, sets, sorted setsstring是很好的存储方式,用来做计数存 ...

  9. Python基础学习笔记(十一)函数、模块与包

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-functions.html 3. http://www.liao ...

  10. 关于gitlab的一个问题解决

    这两天折腾一个关于gitlab的问题,搞得比较焦头烂额.不过经过折腾,最终还是成功了. 当面对着一个问题,并且看到还没被解决的时候,感觉很不舒服: 努力折腾之后,解决之后,也会身体轻松.或许工程师天生 ...