zoukankan      html  css  js  c++  java
  • LeetCode-6 ZigZag Conversion

    两种解法

    1、将字符串先拼成图

     1     public String convert(String s, int numRows) {
     2         int l=s.length();
     3         if(l<=numRows)
     4             return s;
     5         if(numRows==1)
     6             return s;
     7         int COL=numRows-1;//每块占的列数
     8         int numCols=(int)Math.ceil(l*1.0/(2*numRows-2))*COL;
     9         char[][] cs=new char[numRows][numCols];
    10         int indexBlock=0,index=0;//块顺序,块中的编号
    11         int r=0,c=0;//所在的行和列
    12         //
    13         //System.out.println("number of columns is "+numCols);
    14         //
    15         for(int i=0;i<numRows;i++)
    16         {
    17             for(int j=0;j<numCols;j++)
    18             {
    19                 cs[i][j]=0x7fff;
    20             }
    21         }
    22         for(int i=0;i<l;i++)
    23         {
    24             indexBlock=(int)Math.floor(i*1.0/(2*numRows-2));
    25             index=(int)(i%(2*numRows-2));
    26             if(index<numRows)//竖列
    27             {
    28                 c=indexBlock*COL;
    29                 r=index;
    30                 cs[r][c]=s.charAt(i);
    31                 //System.out.println("竖列  "+i+"th char "+s.charAt(i)+" row="+r+"  col="+c);
    32             }
    33             else //
    34             {
    35                 c=indexBlock*COL+index+1-numRows;
    36                 r=numRows-(index-numRows)-2;
    37                 cs[r][c]=s.charAt(i);
    38                 //System.out.println("斜列  "+i+"th char "+s.charAt(i)+" row="+r+"  col="+c);
    39             }
    40         }
    41         //输出
    42         StringBuilder builder=new StringBuilder();
    43         for(int i=0;i<numRows;i++)
    44         {
    45             for(int j=0;j<numCols;j++)
    46             {
    47                 if(cs[i][j]!=0x7fff)
    48                 {
    49                     builder.append(cs[i][j]);
    50                 }
    51             }
    52         }
    53         
    54         return builder.toString();
    55     }

    2、动态添加

     1    public String convert(String s, int numRows) {
     2         if(numRows==1) return s;
     3         if(s.length()<numRows) return s;
     4         String[] strs=new String[numRows];
     5         //initial
     6         for(int i=0;i<numRows;i++)
     7         {
     8             strs[i]="";
     9         }
    10         int index=0,gap=numRows-2,rowIndex=0;
    11         while(index<s.length())
    12         {
    13             for(rowIndex=0;rowIndex<numRows && index<s.length();rowIndex++) 
    14             {
    15                 strs[rowIndex]+=""+s.charAt(index++);
    16             }
    17             
    18             for(rowIndex=gap;rowIndex<=gap && rowIndex>0 && index<s.length();rowIndex--)
    19             {
    20                 strs[rowIndex]+=""+s.charAt(index++);
    21             }
    22             
    23         }
    24         //输出
    25         StringBuilder builder=new StringBuilder();
    26         for(int i=0;i<numRows;i++)
    27         {
    28             builder.append(strs[i]);
    29         }
    30         return builder.toString();
    31     }
  • 相关阅读:
    is 和 == 的区别,utf和gbk的转换,join用法
    python字典的整理信息
    poj分类
    cloud computing
    POJ1007 关于STL排序方法 动态数组的创建 和向量的使用
    math类
    研究生考试感想
    4.11
    重看设计模式 观察者模式
    子串计算 2010北京大学复试机试题
  • 原文地址:https://www.cnblogs.com/maydow/p/4625470.html
Copyright © 2011-2022 走看看