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. ListView的adapter中getView方法一直调用

    当ListView的高度不定(比如重写ListView搞成可自己主动的扩展的ListView)或 ListView嵌套在SrollView(高度不定)中,listView中的一个item元素改变会使得 ...

  2. HDU 1874 畅通project续 (最短路径)

    畅通project续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. c2

    #include <stdio.h> int main() { // 整型常量 ; // 实型常量(小数) // 单精度float / 双精度double // 注意: 默认情况下编写的小 ...

  4. Linux下将PHP添加到环境变量,将Mysql加入环境变量。

    1.修改/etc/profile vi /etc/profile 2.添加两行 PATH=$PATH:/usr/local/php7/bin:/usr/local/mysql/bin export P ...

  5. ubuntu 使用阿里云 apt 源

    以下内容来自 https://opsx.alibaba.com/mirror Ubuntu对应的“帮助”信息 修改方式:打开 /et/apt/sources.list 将http://archive. ...

  6. svn插件失效

    安装其他插件后,可能出现SVN插件失效了,在eclipse中完全找不到SVN的任何操作选项,此时可尝试通过以下办法解决: 把eclipse/configuration目录下的org.eclipse.u ...

  7. BZOJ 4710 容斥原理+dp

    //By SiriusRen #include <cstdio> using namespace std; int n,m,a[1005]; typedef long long ll; l ...

  8. Android 多个APK共享数据

    Android给每个APK进程分配一个单独的用户空间,其manifest中的userid就是对应一个Linux用户(Android 系统是基于Linux)的.所以不同APK(用户)间互相访问数据默认是 ...

  9. 跨域-jsonp、cors、iframe、document.domain、postMessage()

    同源策略 概念:同源: 协议.域名.端口号 完全相同 同源策略是浏览器的一种安全策略:且浏览器不会将违反同源策略的响应信息返回 http://127.0.0.1:3000/index.html     ...

  10. javascript中document.getElementsByClassName兼容性封装方法一

    var getElmsByClsName = function(className, results) { results = results || []; // 判断浏览器是否支持 getEleme ...