leetcode题解||ZigZag Conversion问题
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问题的更多相关文章
- [LeetCode 题解]: ZigZag Conversion
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 The string ...
- LeetCode题解——ZigZag Conversion
题目: 把一个字符串按照Z型排列后打印出来,例如 "PAYPALISHIRING" 重新排列后为3行,即 P A H N A P L S I I G Y I R 那么输出为&quo ...
- LeetCode 6. ZigZag Conversion & 字符串
ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...
- Leetcode 6. ZigZag Conversion(找规律,水题)
6. ZigZag Conversion Medium The string "PAYPALISHIRING" is written in a zigzag pattern on ...
- LeetCode 6 ZigZag Conversion 模拟 难度:0
https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in ...
- LeetCode 6 ZigZag Conversion(规律)
题目来源:https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is writt ...
- [LeetCode][Python]ZigZag Conversion
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/zigzag- ...
- LeetCode——6. ZigZag Conversion
一.题目链接:https://leetcode.com/problems/zigzag-conversion/description/ 二.题目大意: 给定一个字符串和一个数字,将其转换成Zigzag ...
- 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
随机推荐
- java删除文件夹及子目录
package test; import java.io.FileNotFoundException; import java.io.IOException; import java.io.File; ...
- Tomcat学习之ClassLoader
Tomcat学习之ClassLoader 2012-09-04 22:19 8993人阅读 评论(4) 收藏 举报 分类: WEB服务器(13) 版权声明:本文为博主原创文章,未经博主允许不得转载 ...
- 2015.04.21,外语,读书笔记-《Word Power Made Easy》 11 “如何辱骂敌人” SESSION 31
1.no reverence iconoclast([ai'kɔnәklæst] n. 毁坏宗教神像的人, 提倡打破旧习的人)藐视传统.在青年的反叛期很容易出现iconoclasm([ai'kɔnә ...
- UESTC--1269--ZhangYu Speech(模拟)
ZhangYu Speech Time Limit: 1000MS Memory Limit: 65535KB 64bit IO Format: %lld & %llu Submit ...
- poj--3159--Candies(简单差分约束)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 26888 Accepted: 7398 Descrip ...
- block的一些注意事项
1,定义block时是可以同时进行赋值的 2,block中是代码块,就是里面写的是语句,需要加分号 3,在block中,允许有多条语句 4,在带有参数的block中,声明部分参数名可以省略,但是建议写
- sql获得某个时间段的数据
CONVERT(Date, 时间字符串变量 ) between CONVERT(Date,'2015/2/10') and CONVERT(Date,'2015-3-10')
- Android中onActivityResult()获取返回值
需求:从FirstActivity跳到SecondActivity,在SecondActivity中进行了操作并返回到FirstActivity. FirstActivity中的主要代码: priva ...
- 发现个很变态的css问题,记录下。
注意看Ul:nth-child(){} 本来是一一对应的,因为后来改版在div中加了<h1></h1>标记后,竟然自动后移了.... 不可思议,莫明奇妙
- LeetCode(15)3Sum
题目如下: Python代码: def threeSum(self, nums): res = [] nums.sort() for i in xrange(len(nums)-2): if i &g ...