题意:

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

  1. leetcode6:Zigzag Conversion@Python

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

  2. 【leetcode❤python】 6. ZigZag Conversion

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

  3. 64. ZigZag Conversion

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

  4. No.006 ZigZag Conversion

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

  5. leetcode第六题 ZigZag Conversion (java)

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

  6. leetcode题解 6.ZigZag Conversion

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

  7. 6.[leetcode] ZigZag Conversion

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

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

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

  9. LeetCode--No.006 ZigZag Conversion

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

随机推荐

  1. dom select选单

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. canvas脏域问题纪录

    canvas 脏域问题 今天无意之中碰见了一.问题描述: 在支持html5的浏览器中运行javascript脚本,脚本主要是操作网页上的标签canvas,出错的操作为, getImageData(im ...

  3. matlab 画平面

    y = :; z = ones(); surf(x,y,z):

  4. 一个效果很华丽的仿桌面APP,却胜似Launcher

    开发Android APP的同学是否对于Launcher实现的绚丽效果而痴迷呢?什么,连Android Launcher是什么都不知道.好吧,拿起侬的手机,在解锁后的首页界面上左右滑动滑动,体验体验, ...

  5. Clean Code第三章<函数>

    1.方法不要写太长,如果太长,抽取其中的逻辑到新的方法中 bad good 2.函数只做一件事 如果做了多件事,要在方法名里体现出来 3.每个函数一个抽象层级 4.函数名可以长一些,比长注释好 5.方 ...

  6. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  7. HDU 4610 Cards (合数分解,枚举)

    Cards Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  8. HDU 3923 Invoker(polya定理+逆元)

    Invoker Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 122768/62768 K (Java/Others)Total Su ...

  9. apiCode/1/1.1/1.1.1

    public abstract class myClass { private string id = ""; private string name = "" ...

  10. JVM启动参数小结

    一:JVM启动参数共分为三类:         其一是标准参数(-),所有的JVM实现都必须实现这些参数的功能,而且向后兼容:        其二是非标准参数(-X),指的是JVM底层的一些配置参数, ...