题目描述

字符串"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. Lesktop开源IM移动端:接入LayIM移动端UI

    在<开源企业即时通讯和在线客服>中已介绍了Lesktop的桌面模式和Web模式,但是没有移动端.评论中 dotnetcms.org工作室 提到了LayIM,看了一下官网的演示和文档,如果用 ...

  2. try with resource当中你没有注意到点。。

    怎么使用try with resource语法 在 JDK 9 中更简洁使用 try-with-resources 语句 try with resource当中你没有注意到点 try with res ...

  3. RHSA-2018:1200-重要: patch 安全更新(代码执行)

    [root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) 修复命令: 使用root账号登陆She ...

  4. 一些IT service的相关知识

    1. cmd是什么,怎么在电脑上打开cmd命令框. 在windows环境下,命令行程序为cmd.exe,是一个32位的命令行程序,微软Windows系统基于Windows上的命令解释程序,类似于微软的 ...

  5. 分布式系统中的CAP、ACID、BASE概念

    目录 CAP ACID BASE CAP 分布式系统中,这三个特性只能满足其中两个. 一致性(Consistency):分布式中一致性又分强一致性和弱一致性,强一致性主浊任何时刻任何节点看到的数据都是 ...

  6. selenium登录163邮箱,得到cookie,requests后续请求

    1.场景 很多时候登录操作是比较复杂的,因为存在各种反爆破操作,以及为了安全性提交数据都会存在加密.如果要完全模拟代码去实现登录操作是比较复杂,并且该网站后续更新了登录安全相关功能,那么登录的模拟操作 ...

  7. 为什么C语言是最适合单片机编程的高级语言!

    为什么还在用C语言编程?答案是:C语言是最适合单片机编程的高级语言. 这个问题的意思应该是:现在有很多很好用的高级语言,如java,python等等,为什么这些语言不能用来编写单片机程序呢?那么这个问 ...

  8. 拦截导弹简单版——线性dp

    题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的导弹 ...

  9. 【Azure Redis 缓存 Azure Cache For Redis】如何设置让Azure Redis中的RDB文件暂留更久(如7天)

    问题描述 Azure Redis和所有的Redis服务一样,可以让你保留存储在Redis中的数据.以防万一在Redis服务器出现故障的时候能尽可能小的减少数据的损失.在Azure Redis服务中,默 ...

  10. dubbo-config-spring自定义xml标签扩展

    要实现自定义自定义标签扩展,需要有如下步骤(在spring中定义了两个接口NamespaceHandler.BeanDefinitionParser,用来实现扩展) 1.设计配置属性和JavaBean ...