LeetCode题库解答与分析——#6.Z字形转换ZigZagConversion

杀马特。学长 韩版系。学妹 提交于 2019-12-08 07:00:23

#6 Z字形转换 ZigZag Conversion

将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数:(下面这样的形状)

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

之后按逐行顺序依次排列:"PAHNAPLSIIGYIR"

实现一个将字符串进行指定行数的转换的函数:

string convert(string text, int nRows);

convert("PAYPALISHIRING", 3) 应当返回 "PAHNAPLSIIGYIR" 。

个人思路:

分别定义根据给定行数和当前字符长度确定所在行和列的函数,并声明二维数组按示例方式保存每个字符至相应位置,最后按行循环输出最终的字符串

代码(Java)(未通过)

import java.lang.Math.*;
class Solution {
    public String convert(String s, int numRows) {
        if(numRows>=s.length()){
            return s;
        }
        int numCols = countCol(s.length(),numRows);
        int row=0,col=0;
        char c[][] = new char[numRows][numCols];
        String result="";
        for(int i=1;i<=s.length();i++){
            row=countRow(i,numRows)-1;
            col=countCol(i,numRows)-1;
            System.out.print("row "+row+"\n"+"col "+col+"\n");
            //c[row][col]=s.charAt(i-1);
        }
        for(int i=0;i<numRows;i++){
            for(int j=0;j<numCols;j++){
                if(c[i][j]!='\0'){
                    result=stringAppend(result,c[i][j]);
                }
            }
        }
        return result;
    }
    public int countCol(int length,int numRows){
        if(numRows==1||length==1){
            return length;
        }
        if(numRows>=length){
            return 1;
        }
//        if(numRows==2){
//            return (int)Math.ceil(length/2);
//        }
        int col=0;
        int gro=2*numRows-2;//每组字符的数量
        int remain=length%gro;//长度除以每组得到的余数
        int time=length/gro;//共有多少组(向下取整)
        int cols_per=gro-numRows+1;//每组有多少列
        if(remain>0&&remain<=numRows){
            col = (gro-numRows+1)*time+1;
        }
        else if(remain==0){
            col = (gro-numRows+1)*time;
        }
        else{
            col = remain-numRows+1+(gro-numRows+1)*time;
        }
        return col;
    }
    public int countRow(int length,int numRows){
        if(numRows==1||length==1){
            return 1;
        }
        if(numRows==2){
            if(length%2==0) return 2;
            else return 1;
        }
        int row=0;
        int gro=2*numRows-2;
        int remain=length%gro;
        if(remain>0&&remain<=numRows){
            row = remain;
        }
        else if(remain==0){
            row = 2;
        }
        else{
            row = numRows-(remain-numRows);
        }
        return row;
    }
    String stringAppend(String a,char b){
        return a+b;
    }
}

他人思路:

声明长度为给定行数的StringBuffer数组,设定双循环,两个内循环,先由上至下添加到数组中的每一行,再由下至上添加

import java.lang.Math.*;
class Solution {
public String convert(String s, int nRows) {
    char[] c = s.toCharArray();
    int len = c.length;
    StringBuffer[] sb = new StringBuffer[nRows];
    for (int i = 0; i < sb.length; i++) sb[i] = new StringBuffer();
    
    int i = 0;
    while (i < len) {
        for (int idx = 0; idx < nRows && i < len; idx++) // vertically down
            sb[idx].append(c[i++]);
        for (int idx = nRows-2; idx >= 1 && i < len; idx--) // obliquely up
            sb[idx].append(c[i++]);
    }
    for (int idx = 1; idx < sb.length; idx++)
        sb[0].append(sb[idx]);
    return sb[0].toString();
}
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!