ZigZag Conversion
来源:互联网 发布:职业经理人 知乎 编辑:程序博客网 时间:2024/06/05 19:46
idea
if we could know the index of each character which is in the original string,the problem will be solved easily.Now we need to find the regulationin the conversion process by some example.
example
e.g1:
string:“abcdefabcdef” nRows:4
after conversion:
as shown in the figure above,the old string was divided into two units and every unit was integrated which is we want to see.
obviously,the result String is "aabfbfcecedd",but it is most primary purposes to find the law.
It is not difficult to find that when we know the "nRows",we can figure out a lot of information include "nCols" which refer to the unit count,int this example,the "nCols" is 2,the quantity of total characters in a unit,the relationship between index ,"nRows" and "nCols".
Law summary:
Assuming the "nRows" is n,the quantity of total characters in a unit is n+n-2,being 2n-2.the "nCols" is {s.length()/(2n-2)} ,{x} expressed the smallest integer that is not less than x.
the index law:
when we konw the index of the first unit,the rest of unit will be determinated.
now we focus that how to express first unit by "n" when "nRows" is "n".
the first unit index:
now we have konwn all index of characters in the new String "pciture" after Zigzag conversion.
then what we need to do is to write into "result string" line by line according the "picture".it is easy.
All of these are based on conditions that the "s" can be divided into some unbroken units.however if the "s" doesn't satisfy this condition,what should we do?
e.g2:
string:“abcdefabcdefg” nRows:4
after conversion,we konw the last unit only have one character “g”.some errors will occurre in the program under the circumstances.
Don't worry,we can sovle by adding right quantity of character such as "#" behind the original s
in this e.g. ,we need to add 5 "#",then s= “abcdefabcdefg#####”.After that,Follow the steps of example 1,finally we will get "reslut string" include the characters weadded.It is true "result String" after getting rid of these redundant characters.
Java Code
public String convert(String s, int numRows) {
if(s==""||numRows==1){
return s;
}
String rString="";
int l=s.length();
int numCols=(int) Math.ceil((double)l/(double)(2numRows-2));
int jiange=2numRows-2;
while(numCols*(2numRows-2)>l){
s=s+"#";
l++;
}
//first row
for(int col=0;col<numCols;col++){
rString=rString+s.charAt(0+coljiange);
}
//form seconde to numRows-1 row
for(int row=2;row<=numRows-1;row++){
for(int col=0;col<numCols;col++){
rString=rString+s.charAt(row-1+coljiange)+s.charAt(2numRows-row-1+coljiange);
}
}
//the nRows row
for(int col=0;col<numCols;col++){
rString=rString+s.charAt(numRows-1+coljiange);
}
System.out.println("one to nRows row:"+rString);
return rString.replace("#", "");
}
- ZigZag Conversion
- ZigZag Conversion
- ZigZag Conversion
- Zigzag Conversion
- ZigZag Conversion
- ZigZag Conversion
- ZigZag Conversion
- ZigZag Conversion
- ZigZag Conversion
- ZigZag Conversion
- ZigZag Conversion
- ZigZag Conversion
- ZigZag Conversion
- ZigZag Conversion
- ZigZag Conversion
- ZigZag Conversion
- ZigZag Conversion
- ZigZag Conversion
- bzoj1588 [HNOI2002]营业额统计
- Tensorflow学习笔记——占位符和feed_dict(二)
- 解决Android SDK Manager 无法更新下载SDK版本问题
- 字符串置换-lintcode-c++
- bzoj2301 [HAOI2011]Problem b
- ZigZag Conversion
- CentOS 系统启动流程(四)
- bzoj2743 [HEOI2012]采花
- css3-划过div放大突出效果
- java多线程(5)之线程池概念理解
- Raspberry Pi 3 Model B snappy Ubuntu core安装及使用
- 持续集成进阶篇
- collections.nametuple的用法
- Coprime HDU