LeetCode06:ZigZag Conversion

原题目:
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)
      A         H         N
P           I   I     G
      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".

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   
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" .

给定一个字符串,并给定特定的行号,输出该字符串的之字打印结果,按行输出。

算法分析:

题目要求按之字型排列数组,并进行输出,4行之字形的排列形状如下所示:
0              6                 12
1        5  7          11 13
2   4       8    10       14
3            9               15
最后输出字符为0612157111324810143915

算法1:
将这个排列放入一个二维矩阵中,然后将矩阵按列从上到下,再向右上依次输出
0     6     12
1   5 7   11 13
2 4   8 10   14
3     9     15

算法2:
对于以上的字符排列,可以发现以下规律:
(1)第1行和最后一行的每个字符间隔为2*numRows - 2(以4行为例,0——6——12间隔为 6,所以2*4-2 = 6)
(2)中间行的间隔是周期性的,第i行的间隔是: 2*numRows-2–2*i,  2*i,  2*numRows-2–2*i, 2*i, 2*numRows-2–2*i, 2*i, …



遇到问题及解决方案:

1.给字符串赋空值。当声明一个字符串时,不应该声明为null,这样在输出的字符串的开头会包含null,而应该生成为"",这样就不会出现null字符
String  resultString  =  "" ;                //此处不要将resultSring 和tmp两个字符串初值设为null,这样输出字符串前会有null出现
        String tmp = "";                         //而应当直接赋值一个""
