6 zigzag conversion

这种题看着就恶心。。。一点意思都没有。。。我刚开始的时候,还好认真的思考这种题,反复回味,在当时还是挺好的一种锻炼。。。本质上就是怎么把一个2D 矩阵的index进行重排。。。找到规律就好了。。。

下面的代码太长了,我自己都不想看,烧脑子,而且没什么用。。。现在知道用更简便的方法了。。。


public class Solution {
    public String convert(String s, int numRows) {
        // essential: transform 1D index to 2D index
        StringBuilder sb = new StringBuilder();
        if(s=="") return "";
        else if( numRows==1) return s;
        
        /**add this special case after review my result when input being "AB", 2*/
        else if( numRows==2 ) {
            for(int x=0; x<s.length(); x+=2){
                sb.append(s.charAt(x));
            }
            for(int y=1; y<s.length(); y+=2){
                sb.append(s.charAt(y));
            }
            
            return sb.toString();
        }
        
        else{
            int n=numRows;
            int blockSize = (2*n-2);
            int block = s.length()/blockSize;
            int remainder = s.length()%blockSize;
            int blockWidth = n-1;
            
            int rowNum=0; int colNum=0;
            
            // set colNum  !!! blockWidth, not blockSize
            if(remainder==0) colNum=block*blockWidth;
            else if(remainder>0 && remainder<=n) colNum=block*blockWidth+1;
            else if(remainder>n && remainder<2*n-2) colNum=block*blockWidth+1+(remainder-n);
            
            // set rowNum
            if(block==0 && remainder<=n) return s; // remainder = s.length; directly return!!!
            else rowNum = n;
            
            // set matrix dim
            char[][] matrix = new char[rowNum][colNum];
            
            // filling matrix
            for(int i=1; i<=s.length(); i++){  // a little trick for index in the string, easy to follow the same algorithm as rowNum, colNum
                int blockID = i/blockSize;
                int remainderID = i%blockSize;
                
                // transform 1D index to 2D index
                if(remainderID>=1&&remainderID<=n) matrix[remainderID-1][blockID*blockWidth+1-1]=s.charAt(i-1);// in matrix, row and col starts w/ 0 
                else if(remainderID>n&&remainderID<(2*n-2)) matrix[n-1-(remainderID-n)][blockID*blockWidth+1+(remainderID-n)-1]=s.charAt(i-1);
                else if(remainderID==0) matrix[1][blockID*blockWidth-1]=s.charAt(i-1);
            }
            
            // generating result
            for(int p=0; p<rowNum; p++){
                for(int q=0; q<colNum; q++){
                    if(matrix[p][q]!='\u0000') sb.append(matrix[p][q]);
                }
                
            }
        }
        
        return sb.toString();
    }
}

看看今天写的码,简练多了。。。具体分析见ppt

public class Solution {
    public String convert(String s, int numRows) {
        if(numRows==1) return s;  // 这个特殊情况一定要考虑,因为jump在numRows==1时为0,那么就是死循环了。。。
        int jump=2*numRows-2;
        StringBuilder result = new StringBuilder();
        for(int i=0; i<numRows; i++){
            for(int j=i; j<s.length(); j+=jump){
                result.append(s.charAt(j));
                if(i!=0 && i!=numRows-1 && ((j+jump-2*i)<s.length())){
                    result.append(s.charAt(j+jump-2*i));
                }
            }
        }
        
        return result.toString();
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值