题意:

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. pku3659 Cell Phone Network

    http://poj.org/problem?id=3659 树状DP,树的最小点覆盖 #include <stdio.h> #include <vector> #define ...

  2. mac机器下远程仓库添加完毕之后,却无法上传应有的内容。

    Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you ha ...

  3. redis缓存数据表

    直观上看,数据库中的数据都是按表存储的:更微观地看,这些表都是按行存储的.每执行一 次select查询,数据库都会返回一个结果集,这个结果集由若干行组成.所以,一个自然而然 的想法就是在Redis中找 ...

  4. easyui反选全选和全不选代码以及方法的使用

    首先要说明的是,onclick="javascript:这里能写方法的名字,也能写一段JS的代码,但是方法名字要带括号.",其次就是onclick=“这里写的方法名必须存在于本页面 ...

  5. Codeforces 711 C. Coloring Trees (dp)

    题目链接:http://codeforces.com/problemset/problem/711/C 给你n棵树,m种颜色,k是指定最后的完美值.接下来一行n个数 表示1~n树原本的颜色,0的话就是 ...

  6. How Tomcat Works(十)

    本文接下来分析tomcat的日志记录器,日志记录器是用来记录消息的组件,在tomcat中,日志记录器需要与某个servlet容器相关连:在org.apache.catalina.logger包下,to ...

  7. mysql 字段操作

    1.添加字段 ALTER TABLE lucky_user ADD COLUMN id_type TINYINT NOT NULL DEFAULT '0' COMMENT "0: 普通用户, ...

  8. OC:点语法

    IOS中的@property 与 assign copy retain 的区别参考 //@理解为 OC 代码的标记 //如何去创建一个对象 创建对象的两步: // (1)为对象在堆区中开辟空间 Stu ...

  9. iOS 3DES DES AES加密注意事项!!很重要,否则会加密失败

    今天做项目,需要进行3des加密. 加密的gkey:abcdefgh   giv:(偏移量)abcdefgh 加密后结果:p+X985x5bFS6dWjAnm6sdQ== 下面是代码: +(NSStr ...

  10. SCCM2007

    Active Directory系统组发现:此方法按照上次运行发现方法时 Active Directory 中的响应返回对象,可发现活动目录OU.全局组.通用组.嵌套组.非安全组. Active Di ...