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. 快速开发jQuery插件的10大技巧

    原文链接:http://wiki.itivy.com/?p=36 在开发过很多 jQuery 插件以后,我慢慢的摸索出了一套开发jQuery插件比较标准的结构和模式.这样我就可以 copy & ...

  2. Jmeter接口压测

    对于各个组件的使用,建议参考官方文档 1. Jmeter参数化,从txt文件读取. 1.txt

  3. python中将HTTP头部中的GMT时间转换成datetime时间格式

    原文: https://blog.csdn.net/zoulonglong/article/details/80585716 需求背景:目前在做接口的自动化测试平台,由于接口用例执行后返回的结果中的时 ...

  4. securecrt8注册码

    securecrt8注册码,两个可用 Name:meisiCompany:TEAM ZWTSerial Number:03-14-367662License Key:ACCFAX R9FHJ7 QZV ...

  5. How to Use vcpkg On Windows

    Introduction If you do any sort of C++ development on Windows, then you know that library/package ma ...

  6. Spark运行模式概述

    Spark编程模型的回顾 spark编程模型几大要素 RDD的五大特征 Application program的组成 运行流程概述 具体流程(以standalone模式为例) 任务调度 DAGSche ...

  7. a标签解析url

    var url = 'http://127.0.0.1:8080/index.jsp?username=admin#name'; var aLink = document.createElement( ...

  8. snmp简单识记

    免费snmp探测 http://webluker.com/webtools/snmp snmp简单网络管理协议(simple network management protocol)前身时sgmp简单 ...

  9. HTML5 Canvas ( 事件交互, 点击事件为例 ) isPointInPath

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. springMVC之Interceptor拦截器

    转自:https://blog.csdn.net/qq_25673113/article/details/79153547 Interceptor拦截器用于拦截Controller层接口,表现形式有点 ...