• ZigZag Conversion


    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   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".

    class Solution {
    public:
        string convert(string s, int nRows) 
        {
            string* str=new string[nRows];
            int index=0;
            while(index<s.length())
            {
                for(int j=0;j<nRows && index<s.length();j++)
                    str[j]+=s[index++];
                for(int j=nRows-2;j>0 && index<s.length();j--)
                    str[j]+=s[index++];
            }
            string result;
            if(nRows>s.length()) nRows=s.length();
            for(int i=0;i<nRows;i++)
                result=result+str[i];
            return result;
        }
    }; 
  • 相关阅读:
    day04
    day02
    day01
    if语句用户交互字符串
    python安装和pycharm安装教程
    day1预习
    博客园的使用
    python day 3
    从cbv到fbv:用函数写视图与用类写视图的区别(drf与restful)
    resful规范: 进行数据交换时的代码潜规则
  • 原文地址:https://www.cnblogs.com/erictanghu/p/3759179.html
Copyright © 2020-2023  润新知