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. 安装了VS2010 sp1 后再安装ASP.NET MVC 3.0的问题

    安装了VS2010 sp1 后再安装ASP.NET MVC 3.0的问题(Final Result: Installation failed with error code: (0x80070643) ...

  2. Model2模型介绍

    在JSP课程中有 Model1 模型的介绍 模型二: 实例接JSP课程,先去看JSP课程了

  3. python 程序构架

    http://blog.csdn.net/heyabo/article/details/8806176

  4. 探索javascript----获得节点计算后样式

    节点计算后样式是一个属性与属性值的值对对象: IE:    node.currentStyle; 非IE: window.getComputedStyle(node,null); 兼容方式: func ...

  5. tomcat一闪而过------Java EE环境部署

    今天浪费了一个多钟头,tomcat一直一闪而过,最终原因让人哭笑不得,最后发现自己下载的是tomcat的源码版本....哎 部署环境步骤: 1.安装JDK 下载安装,JDK只需要配以下两个环境变量就可 ...

  6. java学习第十一天

    第十二次课 目标 一维数组(创建访问) 一.概念与特点 1.概念 相同数据类型的有序集合[] 数组名: 容器的名字 元素:  下标变量,数组名[下标] 长度:  length 下标:   位置.索引  ...

  7. ios项目接入sdk事项

    使用cocos2d-x引擎创建的项目在xcode里可以看到都带有一个ios目录,把要接入的sdk的包含.framework库文件和.bundle的资源文件的父目录拖入到xcode项目里的这个ios目录 ...

  8. A Brief Introduction to Markovs Chains

    本文译自A Brief Introduction to Markovs Chains 译者按: 前面一篇文章讲的是蒙特卡洛积分,也就是通过生成符合特定分布的随机变量来近似计算积分值,例如: \(E = ...

  9. PHP curl传 json字符串

    $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_seto ...

  10. 信号处理基础概念比较----频谱vs功率谱vs能谱

    频谱: 对动态信号在频率域内进行分析,分析的结果是以频率为坐标的各种物理量的谱线和曲线,可得到各种幅值以频率为变量的频谱函数F(ω).频谱是个很不严格的东西,常常指信号的Fourier变换.频谱分析中 ...