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的更多相关文章

  1. LeetCode 6. ZigZag Conversion & 字符串

    ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...

  2. Leetcode 6. ZigZag Conversion(找规律,水题)

    6. ZigZag Conversion Medium The string "PAYPALISHIRING" is written in a zigzag pattern on ...

  3. LeetCode 6 ZigZag Conversion 模拟 难度:0

    https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in ...

  4. LeetCode 6 ZigZag Conversion(规律)

    题目来源:https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is writt ...

  5. [LeetCode][Python]ZigZag Conversion

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/zigzag- ...

  6. 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]

    题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

  7. [LeetCode 题解]: ZigZag Conversion

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 The string ...

  8. [LeetCode] 6. ZigZag Conversion 之字型转换字符串

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  9. 【leetcode】ZigZag Conversion

    题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...

随机推荐

  1. HBase配置性能调优(转)

    因官方Book Performance Tuning部分章节没有按配置项进行索引,不能达到快速查阅的效果.所以我以配置项驱动,重新整理了原文,并补充一些自己的理解,如有错误,欢迎指正. 配置优化 zo ...

  2. 动态linq to list排序

    public class QeurySort { public static IList<T> Sort<T>(IList<T> list,string sidx, ...

  3. HDU 1789 Doing Homework again(贪心)

    Doing Homework again 这只是一道简单的贪心,但想不到的话,真的好难,我就想不到,最后还是看的题解 [题目链接]Doing Homework again [题目类型]贪心 & ...

  4. win下安装oracle的步骤

  5. 69 个经典 Spring 面试题和答案

    Spring 概述 什么是spring?Spring 是个java企业级应用的开源开发框架.Spring主要用来开发Java应用,但是有些扩展是针对构建J2EE平台的web应用.Spring 框架目标 ...

  6. Click模块化路由器

    [概述] Click是一种基于软件控制的模块化路由器.其架构可以大致视为一系列数据包处理模块(称为elements)组成的.一个Click路由器可以看成一张由elements作为顶点,数据包传递路径作 ...

  7. (Tree)94.Binary Tree Inorder Traversal

    /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * Tre ...

  8. Boot 44b0x by uboot

    1. Creat a branch from tag v2013.10-rc4 2. Build it: make B2 Install NFS service for Ubuntu 12.04 1. ...

  9. samba服务器搭建小记

    经常要在局域网的linux和windows主机之间共享文件,我遇到了当年samba作者同样的问题,既然人家已经写好了这个软件那就直接拿来用吧. 首先,在linux主机上执行 sudo apt-get ...

  10. AX7: HOW TO USE TABLE METHOD EXTENSION CLASS

    To create new methods on a table without customize you should use the Table method extension class. ...