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 ...
随机推荐
- Linux系统编程——多线程实现多任务
概述 每一个进程都拥有自己的数据段.代码段和堆栈段.这就造成进程在进行创建.切换.撤销操作时,须要较大的系统开销. 为了降低系统开销,从进程中演化出了线程.为了让进程完毕一定的工作.进程必须至少包括一 ...
- Jmeter简单应用
JMeter 是Apache组织的开源项目,是一个纯Java桌面应用,用于压力测试和性能测量. 1.安装jmeter jdk1.6以上下载地址:http://www.oracle.com/techne ...
- OpenST Basic tool library
/***************************************************************************** * OpenST Basic tool l ...
- 遗传奥秘的伟大揭秘者:J.Watson
J.Watson的近照: 人们公认,揭秘生命体的遗传奥秘(DNA)是二十世纪最伟大的科技成果之中的一个,或许就是人类最伟大的科技进步(而不是"之中的一个"). 上世纪是人类做出伟大 ...
- mysql安装出错cannot create windows service for mysql.error:0
配置时最后一步出现不能启动mysql 解决成功的办法:[MySQL] Could not start the service MySQL 解决方法 安装mysql 5.1.33,在运行Server I ...
- 初学ToggleButton 点击button,更换button背景图片;再次点击,恢复之前背景图
上方的图标,R.drawable.register_checked 是选中图片 下方的图标, R.drawable.register_unchecked 是未选中图片 默认是上方的选中效果.点击 ...
- 多线程编程TSL相关的技术文档
线程本地存储 (TLS) https://msdn.microsoft.com/zh-cn/library/6yh4a9k1(v=vs.80).aspx Using Thread Local Stor ...
- poj3685(嵌套二分)
Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 4658 Accepted: 1189 Descriptio ...
- elasticsearch源码分析之search模块(server端)
elasticsearch源码分析之search模块(server端) 继续接着上一篇的来说啊,当client端将search的请求发送到某一个node之后,剩下的事情就是server端来处理了,具体 ...
- NPAPI——实现非IE浏览器的类似ActiveX的本地程序(插件)调用
一.Netscape Plugin Interface(NPAPI) 大致的说明可以看下官方文档Plugin 本文主要针对于JavaScript与插件交互部分做一些交流,比如用于数字证书的操作(淘宝和 ...