6. ZigZag Conversion

6. ZigZag Conversion

Total Accepted: 97722
Total Submissions: 395539
Difficulty: Easy

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 NA P L S I I GY 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"
.


package com.Algorithm.Leetcode;

public class ZigZagConversion6 {
public String convert(String s, int numRows) {
if ("".equals(s) || numRows < 2) return s;
char[] sCArr = s.toCharArray();
char[] mathArr = new char[sCArr.length];
int pos = 0;
int width = 2 * numRows - 2;//首行数据的宽度
int shang =0;
int yuShu =0;
int position = 0;
// System.out.println("sCArr.length= "+sCArr.length);
// System.out.println("width= "+width);
int Tshang = (sCArr.length-1)/width;//商
int TyuShu = (sCArr.length-1)%width;//余数
// System.out.println("Tshang"+Tshang+" TyuShu= "+TyuShu);
for(int i = 0; i shang = i/width;
yuShu = i%width;
if(yuShu==0){//处在第一行的数据
position = shang;
}else if(yuShu==numRows-1){//处在最后一行的数据
if(TyuShu<(width/2)){
position = Tshang+1+2Tshang(yuShu-1)+TyuShu+shang;
}else{//TyuShu大于numRows-1
position = Tshang+1+2Tshang(yuShu-1)+TyuShu-1+shang;
}
}else {//处在中间的数据
int Line=0;
if(TyuShu<(width/2)){//总字符串的余数小于等于width/2
Line = TyuShu;
if(yuShu<(width/2)){//余数是小于width/2
if(yuShu>Line){
position = Tshang+1+(yuShu-1)2Tshang+Line+shang2;
}else{
position = Tshang+1+(yuShu-1)
2Tshang+yuShu-1+shang2;
}
}else if(yuShu>(width/2)){//大于width/2
if((width-yuShu)>Line){
position = Tshang+1+(width-yuShu-1)2Tshang+Line+shang2+1;
}else{
position = Tshang+1+(width-yuShu-1)
2Tshang+(width-yuShu)-1+shang2+1;
}
}
}else{//总字符串的余数大于width/2
Line = width-TyuShu;
if(yuShu if(yuShu<=Line){//余数小于或者等于当前行
position = Tshang+1+(yuShu-1)2Tshang+yuShu-1+shang2;
}else{
position = Tshang+1+(yuShu-1)
2Tshang+Line-1+2(yuShu-Line)+shang2;
}
}else if(yuShu>(width/2)){//大于了width/2
if((width-yuShu)>Line){
position = Tshang+1+(width-yuShu-1)
2Tshang+Line-1+2(width-yuShu-Line)+shang2+1;
}else{
position = Tshang+1+(width-yuShu-1)
2Tshang+(width-yuShu-1)+shang2+1;
}
}

            }
        }
   //     System.out.println("位置= "+i+"= "+sCArr[i]);

// System.out.println("char= "+sCArr[i]+" shang= "+shang +" yuShu= "+yuShu );
// System.out.println("position= "+position );
mathArr[position] = sCArr[i];
// System.out.println("mathArr["+position+"]= "+mathArr[position]);

    }

// for (int i = 0; i < mathArr.length; i++) {
// System.out.println("==mathArr["+i+"]= "+mathArr[i]);
// }
s=String.valueOf(mathArr );
// System.out.println("++s= "+s);

    return s;
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    ZigZagConversion6 zzc = new ZigZagConversion6();
String s = "Apalindromeisaword,phrase,number,orothersequenceofunitsthatcanbereadthesamewayineitherdirection,withgeneralallowancesforadjustmentstopunctuationandworddividers.";
 // String s = "ABCDE";
//  String s = "ABC";
//    String s = "PAYPALISHIRING";
    //PAHNAPLSIIGYIR
    int numRows = 5;
    System.out.println(zzc.convert(s, numRows));
    
}

}

你可能感兴趣的:(6. ZigZag Conversion)