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 ...
随机推荐
- 【java】之 apache commons-codec 与Apache Digest demo实例,支持md5 sha1 base64 hmac urlencode
使用commons-codec 进行加密的一些操作 package com.jiepu.ApacheDigest; import java.io.FileInputStream; import org ...
- 如何在Ubuntu下的VirtualBox虚拟机(Windows XP)里挂载/使用U盘 (转载)
文章来源:http://www.codelast.com/ 在Ubuntu下安装了VirtualBox之后,如果你的虚拟机安装的是Windows XP系统,那么,你会发现,当你插上U盘时,无论你怎么折 ...
- iOS开发项目之MVC与MVVM
MVC MVC,Model-View-Controller,我们从这个古老而经典的设计模式入手.采用 MVC 这个架构的最大的优点在于其概念简单,易于理解,几乎任何一个程序员都会有所了解,几乎每一所计 ...
- shell中bc expr [ ] (( ))的使用方法
http://blog.chinaunix.net/uid-20671208-id-3552751.html
- Java 如何将String转化为Int
在 Java 中要将 String 类型转化为 int 类型时,需要使用 Integer 类中的 parseInt() 方法或者 valueOf() 方法进行转换. 例1: 1 2 3 4 5 6 S ...
- selenium 关于富文本的处理
http://www.cnblogs.com/xiaobaichuangtianxia/p/5889999.html
- [转载] COM 套间
http://www.vckbase.com/index.php/wv/1315 简序 大学毕业前的最后一学期,在一家公司实习,当时的工作需要用到一些操作系统提供的组件.那时候只知道COM这个名词,并 ...
- html5手写签名
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta co ...
- 关于jq+devexpress基础知识总结(随便的基础)
//获取某行某列的值 onSelectionChanged: function (selectedItems) { ]; if (data != null) postionno = data.POST ...
- STL之序列容器vector
首先来看看vector的模板声明: template <class T, class Alloc = allocator<T>> class vector { //… }; v ...