详细见: leetcode.com/problems/zigzag-conversion/
Python Solution: github
Java Solution: github
package leetcode;
import java.util.Arrays;
/*
6. ZigZag Conversion QuestionEditorial Solution My Submissions
Total Accepted: 101242
Total Submissions: 406283
Difficulty: Easy
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".
*/
public class P006_ZigZagConversion {
public static void main(String[] args) {
System.out.println(new Solution1().convert("PAYPALISHIRING", 3));
}
/*
* 这道题非常烦,没有同一的方法原型,很困难。
* 82.70%
* 4ms
*/
static class Solution1 {
public String convert(String s, int n) {
if (s == null || s.length() == 0)
return "";
if (n == 1)
return s;
char[] c = new char[s.length()];
int[] index = new int[c.length];
int circle = (n << 1) - 2;
int[] help_last = new int[n];
Arrays.fill(help_last, 0);
int count_last = c.length - (c.length / circle) * circle;
for (int i_last = 0; i_last < count_last; i_last ++) {
if (i_last < n)
help_last[i_last] ++;
else
help_last[- i_last + circle] ++;
}
int nums_group = c.length / circle;
int cur_index = 0;
for (int row = 0; row < n; row ++) {
// i == 0
index[cur_index] = row;
if ( ++ cur_index >= index.length)
break;
if (row != 0 && row != n - 1 && nums_group != 0) {
index[cur_index] = circle - index[cur_index - 1];
if ( ++ cur_index >= index.length)
break;
}
for (int i = 1; i < nums_group; i ++) {
if (row == 0 || row == n - 1) {
index[cur_index] = index[cur_index - 1] + circle;
if ( ++ cur_index >= index.length)
break;
} else {
index[cur_index] = index[cur_index - 2] + circle;
if ( ++ cur_index >= index.length)
break;
index[cur_index] = index[cur_index - 2] + circle;
if ( ++ cur_index >= index.length)
break;
}
}
if ((row == 0 || row == n - 1) && (help_last[row] == 1)) {
if (nums_group != 0) {
index[cur_index] = index[cur_index - 1] + circle;
if ( ++ cur_index >= index.length)
break;
}
} else {
if (help_last[row] == 1) {
if (nums_group != 0) {
index[cur_index] = index[cur_index - 2] + circle;
if ( ++ cur_index >= index.length)
break;
}
} else if (help_last[row] == 2) {
if (nums_group != 0) {
index[cur_index] = index[cur_index - 2] + circle;
if ( ++ cur_index >= index.length)
break;
index[cur_index] = index[cur_index - 2] + circle;
if ( ++ cur_index >= index.length)
break;
} else {
index[cur_index] = circle - index[cur_index - 1];
if ( ++ cur_index >= index.length)
break;
}
}
}
}
for (int i = 0; i != c.length; i ++) {
c[i] = s.charAt(index[i]);
}
return new String(c);
}
}
}
C Solution: github
/*
url: leetcode.com/problems/zigzag-conversion/
25ms 45.36%
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
char _access_char_array(char* s, int n, int t, int i, int j) {
int index = t * j + i;
if (index >= n) return '\0';
return *(s + index);
}
char* convert(char* s, int r) {
int n = strlen(s);
char *a = (char *) malloc(sizeof(char) * (n + 1));
int t = r == 1 ? 1 : 2 * r - 2, i = 0, j = 0, ai = 0, col = n / r + 1;
char c = '\0';
*(a + n) = '\0';
for (i = 0; i < r; i ++) {
for (j = 0; j < col; j ++) {
c = _access_char_array(s, n, t, i, j);
if (c != '\0')
*(a + (ai ++)) = c;
if (i != 0 && i != r - 1 && r > 1) {
c = _access_char_array(s, n, t, t - i, j);
if (c != '\0')
*(a + (ai ++)) = c;
}
}
}
return a;
}
int main() {
char *s = "A";
int r = 1;
char *a = convert(s, r);
unsigned int i = 0;
for (i = 0; i < strlen(a); i ++) {
printf("%c", *(a + i));
}
printf("\r\n");
free(a);
return 0;
}
Python Solution: github
#coding=utf-8
'''
url: leetcode.com/problems/zigzag-conversion/
@author: zxwtry
@email: zxwtry@qq.com
@date: 2017年3月26日
@details: Solution: AC 198ms 14.38%
'''
class Solution(object):
def accessString(self, row_i, col_j, row_num, s, s_len):
index = (2 * row_num - 2) * col_j + row_i
return "" if index >= s_len else s[index]
def convert(self, s, row_num):
"""
:type s: str
:type numRows: int
:rtype: str
"""
if row_num < 2: return s
s_len = 0 if s == None else len(s)
col_num = s_len // (2 * row_num - 2)
ans = []
for row_i in range(row_num):
for col_j in range(col_num + 1):
v = self.accessString(row_i, col_j, row_num, s, s_len)
if v != "": ans.append(v)
if row_i != 0 and row_i != row_num - 1:
v = self.accessString( 2 * row_num - 2 - row_i, col_j, row_num, s, s_len)
if v != "": ans.append(v)
return "".join(ans)
if __name__ == "__main__":
s = "012345678"
sol = Solution()
v = sol.convert(s, 3)
print(v)