LeetCode6 ZigZag Conversion
题意:
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". (Easy)
分析:
首先是理解题意,题目例子举得不好...起始ZigZag的意思以4举例更易懂一些。
1 7 13
2 6 8 12
3 5 9 11
4 10
读懂了题意,其实就是判断好什么时候向下,什么时候向上即可,我用的vector<string>实现,维护nRows个string,最后拼接在一起。
代码:
class Solution {
public:
string convert(string s, int numRows) {
if (numRows == ) {
return s;
}
vector<string> v(numRows);
int j = ;
for (int i = ; i < s.size(); ++i) {
if ( (i / (numRows - ) ) % == ) {
v[j].insert(v[j].end(), s[i]);
j ++;
}
if ( (i / (numRows - )) % == ) {
v[j].insert(v[j].end(), s[i]);
j--;
}
}
string result;
for (int i = ; i < v.size(); ++i) {
result += v[i];
}
return result;
}
};
LeetCode6 ZigZag Conversion的更多相关文章
- leetcode6:Zigzag Conversion@Python
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【leetcode❤python】 6. ZigZag Conversion
#-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object): def convert(self, s, numRow ...
- 64. ZigZag Conversion
ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...
- No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...
- leetcode第六题 ZigZag Conversion (java)
ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...
- leetcode题解 6.ZigZag Conversion
6.ZigZag Conversion 题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a gi ...
- 6.[leetcode] ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 字符串按照Z旋转90度然后上下翻转的字形按行输出字符串--ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode--No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...
随机推荐
- 通过扩展让ASP.NET Web API支持JSONP -摘自网络
同源策略(Same Origin Policy)的存在导致了“源”自A的脚本只能操作“同源”页面的DOM,“跨源”操作来源于B的页面将会被拒绝.同源策略以及跨域资源共享在大部分情况下针对的是Ajax请 ...
- 怎么从sqlserver 数据库导出 insert 的数据语句
In SSMS in the Object Explorer, right click on the database right-click and pick "Tasks" a ...
- windows平台下,快速删除所有.svn文件夹
新建一个注册表文件名为:DELSVN.reg编辑其内容如下: Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Cla ...
- systemd详解
CentOS 7 使用systemd替换了SysV.Systemd目的是要取代Unix时代以来一直在使用的init系统,兼容SysV和LSB的启动脚本,而且够在进程启动过程中更有效地引导加载服务. s ...
- Percona Xtrabackup备份mysql(转)
add by zhj:另外,参考了Xtrabackup之innobackupex备份恢复详解,我用的是Xtrabackup2.2.6版本, 可以成功备份和恢复指定的数据库. 原文:http://www ...
- 实现系统函数time,获取当前时间与UTC的间隔
因种种原因,最近很少上cnblogs了.刚写了一个实现time的函数,可以通过该函数获取当前时间与1970年1月1日 0时0分0秒的差值,精确到秒,可以用在某些没有时候使用time不正确而不得不调用硬 ...
- SqlAgent备份脚本
) ) set @dbname='emcp' set @back_path= 'e:\'+@dbname+'\'+@dbname ),) )) )) )) +'.bak' exec('use ['+@ ...
- MySQL重置密码(OSX)
1.停止MySQL的服务 sudo /Library/StartupItems/MySQLCOM/MySQLCOM stop 2.cd /usr/local/mysql/bin ./mysqld_ ...
- Java集合框架(JCF)之collention
一.概念:是为了实现某一目的或功能而预先提供的一系列封装好了的具有继承或实现的类与接口. 二.特点: 1.元素的类型可以不同 2.集合长度可变 3.空间不固定 三.collection与collec ...
- C#对HTML转译需要注意的问题
在做B/S程序时我们多少会用到一点HTML特殊符号转译. 如:“&”——>“&” , "<"——>"<" , " ...