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

博客围绕字符串之字形转换问题展开,介绍了两种解法。一是用二维数组按之字形存储后逐行逐列读取拼接,耗时19ms,占用38.4MB;二是发现数字规律直接逐行读取,效率最高,耗时3ms,占用36MB。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述:

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"
Explanation:
P   A   H   N
A P L S I I G
Y   I   R

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;
        //columns实际上是指二维数组列下表的最大值
        int columns = s.length()/(2*numRows-2)*(numRows-1);
        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;
    }
}

 

Visit By Row(3ms,36MB)

发现数字间的规律,直接逐行去读取(效率最高)

class Solution {
    public String convert(String s, int numRows) {

        if (numRows == 1) return s;

        StringBuilder ret = new StringBuilder();
        int n = s.length();
        int cycleLen = 2 * numRows - 2;
        //逐行去读取
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j + i < n; j += cycleLen) {
                ret.append(s.charAt(j + i));
                //排除第一行和最后一行,因为两竖笔之间没有数字
                if (i != 0 && i != numRows - 1 && j + cycleLen - i < n)
                    ret.append(s.charAt(j + cycleLen - i));
            }
        }
        return ret.toString();
    }
}

 

转载于:https://www.cnblogs.com/mgblog/p/10911678.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值