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. Toward Scalable Systems for Big Data Analytics: A Technology Tutorial (I - III)

    ABSTRACT Recent technological advancement have led to a deluge of data from distinctive domains (e.g ...

  2. C#之属性

    在C#类中有属性这个成员,C#属性用来读写类的字段.实际上是通过get和set访问器实现的.

  3. git常用命令有用

    http://www.cnblogs.com/cspku/articles/Git_cmds.html

  4. 2-legged oauth & 3-legged oauth

    3-legged oauth resource owner, client, server. resource owner 给client访问权限去访问resource owner在server上的r ...

  5. PC缺少一个或多个网络协议 qq可登录(win10)

    打开适配器连接 1打开网络适配器   2卸载microsoft  3 网络客户端   4重启

  6. 收到远程通知,怎么区分是点击通知栏提醒进去的还是在foreground收到的通知?

    我现在是要区分点击通知栏的通知进入应用还是点击应用图标进入的,1,开始程序都是在后台.2,接受通知都是在foreground状态.applicationdidFinishLaunchWithOptio ...

  7. Sublime Text 配置

    Sublime Text 配置 1.键盘映射 映射成emacs的键盘方式: Preferences --> Key Bounding - user:然后复制如下配置信息(注意取消前缀“...-- ...

  8. Centos7.2 Systemd 方式编译 Mysql5.7.11

    导读 MySQL 5.7 版本的发布,也就是说从现在开始5.7已经可以在生产环境中使用,有任何问题官方都将立刻修复. MySQL 5.7主要特性: 原生支持Systemd 更好的性能:对于多核CPU. ...

  9. xmind的第七天笔记

  10. 【转】AngularJS 取消对 HTML 片段的转义

    今天尝试用 Rails 做后端提供 JSON 格式的数据, AngularJS 做前端处理 JSON 数据,其中碰到 AngularJS 获取的是一段 HTML 文本,如果直接使用 data-ng-b ...