• 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;
        }

  • 相关阅读:
    Linux tomcat 去除项目名端口号直接用ip或者域名访问网站
    Linux SSH 安装Tomcat
    Linux SSH下安装Java并设置环境
    自己把jar包添加到maven仓库中
    eclipse 导入maven项目
    将eclipse左边目录结构改为 树形结构
    htt p第一章概述
    Markdown编辑器 简单使用
    CSS 盒子模型
    CSS 基本样式
  • 原文地址:https://www.cnblogs.com/shisw/p/4643645.html
Copyright © 2020-2023  润新知