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. 用Keras搭建神经网络 简单模版(三)—— CNN 卷积神经网络(手写数字图片识别)

    # -*- coding: utf-8 -*- import numpy as np np.random.seed(1337) #for reproducibility再现性 from keras.d ...

  2. [转]CSKIN 作者分享的图片处理类

    本代码来自:http://bbs.cskin.net/forum.php?mod=viewthread&tid=113&fromuid=2446 里面没有我想找的任意角度旋转的方法,代 ...

  3. 新生代老年代GC组合

    新生代通常存活时间较短,因此基于Copying算法来进行回收,所谓Copying算法就是扫描出存活的对象,并复制到一块新的完全未使用的空间中 在执行机制上JVM提供了串行GC(SerialGC).并行 ...

  4. Unreal Engine 4 性能优化工具(Profiler Tool)

    转自:http://aigo.iteye.com/blog/2296548 Profiler Tool Reference https://docs.unrealengine.com/latest/I ...

  5. final修饰的类有什么特点

    变量定义为final,一旦被初始化便不可改变,这里不可改变的意思对基本类型来说是其值不可变,而对于对象变量来说其引用不可再变. 方法定义为final,是为了防止任何继承类改变它. 类定义为final, ...

  6. 学大伟业Day1解题报告

    学大伟业Day1解题报告 张炳琪 一.   时间分配 T1:30分钟  T2: 60分钟  T3:100分钟 二.答题情况及错因 T1:100         T2:55             T3 ...

  7. CSS滚动条样式设置

    webkit浏览器css设置滚动条 主要有下面7个属性 ::-webkit-scrollbar 滚动条整体部分,可以设置宽度啥的 ::-webkit-scrollbar-button 滚动条两端的按钮 ...

  8. Python生态圈

    WEB开发——最火的Python web框架Django, 支持异步高并发的Tornado框架,短小精悍的flask,bottle, Django官方的标语把Django定义为the framewor ...

  9. sts安装出现could not find jar:file解决办法

    转自:https://blog.csdn.net/weixin_43702329/article/details/84823912 标题sts插件下载好但是安装出错 我的eclipse是4.5.2,在 ...

  10. J2SE 8的Lambda --- Comparator

    Person[] personArray = new Person[]{new Person("Tom"),new Person("Jack"),new Per ...