zoukankan      html  css  js  c++  java
  • leetcode143zigzag-conversion

    题目描述

    字符串"PAYPALISHIRING"写成3行的Z字形的样式如下:

    P   A   H   N↵A P L S I I G↵Y   I   R


    按行读这个Z字形图案应该是 "PAHNAPLSIIGYIR"

    请编写代码完成将字符串转化为指定行数的Z字形字符串:

    string convert(string text, int nRows);


    convert("PAYPALISHIRING", 3)应该返回"PAHNAPLSIIGYIR"

    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"
    示例1

    输入

    复制
    "AB",2

    输出

    复制
    "AB"
    class Solution {
    public:
        /**
         *
         * @param s string字符串
         * @param nRows int整型
         * @return string字符串
         */
        string convert(string s, int nRows) {
            // write code here
            if (nRows <=1) return s;
            int t=nRows+nRows -2;//求出循环周期
            string res="";
            vector <string> m(nRows,res);
            for (int i=0;i<s.length();i++){
                int a=i%t;
                if (a<nRows)//往下走
                    m[a]+=s[i];
                else
                    m[t-a]+=s[i];//往上走
                
            }
            for (int i=0;i<nRows;i++)
                res+=m[i];
            return res;
            
            
        }
    };



  • 相关阅读:
    windows下安装rocketmq采坑全记录
    测试日常使用---网络协议与抓包
    python重写及重写后调用父类方法
    python继承和多态
    python私有成员都以双下划线“__”开头,仅类内部可访问
    http中的post请求发生了两次(多了一次options请求)的原因
    测试日常使用---linux命令:
    数据库性能优化
    pytest常用配置文件之pytest.ini
    pytest.main()的使用
  • 原文地址:https://www.cnblogs.com/hrnn/p/13353534.html
Copyright © 2011-2022 走看看