2.char型的默认值为空,即\u0000或'',但不能用null来判断,用null无法判断是否为空字符。
                    if matrix[m][n] != ' '){    或        if matrix[m][n] !=  null ){  
     无法判断空格
所以在判断矩阵是否为空时,应该用\u0000来去除空字符。
ifmatrix[m][n] != '\u0000'){      


LeetCode提交源码:

算法1:
   
   
  1. public String convert(String s, int numRows){
  2. if(s.length() <= 0 || numRows <= 0){
  3. return "";
  4. }
  5. if(numRows == 1){
  6. return s;
  7. }
  8. String resultString = ""; //此处不要将resultSring 和tmp两个字符串初值设为null,这样输出字符串前会有null出现
  9. String tmp = ""; //而应当直接赋值一个""
  10. char[] arr1 = s.toCharArray();
  11. char[][] matrix = new char[numRows][300];
  12. if(numRows == 2){
  13. for(int i = 0; 2*i < arr1.length; i++){
  14. tmp += arr1[2*i];
  15. }
  16. for(int i = 0; 2*i+1 < arr1.length; i++){
  17. //tmp += arr1[2*i];
  18. tmp += arr1[2*i+1];
  19. }
  20. resultString = tmp;
  21. }
  22. System.out.println("length= " + s.length());
  23. if(numRows >= 3){
  24. int i = 0;
  25. int j = 0;
  26. // int len = 0;
  27. // int r = numRows;
  28. /*
  29. 将字符放入二维数组中
  30. */
  31. for(int k = 0; k < arr1.length;){
  32. while(i < numRows){
  33. matrix[i][j] = arr1[k];
  34. //len++;
  35. i++;
  36. k++;
  37. if(k >= arr1.length)
  38. break;
  39. }
  40. if(k >= arr1.length)
  41. break;
  42. if(i == numRows){
  43. i = i-2;
  44. j++;
  45. while(i > 0){
  46. matrix[i][j] = arr1[k];
  47. i--;
  48. j++;
  49. k++;
  50. if(k >= arr1.length)
  51. break;
  52. }
  53. }
  54. }
  55. /*
  56. 从二维数组中取出字符,去除空格
  57. 重要:char型的默认值为空,\u0000,但不能用null判断,所以在判断矩阵是否为空时,不能用户
  58. ' ' 或" "或null,而应该用\u0000来去除空字符。
  59. */
  60. for(int m = 0; m < numRows; m++){
  61. for(int n = 0; n < 300; n++){
  62. if( matrix[m][n] != '\u0000'){ //重要!!!
  63. tmp += matrix[m][n];
  64. }
  65. }
  66. }
  67. resultString = tmp;
  68. }
  69. return resultString;
  70. }

 
算法2:

   
   
  1. public String convert(String s, int numRows){
  2. if(numRows == 1)
  3. return s;
  4. int len = s.length();
  5. // int k = 0;
  6. int interval = numRows *2-2; //每行字符的间隔
  7. String resultString = "";
  8. char[] array = s.toCharArray();
  9. for(int j = 0; j < len;j+=interval){
  10. resultString += array[j];
  11. }
  12. for(int i = 1; i < numRows - 1; i++){
  13. int inter = (i<<1); //inter = 2*i;
  14. for(int j = i; j < len; j+=inter){
  15. resultString += array[j];
  16. inter = interval - inter; //2numRows-2-2i
  17. }
  18. }
  19. for(int j = numRows - 1; j < len; j += interval){ //处理最后一行
  20. resultString += array[j];
  21. }
  22. return resultString;
  23. }


 
 

完整运行程序:

   
   
  1. /**************************************************************
  2. * Copyright (c) 2016
  3. * All rights reserved.
  4. * 版 本 号:v1.0
  5. * 题目描述:ZigZag Conversion
  6. * The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this:
  7. * (you may want to display this pattern in a fixed font for better legibility)
  8. * P A H N
  9. * A P L S I I G
  10. * Y I R
  11. * And then read line by line: "PAHNAPLSIIGYIR"
  12. * Write the code that will take a string and make this conversion given a number of rows:
  13. * string convert(string text, int nRows);
  14. * convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
  15. * 给定一个字符串,并给定特定的行号,输出该字符串的之字打印结果,按行输出
  16. * 输入描述:请输入一个字符串:
  17. * 0123456789abcdef
  18. * 请输入之字型的行数:
  19. * 4
  20. * 程序输出:算法1输出的字符串为:
  21. * length= 16
  22. * 06c157bd248ae39f
  23. * 算法2输出的字符串为:
  24. * 06c157bd248ae39f
  25. * 问题分析:1.给字符串赋空值。当声明一个字符串时,不应该声明为null,这样在输出的字符串的开头会包含null,
  26. * 而应该生成为"",这样就不会出现null字符
  27. * 2.char型的默认值为空,即\u0000或'',但不能用null来判断,用null无法判断是否为空字符。
  28. * 算法描述:算法1:定义一个二维字符数组,将之字形的排列放入数组中,再将数组中的字符按从上到下,从左下到右上的顺序进行输出。
  29. * 算法2:总结规律
  30. * (1)第1行和最后一行的每个字符间隔为2*numRows - 2(以4行为例,0——6——12间隔为 6,所以2*4-2 = 6)
  31. * (2)中间行的间隔是周期性的,第i行的间隔是: 2*numRows-2–2*i, 2*i, 2*numRows-2–2*i, 2*i, 2*numRows-2–2*i, 2*i, …
  32. * 完成时间:2016-11-14
  33. ***************************************************************/
  34. package org.GuoGuoFighting.LeetCode06;
  35. import java.util.Scanner;
  36. class SolutionMethod1{
  37. public String convert(String s, int numRows){
  38. if(s.length() <= 0 || numRows <= 0){
  39. return "";
  40. }
  41. if(numRows == 1){
  42. return s;
  43. }
  44. String resultString = ""; //此处不要将resultSring 和tmp两个字符串初值设为null,这样输出字符串前会有null出现
  45. String tmp = ""; //而应当直接赋值一个""
  46. char[] arr1 = s.toCharArray();
  47. char[][] matrix = new char[numRows][300];
  48. if(numRows == 2){
  49. for(int i = 0; 2*i < arr1.length; i++){
  50. tmp += arr1[2*i];
  51. }
  52. for(int i = 0; 2*i+1 < arr1.length; i++){
  53. //tmp += arr1[2*i];
  54. tmp += arr1[2*i+1];
  55. }
  56. resultString = tmp;
  57. }
  58. System.out.println("length= " + s.length());
  59. if(numRows >= 3){
  60. int i = 0;
  61. int j = 0;
  62. // int len = 0;
  63. // int r = numRows;
  64. /*
  65. 将字符放入二维数组中
  66. */
  67. for(int k = 0; k < arr1.length;){
  68. while(i < numRows){
  69. matrix[i][j] = arr1[k];
  70. //len++;
  71. i++;
  72. k++;
  73. if(k >= arr1.length)
  74. break;
  75. }
  76. if(k >= arr1.length)
  77. break;
  78. if(i == numRows){
  79. i = i-2;
  80. j++;
  81. while(i > 0){
  82. matrix[i][j] = arr1[k];
  83. i--;
  84. j++;
  85. k++;
  86. if(k >= arr1.length)
  87. break;
  88. }
  89. }
  90. }
  91. /*
  92. 从二维数组中取出字符,去除空格
  93. 重要:char型的默认值为空,\u0000,但不能用null判断,所以在判断矩阵是否为空时,不能用户
  94. ' ' 或" "或null,而应该用\u0000来去除空字符。
  95. */
  96. for(int m = 0; m < numRows; m++){
  97. for(int n = 0; n < 300; n++){
  98. if( matrix[m][n] != '\u0000'){ //重要!!!
  99. tmp += matrix[m][n];
  100. }
  101. }
  102. }
  103. resultString = tmp;
  104. }
  105. return resultString;
  106. }
  107. }
  108. class SolutionMethod2{
  109. public String convert(String s, int numRows){
  110. if(numRows == 1)
  111. return s;
  112. int len = s.length();
  113. // int k = 0;
  114. int interval = numRows *2-2; //每行字符的间隔
  115. String resultString = "";
  116. char[] array = s.toCharArray();
  117. for(int j = 0; j < len;j+=interval){
  118. resultString += array[j];
  119. }
  120. for(int i = 1; i < numRows - 1; i++){
  121. int inter = (i<<1); //inter = 2*i;
  122. for(int j = i; j < len; j+=inter){
  123. resultString += array[j];
  124. inter = interval - inter; //2numRows-2-2i
  125. }
  126. }
  127. for(int j = numRows - 1; j < len; j += interval){ //处理最后一行
  128. resultString += array[j];
  129. }
  130. return resultString;
  131. }
  132. }
  133. public class ZigZagConversion {
  134. public static void main(String[] args){
  135. System.out.println("请输入一个字符串:");
  136. Scanner scanner = new Scanner(System.in);
  137. String str = scanner.nextLine();
  138. System.out.println("请输入之字型的行数:");
  139. int rows = scanner.nextInt();
  140. scanner.close();
  141. SolutionMethod1 solution1 = new SolutionMethod1();
  142. System.out.println("算法1输出的字符串为:");
  143. System.out.println(solution1.convert(str, rows));
  144. SolutionMethod2 solution2 = new SolutionMethod2();
  145. System.out.println("算法2输出的字符串为:");
  146. System.out.println(solution2.convert(str, rows));
  147. }
  148. }


程序运行结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值