LeetCode第六题—— ZigZag Conversion(字符串的之字形转换)

Leetcode第六题—— 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)

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 s, int numRows);

Example 1:

Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"

Example 2:

Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:

P     I    N
A   L S  I G
Y A   H R
P     I

My Solution(19ms,38.4MB)

比较容易想到的一个办法,用一个二维数组按照之字形储存起来,然后逐行逐列地去读取,然后拼接成一个字符串。

class Solution {
    public String convert(String s, int numRows) {
        if(numRows<2){return s;}//防止除数为零
        int index = 0,row=numRows-2;
        int columns = s.length()/(2*numRows-2)*(numRows-1);//columns实际上是指二维数组列下表的最大值
        int remainder = s.length()%(2*numRows-2);
        if(remainder>numRows){columns = remainder - numRows + columns;}
        String[][] zigzag = new String[numRows][columns+1];
        //开始逐行填充数组
        for(int j = 0;j<columns+1;j++){
            if(j%(numRows-1)==0){//竖笔
              for(int i = 0;i<numRows;i++){
                if(index<s.length()){
                    zigzag[i][j] = s.substring(index,index+1);index++;
                    row = numRows-2;
                }
              }  
            }else{
                if(index<s.length()){
                    zigzag[row][j] =  s.substring(index,index+1);index++;
                    row--;
                }
            }
        }
        开始拼接结果字符串
        String result = "";
        for(int i =0 ; i<numRows ;i++){
            for(int j=0;j<columns+1;j++){
                if(zigzag[i][j]!=null){
                    result = result + zigzag[i][j];
                }
            }
        }
        return result;
    }
}

猜你喜欢

转载自www.cnblogs.com/mgblog/p/10911678.html