题目描述

字符串"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. PADS Layout VX.2.3 将PCB中的元器件封装保存到库

    工具1:PADS Layout VX.2.3 菜单File > Library...,打开Library Manager,点击Create New Lib...新建一个库. 使用快捷键Ctrl ...

  2. 在uniapp或者vue中单行文字或者符号无法换行的终极解决方案

    在VUE开发过程中,会出现比较诡异的情况. 比如常规的英文或中文显示都是很正常的,但是当出现了一些中文符号(比如,!等等)在文末的时候,总是会超出view的显示区域. 那么在遇到上面这种问题我们记得检 ...

  3. 版本控制系统之基于httpd搭建私有git仓库

    在上一篇博客中,我们主要聊到了git的基本工作原理和一些常用的git命令的使用:回顾请参考https://www.cnblogs.com/qiuhom-1874/p/13787701.html:今天我 ...

  4. js 基础概念

    一 执行上下文 和 执行上下文栈 执行上下文:一段javascript代码执行前的准备工作 问题一:js引擎遇到怎样一段代码才会做"准备工作呢"? 可执行代码类型:全局代码.函数代 ...

  5. ps 安装 ps 2017 下载 及教程(保姆式教程)

    链接:https://pan.baidu.com/s/1GJHiwmxwRApFYhyNZBCQtQ 提取码:7r6u 以上是百度网盘的地址. 1.下载解压安装前先断网在安装点击set-up 软件,之 ...

  6. 如何修改或新增visual studio 的模板

    在 visual studio 中添加模板,我这里是新增mvc.net的模板 vs2017在文件夹=>(举例说明,请替换为相应的安装目录) D:\Program Files (x86)\Micr ...

  7. LCA树上倍增求法

    1.LCA LCA就是最近公共祖先(Least common ancestor),x,y的LCA记为z=LCA(x,y),满足z是x,y的公共祖先中深度最大的那一个(即离他们最近的那一个)qwq 2. ...

  8. 【暑假集训】HZOI2019 水站 多种解法

    题目内容 已知有一个\(n\)层的水站: \(W_i\)表示未操作之前第\(i\)层的已有水量: \(L_i\)表示第\(i\)个水站能够维持或者储存的水的重量: 表示在第\(P_i\)层进行减压放水 ...

  9. 【Luogu】P3005 [USACO10DEC]槽的游戏The Trough Game

    一.题目 题目描述 农夫约翰和贝西又在玩游戏.这个游戏需要很多个槽. 农夫约翰在谷仓里藏起来了N(1<=N<=20)个槽,并且他已经把其中的一些装上了食物.贝西以"在这个表里(表 ...

  10. 说明资源路径位置类型无法解析The type javax.servlet.http.HttpServletResponse cannot be resolved.

    导入dispatch项目后报错: 解决办法:在项目上单击鼠标右键>  Add Libraries  选择 Server Runtime,下一步  选中Apache Tomcat7 完成  切换标 ...