zoukankan      html  css  js  c++  java
  • 6:ZigZag Conversion

    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   N
    A P L S I I G
    Y   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".

    参考 ZigZag Conversion 思路很清晰

    String convert(String s, int nRows){
            if(nRows == 1){
                return s;
            }
            
            String [] strs = new String[nRows];
            
            int i=0,j,gap = nRows-2;
            while(i<s.length()){
                for(j=0;j<nRows && i<s.length();j++){
                    if(strs[j]==null){
                        strs[j] = "";
                    }
                    strs[j] += s.charAt(i++);
                }
                for(j=gap;j>0 && i<s.length();j--){
                    strs[j] += s.charAt(i++);
                }
            }
            
            String str = "";
            for(String ss : strs){
                str += ss;
            }
            return str;
        }

  • 相关阅读:
    [转] Optimizely:在线网站A/B测试平台
    批处理命令——choice
    批处理命令——%0
    批处理命令——call 和 start
    批处理命令——rem 和 pause
    批处理命令——goto 和 :
    PHPCMS V9 学习总结
    PHPCMS V9 环境搭建
    批处理命令——echo 和 @
    利用Qt Assistant 定制帮助文档
  • 原文地址:https://www.cnblogs.com/shisw/p/4643645.html
Copyright © 2011-2022 走看看