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

思路:图的遍历,先纵向,再横向。

图用string数组存储。

如果nRows=4,注意短的那列是倒序。

P      I      N

A  L  S  I  G

Y  A  H  R

P      I

如果nRows=2,全部是正序。

P  Y  ...

A  P  ...

class Solution {
public:
string convert(string s, int numRows) {
string sArray[numRows];
int pChar = ; //pointer to s
int pRow = ; //pointer to indicate row number while(){
pRow = ;
while(pRow < numRows && pChar != s.length()){ //traverse first colomn
sArray[pRow++] += s[pChar++];
}
if(pChar == s.length()) return getReturnStr(sArray,numRows); pRow = numRows-;
while(pRow > && pChar != s.length()){
sArray[pRow--] += s[pChar++];
}
if(pChar == s.length()) return getReturnStr(sArray,numRows);
}
}
string getReturnStr(string* sArray, int numRows){
string ret = "";
for(int i = ; i < numRows; i++){
ret += sArray[i];
}
return ret;
}
};

6.ZigZag Conversion(Graph, traverse)的更多相关文章

  1. 【leetcode❤python】 6. ZigZag Conversion

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

  2. 64. ZigZag Conversion

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

  3. No.006 ZigZag Conversion

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

  4. leetcode第六题 ZigZag Conversion (java)

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

  5. leetcode题解 6.ZigZag Conversion

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

  6. 6.[leetcode] ZigZag Conversion

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

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

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

  8. LeetCode--No.006 ZigZag Conversion

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

  9. leetcode-algorithms-6 ZigZag Conversion

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

随机推荐

  1. poj 3046 Ant Counting(多重集组合数)

    Ant Counting Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total ...

  2. [转]NSIS:常量大全

    原文链接 http://www.flighty.cn/html/bushu/20140915_251.html ;轻狂志www.flighty.cn ;运行后会在桌面生成NSIS常量大全.txt文件 ...

  3. 1005 Spell It Right (20 分)

    1005 Spell It Right (20 分) Given a non-negative integer N, your task is to compute the sum of all th ...

  4. pandas的set_index和reset_index方法

    import pandas as pd data = pd.DataFrame(np.arange(1,10).reshape(3,3),index=["a","b&qu ...

  5. BPM与ESB

    BPM:业务流程管理  --监控处理流程的轨迹以及处理过程 开源:JBPM 场景: 1.单一系统的协同工作比如审批流程,请假流程 2.多个系统的集成,复用各个子系统,构建新的处理流程(流程的优化与流程 ...

  6. 伯克利、OpenAI等提出基于模型的元策略优化强化学习

    基于模型的强化学习方法数据效率高,前景可观.本文提出了一种基于模型的元策略强化学习方法,实践证明,该方法比以前基于模型的方法更能够应对模型缺陷,还能取得与无模型方法相近的性能. 引言 强化学习领域近期 ...

  7. css (具体代码看笔记本)

    参考:https://www.cnblogs.com/liwenzhou/p/7999532.html  1. CSS语法   选择器 {属性1:值1;...;}  2. CSS导入方式   1. 行 ...

  8. Bogart SysPwd.vb

    Module syspwd Public Const STR_MASK = "MyFunction" '加密用字串 '預定義密碼長度 Public GintCheckPwd As ...

  9. idea-activate code

    N757JE0KCT-eyJsaWNlbnNlSWQiOiJONzU3SkUwS0NUIiwibGljZW5zZWVOYW1lIjoid3UgYW5qdW4iLCJhc3NpZ25lZU5hbWUiO ...

  10. 马尔可夫毯(Markov Blanket)

    马尔可夫毯(Markov Blanket) 最近接触到马尔可夫毯(MarkovBlanket)这个概念,发现网上资料不多,通俗易懂的解释甚少,查了一些资料后,决定写一个总结. 提到马尔可夫毯,就会有一 ...