您的位置:首页 > 其它

LeetCode 6 ZigZag Conversion

2016-06-08 00:00 501 查看
<span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 24px; line-height: 26.399999618530273px;">ZigZag Conversion</span>


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"

.

这道题目很有意思,把字符串按照锯齿形式展开,然后自上往下遍历。

在LeetCode上,这是一道容易的题目,但是我却花了好久,在OJ上做题的时候,一定要保证100%的全神贯注,否则会浪费大量时间。

我的思路:

这道题本质上来说,就是重新安排字符串的索引,使其能符合锯齿序列。

对于序列安排有两种情况,首行和末行需要特殊考虑。其他行的索引求法可以归纳出通项公式。

通过简单归纳可以知道,记num为要求的锯齿行数

首行和末行的索引相差 :2num-2。

中间的索引求法:(1)2*num-2*i, i为当前行数。 (2)2*i,i为当前行数。

循环的边界条件也很重要,就是字符串的长度,每一行在求索引的时候,都要判定长度是否超出给定的字符串长度。

代码如下:

public String convert(String s, int numRows) {
int n = 2*numRows-2;
int n = 2*numRows-2;
int len = s.length();
int []index = new int[len];
int col=0;
String re = new String();
<span style="white-space:pre">	</span>if(s.length()==0){
<span style="white-space:pre">		</span>return "";
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>if(numRows==1){
<span style="white-space:pre">		</span>return s;
<span style="white-space:pre">	</span>}
for (int i = 0; i < numRows; i++) {
<span style="white-space:pre">			</span>if(i==0||i==numRows-1){
<span style="white-space:pre">				</span>int k = i;
<span style="white-space:pre">			</span>while(k<len){
<span style="white-space:pre">				</span>index[col++]=k;
<span style="white-space:pre">				</span>k+=n;
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>}else{<span style="white-space:pre">	</span>//other /为偶数 2m-2i 为奇数 2i-m
<span style="white-space:pre">				</span>int k =i;
<span style="white-space:pre">				</span>int type =0;//作不同的加法
<span style="white-space:pre">				</span>int a = 2*(numRows-1)-2*i;
<span style="white-space:pre">				</span>int b = 2*i;
<span style="white-space:pre">				</span>while(k<len){
<span style="white-space:pre">					</span>if(type==0){
<span style="white-space:pre">						</span>index[col++]=k;
<span style="white-space:pre">						</span>k+=a;
<span style="white-space:pre">						</span>type=1;
<span style="white-space:pre">						</span>continue;
<span style="white-space:pre">					</span>}
<span style="white-space:pre">					</span>if(type==1){
<span style="white-space:pre">						</span>index[col++]=k;
<span style="white-space:pre">						</span>k+=b;
<span style="white-space:pre">						</span>type=0;
<span style="white-space:pre">					</span>}
<span style="white-space:pre">				</span>}}
<span style="white-space:pre">		</span>}
for (int i = 0; i < index.length; i++) {
<span style="white-space:pre">			</span>re+=s.charAt(index[i]);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>return re;
}


明天起来看看,别的大神有没有更好的解法。

似乎大部分解法的思路与我的算法一致。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: