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".

思路:

方法1: 刚开始正着方三个,然后反着放两个,正着放两个。直到结束。(优)

class Solution {
public:
string convert(string s, int nRows) {
if(nRows <= 1 || s == "") return s;
int len = s.length();
string s2[nRows];
int k = 0, k2 = 0;
while(k2 < len){
while(k < nRows && k2 < len) s2[k++].push_back(s[k2++]);
k--;
while(k > 0 && k2 < len) s2[--k].push_back(s[k2++]);
k++;
}
string s3;
for(int i = 0; i < nRows; ++i) {
s3 += s2[i];
}
return s3;
}
};

方法二: 类似方法一,不过存下每个位置的字符应该放的地方。然后依次读入。

class Solution {
public:
string convert(string s, int nRows) {
if(nRows <= 1 || s == "") return s;
int len = s.length();
vector<int> index(2*nRows-2);
int t = 0;
for(int i = 0; i < nRows; ++i) index[t++] = i;
for(int j = nRows-2; j > 0; --j) index[t++] = j;
string s2[nRows];
int k = 0, m = 2*(nRows-1);
while(k < len){
s2[index[k % m]].push_back(s[k]);
k++;
}
string s3;
for(int i = 0; i < nRows; ++i) {
s3 += s2[i];
}
return s3;
}
};

64. ZigZag Conversion的更多相关文章

  1. 【leetcode❤python】 6. ZigZag Conversion

    #-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object):    def convert(self, s, numRow ...

  2. No.006 ZigZag Conversion

    6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...

  3. leetcode第六题 ZigZag Conversion (java)

    ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...

  4. leetcode题解 6.ZigZag Conversion

    6.ZigZag Conversion 题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a gi ...

  5. 6.[leetcode] ZigZag Conversion

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

  6. 字符串按照Z旋转90度然后上下翻转的字形按行输出字符串--ZigZag Conversion

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

  7. LeetCode--No.006 ZigZag Conversion

    6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...

  8. leetcode-algorithms-6 ZigZag Conversion

    leetcode-algorithms-6 ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag ...

  9. LeetCode 6. ZigZag Conversion & 字符串

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

随机推荐

  1. HDU 1171 背包

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  2. SSH框架之一详解maven搭建多模块项目

    闲来无事,思量着自己搭建一个ssh框架,一来回顾熟悉一下ssh的内容,hibernate还就没用过了,生疏了都.二来整合一下,将其他掌握的和正在学习的框架核技术糅合到一起,就当是做一个demo练手了. ...

  3. 正则表达式学习与python中的应用

    目录: 一.正则表达式的特殊符号 二.几种重要的正则表达式 三.python的re模块应用 四.参考文献 一.正则表达式的特殊符号 特殊符号可以说是正则表达式的关键,掌握并且可以灵活运用重要的pyth ...

  4. Ubuntu软件中心打不开,Encountered a section with no Package: header错误之解决

    sudo rm /var/lib/apt/lists/* -vf sudo apt-get update

  5. Zyxel Switch-How to block a fake DHCP server without enabling DHCP snooping?

    How to block a fake DHCP server without enabling DHCP snooping? Scenario How to block a fake DHCP se ...

  6. oracle之sqlplus讲解

    这里要解释的sqlplus有2方面内容:sqlplus登陆命令和sql*plus工具命令. [sqlplus登陆命令] 常用的登陆命令有: sqlplus /nolog 登陆到sqlplus,还未登录 ...

  7. Java配置环境变量

    首先,你应该已经安装了Java 的 JDK 了,笔者安装的是:jdk-7u7-windows-x64 接下来主要讲怎么配置 Java 的环境变量 1.进入“计算机”的“属性”选项后如图 2.选择“高级 ...

  8. Opencv创建有滚动条的视频

    #include "stdafx.h"#include "cv.h"#include "cxcore.h"#include "hi ...

  9. 《C与指针》第一章练习

    本章例程 程序1.1 重排字符 #include <stdio.h> #include <stdlib.h> #include <string.h> #define ...

  10. 区间更新 zoj3911

    哎,没什么坑点,一个简单的区间更新题,但是改了好几天没改对,最终还是过了~~发个纪念下 泪奔... #include<cstdio>#include <iostream>#in ...