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. ggplot2 legend图例的修改

    ggplot2中的legend包括四个部分: legend.tittle, legend.text, legend.key, legend.backgroud.针对每一部分有四种处理方式: eleme ...

  2. 加载xib文件

    // Test.xib --编译--> Test.nib // 方式1 NSArray *objs = [[NSBundle mainBundle] loadNibNamed:@"Te ...

  3. Zookeeper相关知识

    一.Zookeeper是什么? Zookeeper 分布式服务框架是 Apache Hadoop 的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务.状态同步服务. ...

  4. 在Windows上安装MySQL5.7

    1. 下载安装包,这里选择压缩版mysql-5.7.16-winx64.zip: http://dev.mysql.com/downloads/mysql/ 2. 解压到安装目录,注意最好不要含有中文 ...

  5. [http] http缓存机制

    原文链接:http://my.oschina.net/leejun2005/blog/369148 1.缓存的分类 缓存分为服务端侧(server side,比如 Nginx.Apache)和客户端侧 ...

  6. Linux上安装Mysql后除了本机其他机器不能访问的问题(zhuan)

    http://blog.sina.com.cn/s/blog_a338027c0101esbs.html http://niutuku.com/tech/Mysql/237638.shtml http ...

  7. 在Windows和Linux上安装paramiko模块

    一.paramiko模块有什么用? paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接.由于使用的是python这样的能够跨平台运行的语言 ...

  8. java中在linux下利用jstack检测死锁

    首先,编写一个死锁程序 package deadlock; public class testJstack { final static Object resource_1 = new Object( ...

  9. 在linux(CentOS-6.7_x86_64)上安装mysql成功记录

    查看linux服务器的yum源设置: [root@hadoop03 yum.repos.d]# cd /etc/yum.repos.d [root@hadoop03 yum.repos.d]# ll ...

  10. Hbase之进行批处理操作

    import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; impo ...