《LeetBook》leetcode题解(6): ZigZag Conversion[E]
我现在在做一个叫《leetbook》的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看
书的地址:https://hk029.gitbooks.io/leetbook/
006.ZigZag Conversion[E]
题目
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)
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”.
思路1——用字符串数组
我能说我一开始完全没看懂吗?我是根据Custom Testcase自己慢慢测试摸索出来的。
其实,应该是这样的
2行:
A _ C _ E _
_ B _ D _ F
3行:
A _ _ _ E _ _ _ I _ _ _
_ B _ D _ F _ H _ J _
_ _ C _ _ _ G _ _ _ K
所以有个简单的思路:
- 每行弄个string。
- 对原始字符串进行扫描,从上往下,从下往上,依次加入每行的string
- 最后把所有的string拼接起来
class Solution {
public:
string convert(string s, int numRows) {
string str[numRows],tmp;
if(numRows == 1)
return s;
int flag;
for(int i = 0,j = 0;i < s.length(); i++)
{
if(j == 0)
flag = 1;
if(j == numRows-1)
flag = -1;
str[j] += s[i];
j += flag;
}
for(int i = 0; i < numRows;i++){
tmp += str[i];
}
return tmp;
}
};
思路2——观察规律
2行:
A _ C _ E _
_ B _ D _ F
3行:
A _ _ _ E _ _ _ I _ _ _
_ B _ D _ F _ H _ J _
_ _ C_ _ _ G _ _ _ K
4行:
A _ _ _ _ _ G _ _ _ _ _
_ B _ _ _ F _ H _ _ _
_ _ C _ E _ _ _ I _ K
_ _ _ D _ _ _ _ _ J
观察规律后,以每行的元素作为轴,可以发现,下面的字母都是对称排列的
换成对应的index后,规律更明显
0 _ _ _ 4 _ _ _ 8 _ _ _
_ 1 _ 3 _ 5 _ 7 _ 9 _
_ _ 2 _ _ _ 6 _ _ _ 10
第2层的元素就是以第一行的元素为轴,+1,-1
第三层的元素就是以第一行的元素为轴,+2,-2
……
但是最后一层的元素,由于其特殊性,我们可以只考虑+k
Ps.所有过界的元素都不考虑
轴也有规律:除了首尾两层,其他都是2个,所以第一层每隔2n-2出现一次。
class Solution {
public:
string convert(string s, int numRows)
{
string tmp;
if(numRows == 1)
return s;
int inc = 2*numRows-2; //每次轴增加的步长
int len = s.length();
for(int i = 0; i < numRows;i++)
{
for(int j = 0;j < s.length()+numRows; j += inc)
{
if(j - i > 0 && j - i < s.length()
&& i != 0 && i != numRows -1) //首,尾只考虑+不考虑-
tmp+= s[j-i];
if(j + i < s.length())
tmp += s[j+i];
}
}
return tmp;
}
};
《LeetBook》leetcode题解(6): ZigZag Conversion[E]的更多相关文章
- leetcode题解 6.ZigZag Conversion
6.ZigZag Conversion 题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a gi ...
- 【LeetCode】6. ZigZag Conversion Z 字形变换
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字形变换,ZigZag,题解,Leetcode, 力扣,P ...
- 【一天一道LeetCode】#6 ZigZag Conversion
一天一道LeetCode系列 (一)题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given ...
- 【LeetCode】006. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【LeetCode】6 - ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- leetcode problem 6 ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode OJ:ZigZag Conversion(字符串的Z字型转换)
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode(6) - ZigZag Conversion
这个题的要求是给你一个字符串,和一个行数,例如(s = "mysisteristhemostlovelygirl" , row = 4),每一行一个字符串,但是s却得按照zigza ...
- 【LeetCode】6. ZigZag Conversion 锯齿形转换
题目: 思路: 以图为例:s={'A','B','C','D','E','F','G','H'.....} 1.先不考虑中间元素F.G.H.N...,每一行前后元素在数组中对应下标相差size=2*n ...
随机推荐
- md1
a b link to baidu 我不懂啊 这是什么意思 http://example.com/ 这里是一段代码' adsfasdf 你 你输得算好了 反正就这样吧,大概也值能这样了注 我想写一段引 ...
- ZooKeeper 一致性协议 ZAB 原理
一致性协议有很多种,比如 Paxos,Raft,2PC,3PC等等,今天我们讲一种协议,ZAB 协议,该协议应该是所有一致性协议中生产环境中应用最多的了.为什么呢?因为他是为 Zookeeper 设计 ...
- How to extract msu/msp/msi/exe files from the command line
http://www.windowswiki.info/2009/02/19/how-to-extract-msumspmsiexe-files-from-the-command-line/ Micr ...
- [Erlang27]如何监控指定目录下的*.beam文件,如果有改动就更新到指定的节点?
在Erlang In Anger第二章中讲到使用rebar来创建一个Erlang项目(Application或Project) 但美中不足的只是给出了指引,但没有给出详细的步骤. 下面我们就使用reb ...
- 网页方式访问 QQ 小说书架
iPad.平板电脑 http://bookshelf.html5.qq.com/page?t=pad#!/bookshelf/ iPhone.手机 http://bookshelf.html5.qq. ...
- 【cocos2d-x 手游研发----博彩大转盘】
博彩大转盘,转盘抽奖的小系统,这是一个很有意思的游戏模块,游戏中增加这样一些趣味的小模块,我会附上源码: 会增进玩家的粘性,每天都想来抽两把试试手气: 我做的这个是个矩形风格的转盘,不是那种圆形的转盘 ...
- centos7 二进制安装mysql5.6
下载mysqltar包 wget -q http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.g ...
- django系列6--Ajax05 请求头ContentType, 使用Ajax上传文件
一.请求头ContentType ContentType指的是请求体的编码类型,常见的类型共有三种: 1.application/x-www-form-urlencoded 这应该是最常见的 POST ...
- python网络编程--管道,信号量,Event,进程池,回调函数
1.管道 加锁可以保证多个进程修改同一块数据时,同一时间只能有一个任务可以进行任务修改,即串行修改,速度慢了,但牺牲了速度却保证了数据安全. 文件共享数据实现进程间的通信,但问题是: 1.效率低(共享 ...
- 百度地图API鼠标获取坐标
var map = new BMap.Map('map'); var poi = new BMap.Point(112.53, 37.87); map.enableScrollWheelZoom(); ...