题目描述

字符串"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;
        
        
    }
};

leetcode143zigzag-conversion的更多相关文章

  1. Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ...

    Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ... 这个错误是因为有两个相 ...

  2. View and Data API Tips : Conversion between DbId and node

    By Daniel Du In View and Data client side API, The assets in the Autodesk Viewer have an object tree ...

  3. 【leetcode】ZigZag Conversion

    题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...

  4. Conversion Operators in OpenCascade

    Conversion Operators in OpenCascade eryar@163.com Abstract. C++ lets us redefine the meaning of the ...

  5. No.006:ZigZag Conversion

    问题: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  6. 错误提示:LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt 的解决方法

    最近在win7 系统下,打算利用 cmake 生成项目文件,然后用vs2010进行编译.但是在cmake的时候出现错误弹窗:

  7. ZigZag Conversion leetcode java

    题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  8. VS2010 LINK1123:failure during conversion to COFF:file invalid or corrupt

    今天用Visual Studio 2010编译一个C工程时突然遇到下面这个编译错误.fatal error LINK1123:failure during conversion to COFF:fil ...

  9. 【leetcode❤python】 6. ZigZag Conversion

    #-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object):    def convert(self, s, numRow ...

  10. LeetCode 6 ZigZag Conversion 模拟 难度:0

    https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in ...

随机推荐

  1. P4552 [Poetize6] IncDec Sequence

    Link 题目描述 给定一个长度为 \(n\) 的数列 \({a_1,a_2,\cdots,a_n}\),每次可以选择一个区间 \([l,r]\),使这个区间内的数都加 \(1\) 或者都减 \(1\ ...

  2. Excel-VLOOKUP函数跨表匹配查找①

    问题场景 对表中的员工进行测评总结,从所有员工考核明细表中匹配这些参与测评的员工的得分和相关信息: 场景一 从所有员工明细表中匹配需要参与测评的员工相关信息. 建了两个sheet页,考核员工表和全员考 ...

  3. Azure Cosmos DB (二) SQL API 操作

    一,引言 还记得国庆期间,我们学习了一下关于Azure Cosmos DB 的一些基础知识以及Azure Cosmos DB 的几种支持数据库类型.今天就开始分享一些实战操作,如何通过Azure Po ...

  4. Python实现好友生日提醒

    Python实现好友生日提醒  

  5. Cypress系列(63)- 使用 Custom Commands

    如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html Custom Commands 自定义命 ...

  6. spring cloud 实现基于Nacos权重的负载均衡

    package com.water.step.service.user.nacos; import com.alibaba.nacos.api.exception.NacosException; im ...

  7. Python错误:AssertionError: group argument must be None for now

    运行多线程出现的错误 调试了很久,最后发先 __init__ 写错了,修改后后,运行正确.

  8. linux(centos8):zabbix配置邮件报警(监控错误日志)(zabbix5.0)

    一,zabbix5.0发邮件报警的准备工作: zabbix5.0在linux平台上的安装:参见这一篇: https://www.cnblogs.com/architectforest/p/129125 ...

  9. 如果只推荐一本 Python 书,我要 Pick 它!

    今年二月初,我偶然看到了一条推特: <流畅的Python>一书的作者发布了一条激动人心的消息:他正在写作第二版! 如果要票选最佳的 Python 进阶类书目,这本书肯定会是得票率最高的书籍 ...

  10. 如何使用 Azure Active Directory 认证和 Microsoft Graph 构建 Blazor Web 应用

    如何使用 Azure Active Directory 认证和 Microsoft Graph 构建 Blazor Web 应用 英文原文:https://developer.microsoft.co ...