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"
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"
输出
"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的更多相关文章
- 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 ... 这个错误是因为有两个相 ...
- 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 ...
- 【leetcode】ZigZag Conversion
题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...
- Conversion Operators in OpenCascade
Conversion Operators in OpenCascade eryar@163.com Abstract. C++ lets us redefine the meaning of the ...
- No.006:ZigZag Conversion
问题: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- 错误提示:LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt 的解决方法
最近在win7 系统下,打算利用 cmake 生成项目文件,然后用vs2010进行编译.但是在cmake的时候出现错误弹窗:
- ZigZag Conversion leetcode java
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- VS2010 LINK1123:failure during conversion to COFF:file invalid or corrupt
今天用Visual Studio 2010编译一个C工程时突然遇到下面这个编译错误.fatal error LINK1123:failure during conversion to COFF:fil ...
- 【leetcode❤python】 6. ZigZag Conversion
#-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object): def convert(self, s, numRow ...
- LeetCode 6 ZigZag Conversion 模拟 难度:0
https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in ...
随机推荐
- 【题解】[SDOI2017]数字表格
Link #include<bits/stdc++.h> using namespace std; #define int long long const int MAXN=1e6; in ...
- 跟我一起学Redis之五种基本类型及其应用场景举例(干了6个小时)
前言 来啦,老弟?来啦,上一篇就当唠唠嗑,接下来就开始进行实操撸命令,计划是先整体单纯说说Redis的各种用法和应用,最后再结合代码归纳总结. Redis默认有16个数据库(编号为0~15),默认使用 ...
- 一、Vuforia_AR
一.AR概念: 增强现实(Augmented Reality,简称AR),是一种将虚拟信息与真实世界巧妙融合的技术,广泛运用了多媒体.三维建模.实时跟踪及注册.智能交互.传感等多种技术手段,将计算机生 ...
- Redis使用RDB持久化和AOF持久化的区别 - 小白之所见
- 网站搭建-云服务器是什么-云服务器ECS是什么
学习上瘾了,本博客关闭,后期再总结整理.
- day60 Pyhton 框架Django 03
day61 内容回顾 1.安装 1. 命令行: pip install django==1.11.18 pip install django==1.11.18 -i 源 2. pycharm sett ...
- 适合刚刚学习编程的萌新:C语言编程学习制作超简单又好玩的报数游戏!
C语言是面向过程的,而C++是面向对象的 C和C++的区别: C是一个结构化语言,它的重点在于算法和数据结构.C程序的设计首要考虑的是如何通过一个过程,对输入(或环境条件)进行运算处理得到输出(或实现 ...
- 【Targan+LCA】HDU 3686 Traffic Real Time Query
题目内容 洛谷链接 给出一个\(n\)个节点,\(m\)条边的无向图和两个节点\(s\)和\(t\),问这两个节点的路径中有几个点必须经过. 输入格式 第一行是\(n\)和\(m\). 接下来\(m\ ...
- 华为路由器配置OSPF
OSPF是什么 OSPF(Open Shortest Pass First,开放最短路径优先协议),是一个最常用的内部网管协议,是一个链路状态协议. 使用场景:适用于运营商.政府机构等大型网络中多节点 ...
- Windows环境下vscode Live Server插件如何开启https
0x01 vscode http插件 Live Server如何开启https 在本机端的开发环境下,如果要测试一些需要HTTPS的功能可以使用mkcert给自己颁发凭证 0x02 安装步骤如下: 1 ...