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

thinking:

(1)读懂题意,把字符串依照"Z"形又一次排列,再按行链接起来输出。

(2)没有解体思路,能够列举几个简单的样例寻找规律。

一: 2排的时候,1到n的排序

1 3 5 7 9  。

。。

2 4 6 8 10 。。。

二: 3排的时候。1到n的排序

1    5    9  。

。。

2 4 6 8 10 。。。

3   7    11 。。。



三: 4排的时候,1到n的排序

1    7       13 。

。。

2  6 8    12 14 。。。

3 5  9  11   15 。

。。

4    10        16 。。





这到规律没有?如果没有找到, 你能够继续写5排的情况。非常快你就能够找到规律。

这是一个解决这个问题问题的方法。

当我们遇到难缠的问题的时候,我们先考虑简单的情形,看看能不能找到规律。这个题目。我们通过写出来这些特殊情况,我们发现例如以下规律,这里我们如果我们分成m排:

1 第i排从i開始

2 第i排两个数的间隔是2(i-1),2(m-i)交替

code:

class Solution {
public:
string convert(string s, int nRows) {
int length = s.size(); if((nRows==1)||(nRows>=length))
return s;
string result;
for(int i=0;i<nRows;i++)
{
bool flag=true;
int j=i;
while(j<length)
{ result.push_back(s.at(j));
if((i==0)||(i==nRows-1))
j+=2*(nRows-1);
else
{
if(flag)
{
j+=2*(nRows-i-1);
flag=false;
}
else
{
j+=2*i;
flag=true;
} }//else
}//while
}//for
return result;
}//convert
};

leetcode题解||ZigZag Conversion问题的更多相关文章

  1. [LeetCode 题解]: ZigZag Conversion

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 The string ...

  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. Mac OSX Yosemite 10.10 brew 错误:mktemp: mkdtemp failed on /tmp/git-LIPo: No such file or directory

    这个问题困扰了我非常久非常久.使得我不得不花一点时间来说一下解决方法. 事情是这种:前两天兴高採烈的更新了一下宝贝mac到10.10. 一切看起来都那么美好,可是. .当我又一次安装magento的时 ...

  2. wpf获取目录路径

    AppDomain.CurrentDomain.BaseDirectory +文件名即可,简单吧? //获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称. string str5=Appl ...

  3. JQuery 登录窗口的布局

    <!-- Button trigger modal --><button type="button" class="btn btn-primary bt ...

  4. PHP邮件发送库:Swiftmailer

    Swiftmailer需要PHP 7.0或更高版本,(proc_*函数可用.) 安装 composer require "swiftmailer/swiftmailer:^6.0" ...

  5. 2017.7.15清北夏令营精英班Day1解题报告

    成绩: 预计分数:20+10+40 实际分数:100+10+40. 一百三十多人的比赛全场rand7还水了个鼠标+键盘 unbelievable! 考试题目链接: https://www.luogu. ...

  6. python小项目之头像右上角加数字

    pillow介绍 一.Image类的属性:1.Format   2.Mode   3.Size    4.Palette    5.Info 二.类的函数:1.New   2.Open   3.Ble ...

  7. win32应用禁止改变窗口大小方法

    一种简单的处理方法是在调用CreateWindow函数时指定的窗口样式中去掉WS_THICKFRAME样式. 如果你使用的样式中已经包含该样式,例如WS_OVERLAPPEDWINDOW,我们可以將W ...

  8. 用Javascript做一个“获取验证码”的按钮

    要求:①点击按钮后背景色会发生改变②有倒计时(一般为30秒) <!DOCTYPE HTML> <html> <head> <meta charset=&quo ...

  9. poj1050查找最大子矩阵和

    题目: To the Max   Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 48507   Accepted: 2566 ...

  10. spring的JdbcTemplate

    一.首先配置JdbcTemplate: 要使用Jdbctemplate 对象来完成jdbc 操作.通常情况下,有三种种方式得到JdbcTemplate 对象.       第一种方式:我们可以在自己定 ...