leetcode 6. ZigZag Conversion
https://leetcode.com/problems/zigzag-conversion/
题目: 将字符串转化成zigzag模式。
例如 "abcdefghijkmlnpq"
当为4行,zigzag模式为:
a g l b f h m n c e i k p d j q 输出为aglbfhmnceikpdjq 当为5行,zigzag模式为: a i b h j q c g k p d f l n e m 输出为aibhjqcgkpdflnem思路:以题目中当行为4的为例,将字符串"abcdefghijkmlnpq" 首先转换为"abcd/fe/ghij/km/lnpq".其实/代表空格。这样最后的输出就从以span=4,从j=0开始a/g/l. 再从j=1开始bfhmn,...,一直到j=4,d/j/q.其中去掉/。
所以首先需要将原字符串转换为用空格填充的字符串。从上面的例子可以看出规则。首先是numRows个字符为一列,然后numRows-2个字符为反序的一列,其上下各有一个空格。
class Solution {
public:
string convert(string s, int numRows) {
int i=,k=,j;
int len=s.length();
string ss="";
int flag=;
while(i<len){
j=numRows-flag;
if(flag==){
while(i<len&&j>&&j--){
ss+=s[i++];
}
while(j>&&j--){
ss+=' ';
}
flag=;
}
else {
ss+=' ';
string temp="";
while(i<len&&j>&&j--){
temp+=s[i++];
}
std::reverse(temp.begin(),temp.end());
26
while(j>&&j--){
ss+=' ';
}
ss+=temp;
ss+=' ';
flag=;
}
}
string s1="";
k=ss.length();
for(i=;i<numRows;i++){
j=i;
while(j<k){
if(ss[j]!=' ')
s1+=ss[j];
j+=numRows;
}
}
return s1;
}
};
leetcode上运行时间为 24ms
leetcode 6. ZigZag Conversion的更多相关文章
- LeetCode 6. ZigZag Conversion & 字符串
ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...
- Leetcode 6. ZigZag Conversion(找规律,水题)
6. ZigZag Conversion Medium The string "PAYPALISHIRING" is written in a zigzag pattern on ...
- LeetCode 6 ZigZag Conversion 模拟 难度:0
https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in ...
- LeetCode 6 ZigZag Conversion(规律)
题目来源:https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is writt ...
- [LeetCode][Python]ZigZag Conversion
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/zigzag- ...
- 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- [LeetCode 题解]: ZigZag Conversion
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 The string ...
- [LeetCode] 6. ZigZag Conversion 之字型转换字符串
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【leetcode】ZigZag Conversion
题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...
随机推荐
- Arrays和Collection之间的转换
1.将数组转换成固定大小的列表: public static <T> List<T> asList(T... a)参数 T... a:数组元素 返回值 List<T> ...
- Python使用struct处理二进制
有的时候需要用python处理二进制数据,比如,存取文件,socket操作时.这时候,可以使用python的struct模块来完成.可以用 struct来处理c语言中的结构体. struct模块中最重 ...
- [MongDB] 主从架构--官方极力不推荐
一.缘由: 看着数据库大家庭都有主从模式,想着Mongodb应该也不会落下.但从官网看来,先是早先舍弃了Master-Master模式,现在又在不推荐 Master-Slave模式,这是要标新立异呀. ...
- sql server 跨数据库插入数据
创建服务器的连接,创建好后可以存在服务器上,可以在不同位置重复使用,和系统函数类似 exec sp_addlinkedserver 'RemoteServer', '', 'SQLOLEDB ', ' ...
- VS2010统计代码行数 [转]
按CTRL+SHIFT+F (Find in files),勾上支持正则表达式,然后输入搜索内容: ^:b*[^:b#/]+.*$ 以上表达式的统计可做到:#开头 和 /开头 或者 空行 都不计入代 ...
- store操作
store.remove(rs); store.sync({ success: function (e, opt) { this.store.commitChanges(); }, failure: ...
- 常用SQL Server日期格式化
Select CONVERT(varchar(100), GETDATE(), 8): 10:57:46 Select CONVERT(varchar(100), GETDATE(), 24): 10 ...
- CuteSTL——跟着感觉造轮子
置顶推荐: CuteSTL:https://github.com/jxd134/algorithm/tree/master/CuteSTL TinySTL:https://github.com/zou ...
- 关于Lucene.net 中高亮显示关键词的深究
这几天一直在学习lucene,也写了3篇自己总结的知识点,本以为很容易上手的东西,但是却遇到了一个很棘手的问题,借此,希望可以跟大家探讨一下 问题:使用盘古高亮显示组件后,如搜索“mp3 player ...
- Genymotion出现virtualbox cannot start the virtual device错误
选择你要启动的device右侧的设置 打开如下界面 将Processor设置为1 (默认为4)