码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 6 -- ZigZag Conversion

时间:2015-05-06 21:04:50      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

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


 

自己一上来没搞懂什么是zigzag,还以为就是多了个中点,结果怎么都不对,搞得相当复杂,权且贴上代码,当个教训吧:

 1 public class ZigzagConversion {
 2     public static String convert(String s, int numRows) {
 3         if (s == null || s.length() == 0)
 4             return s;
 5         if (numRows < 2)
 6             return s;
 7         int gap = numRows - 2;
 8         String res = "";
 9 
10         for (int i = 0; i < numRows; i++) {
11             if (i == 0) {
12                 for (int j = i; j < s.length(); j += (numRows * 2 - 2))
13                     res += s.charAt(j);
14             } else if (i == numRows - 1) {
15                 for (int j = i; j < s.length(); j += (numRows * 2 - 2))
16                     res += s.charAt(j);
17             } else {
18                 for (int j = i; j < s.length(); j += (numRows * 2 - 2)) {
19                     res += s.charAt(j);
20                     if (j + numRows * 2 - 3 - i < s.length())
21                         res += s.charAt(j + numRows * 2 - 3 - i);
22                 }
23             }
24         }
25 
26         return res;
27     }
28 
29     public static void main(String[] args) {
30         String s = "ABCDE";
31         String res = convert(s, 4);
32         System.out.println(res);
33     }
34 
35 }

问题代码,边界问题没有解决好

 

 1 public class Solution {
 2     public String convert(String s, int nRows) {
 3         if (s == null || s.length() <= nRows || nRows <= 1) return s;
 4         StringBuffer sb = new StringBuffer();
 5         // the first row
 6         for (int i = 0; i < s.length(); i += (nRows - 1) * 2) {
 7             sb.append(s.charAt(i));
 8         }
 9         
10         for (int i = 1; i < nRows - 1; i++) {
11             for (int j = i; j < s.length(); j+= (nRows - 1) * 2) {
12                 sb.append(s.charAt(j));
13                 if (j + (nRows - i - 1) * 2 < s.length()) {
14                     sb.append(s.charAt(j + (nRows - i - 1) * 2));
15                 }
16             }
17         }
18         // the last row
19         for (int i = nRows - 1; i < s.length(); i += (nRows - 1) * 2) {
20             sb.append(s.charAt(i));
21         }
22         return sb.toString();
23     }
24 }

 

 

后来百度了下,看到别人代码相当简洁,方知自己压根就没搞懂题目。

Zigzag:即循环对角线结构(

0       8       16      
1     7 9     15 17      
2   6   10   14   18      
3 5     11 13     19      
4       12       20      

构建n个字符串,循环为每个分别添加相应的字符,最后合并所有字符;

自己照着思路重新写了下:

 1 public class ZigzagConversion {
 2     public static String convert(String s, int numRows) {
 3         if (s == null || s.length() == 0)
 4             return s;
 5         if (numRows < 2)
 6             return s;
 7         String[] res = new String[numRows];
 8         String result = "";
 9         
10         int i = 0, j;
11 
12         while (i < s.length()) {
13             for (j = 0; j < numRows && i < s.length(); j++) {
14                 res[j] += s.charAt(i);
15                 i++;
16             }
17             for (j = numRows - 2; j > 0 && i < s.length(); j--) {
18                 res[j] += s.charAt(i);
19                 i++;
20             }
21         }
22         for(int k = 0; k < numRows;k++)
23             result += res[k];
24         return result;
25     }
26 
27     public static void main(String[] args) {
28         String s = "PAYPALISHIRING";
29         String res = convert(s, 3);
30         System.out.println(res);
31     }
32 }

只是没次字符串数组都默认初始化为null,如何覆盖还没解决,原文用C++代码如下:

 1 string convert(string s, int nRows){
 2     if(nRows == 1) return s;
 3     string res[nRows];
 4     int i = 0, j, gap = nRows-2;
 5     while(i < s.size()){
 6         for(j = 0; i < s.size() && j < nRows; ++j) res[j] += s[i++];
 7         for(j = gap; i < s.size() && j > 0; --j) res[j] += s[i++];
 8     }
 9     string str = "";
10     for(i = 0; i < nRows; ++i)
11         str += res[i];
12     return str;
13 }

 

发现自己一些问题:首先是不能很好的思考问题,或者说自己的思路多少总会有些偏差;其次就是边界问题,总是犯些小错误。

LeetCode 6 -- ZigZag Conversion

标签:

原文地址:http://www.cnblogs.com/myshuangwaiwai/p/4483004.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!