前言

 

【LeetCode 题解】系列传送门:  http://www.cnblogs.com/double-win/category/573499.html

 

1.题目描述

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

 

2. 相关题目

ZigZag again:

在本人之前的博客中,曾列出过另一个ZigZag数组的例子http://www.cnblogs.com/double-win/p/3641657.html

与上一篇不同的是,本题对于ZigZag的规则则有所不同。

图一

图二

3. 思路

如果我们将数组分成矩形框中的部分,那么可以看到有如下的规律:

(1)在矩形框图一中的子结构中,设行号为i (i=1~nRows)每次都是行号先增加,后减小: 在矩形框中的数据的个数为 :A = 2*(nRows-1).

       1) 若用 string  temp[nRows]来存储每一行的数据

2) 子结构可以分成两个部分, 一个是行号从1~nRows, 另一个是行号从nRow-1~2

(2)矩形框之外的结构的数据个数B为: s.size()% A.

       1) 若 B<nRows, 那么多出来的字符只能依次填多出来的一列;如图二中的 NG

       2) 若 B >nRows, 那么必然可以将某一列都填满,多出来的数据有B-=nRows, 行号的变化范围为 nRow-1 : nRow-B

4. 解法

 1 class Solution {
2 public:
3 string convert(string s,int nRows)
4 {
5 if(nRows==1) return s;
6 int i=0,j=0,Count=0;
7 string a[nRows],b;
8 for(i=0;i<nRows;i++)
9 a[i].clear();
10 b.clear();
11 int time = s.size()/2/(nRows-1); //完整子结构出现的次数
12 int remain = s.size()%(2*(nRows-1)); // 剩余不足一个子结构的数据长度
13
14 while(time--)
15 {
16 for(i=0;i<nRows;i++)
17 a[i]+=s[Count++];
18 for(i=nRows-2;i>=1;i--)
19 a[i]+=s[Count++];
20 }
21
22 if(remain<=nRows) // 剩余的数据不足nRows
23 {
24 for(i=0;i<remain;i++)
25 a[i]+=s[Count++];
26 }
27 else //剩余的数据在nRows~ 2*(nRow-1)-1 之间
28 {
29 for(i=0;i<nRows;i++)
30 a[i]+=s[Count++];
31 remain-=nRows;
32 i=nRows-2;
33 for(j=0;j<remain;j++)
34 a[i--] += s[Count++];
35 }
36 for(i=0;i<nRows;i++)
37 b+=a[i];
38 return b;
39 }
40 };

 

作者:Double_Win

出处:   http://www.cnblogs.com/double-win/p/3687749.html

声明: 由于本人水平有限,文章在表述和代码方面如有不妥之处,欢迎批评指正~

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

  1. leetcode题解||ZigZag Conversion问题

    problem: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of r ...

  2. LeetCode题解——ZigZag Conversion

    题目: 把一个字符串按照Z型排列后打印出来,例如 "PAYPALISHIRING" 重新排列后为3行,即 P A H N A P L S I I G Y I R 那么输出为&quo ...

  3. LeetCode 6. ZigZag Conversion & 字符串

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

  4. Leetcode 6. ZigZag Conversion(找规律,水题)

    6. ZigZag Conversion Medium The string "PAYPALISHIRING" is written in a zigzag pattern on ...

  5. LeetCode 6 ZigZag Conversion 模拟 难度:0

    https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in ...

  6. LeetCode 6 ZigZag Conversion(规律)

    题目来源:https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is writt ...

  7. [LeetCode][Python]ZigZag Conversion

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/zigzag- ...

  8. LeetCode——6. ZigZag Conversion

    一.题目链接:https://leetcode.com/problems/zigzag-conversion/description/ 二.题目大意: 给定一个字符串和一个数字,将其转换成Zigzag ...

  9. 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]

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

随机推荐

  1. PHP5缓存插件

    1.1操作码介绍及缓存原理 当客户端请求一个PHP程序时,服务器的PHP引擎会解析到该PHP程序,并将其变异为特定的操作码文件(OperateCode opcode),这是要执行的PHP代码的一种二进 ...

  2. [Z]用subcaption包排版子图(表)与图(表)格式设置

    很不错的一篇文章,可以进一步参考caption和subcaption的文档: http://www.peteryu.ca/tutorials/publishing/latex_captions

  3. RHCE7 学习里程-1.配置IP,DNS

    一.安装系统完成 1.系统安装完成之后不同于 6 的 ifconfig 命令.7 使用ip add ,这个跟网络设备配置端口IP 有点类似. 使用  ip add  查看网卡编号 cd  /etc/s ...

  4. JAVA_01

    Java局部变量 局部变量声明在方法.构造方法或者语句块中: 局部变量在方法.构造方法.或者语句块被执行的时候创建,当它们执行完成后,变量将会被销毁: 访问修饰符不能用于局部变量: 局部变量只在声明它 ...

  5. 什么是web资源????

    所谓 web 资源即放在 Internet 网上供外界访问的文件或程序,又根据它们呈现的效果及原理不同,将它们划分为静态资源和动态资源. 1. 什么是静态资源 静态资源是浏览器能够直接打开的,一个 j ...

  6. 从零玩转JavaWeb系列7web服务器-----get与post的区别

    总结get与post的区别 get参数通过url传递,post放在request body中. get请求在url中传递的参数是有长度限制的,而post没有. get比post更不安全,因为参数直接暴 ...

  7. 消息队列—ActiveMQ

    1.   学习计划 1.什么是MQ 2.MQ的应用场景 3.ActiveMQ的使用方法. 4.使用消息队列实现商品同步. 2.   同步索引库分析 方案一:在manager(后台)中,添加商品的业务逻 ...

  8. js是函数式的面向对象编程语言

    js是函数式的面向对象编程语言,而非类式的面向对象编程语言

  9. Asp.net 使用Neatupload 第三方控件上传大文件,在IIS7上无法正常工作解决

    使用环境:Window Server2008 + IIS7 更改web.config配置 1.在<configSections></configSections>节内加入: & ...

  10. 【BZOJ 2120】数颜色【分块/莫队】

    题意 给出n个数字和m个操作.操作有两种.1:查询区间[l,r]内不同种类得数字个数.2: 将下标为p得数字修改为v 分析 如果不是修改操作的话,用莫队贼简单就可以水过,但是因为带了修改就有一些麻烦了 ...