【LeetCode】6. ZigZag Conversion

原创 2018年04月15日 16:11:59

Description:

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

题目说给定一个字符串s和数字numRows,s是几个单词组成,numRows是单词个数。s是由这几个单词按照Z字形的书写规则组成的,我们要做的就是根据给定的s和numRows恢复出正常的从左到右的单词书写顺序。


思路:

设置一个string类型的数组words,元素个数为numRows。我们从words[0]开始到words[numRows-1]下标递增规定为正方向,下标递减规定为反方向。

1.从words[0]当words[numRows-1]我们依次填写s中的字符,

2.然后再从words[Rows-1]到words[0]依次填写s中的字符。

3.不断重复1和2,直到全部填写完s中的字符。

class Solution {
public:
    string convert(string s, int numRows) {
        if(numRows==1)
            return s;
        string words[numRows];
        string res;
        int count=0;
        bool forward=true,reverse=false;
        
        for(int i=0;i<s.length();i++){
            if(forward){
                words[count]=words[count]+""+s[i];//此处为words[count] += ""+s[i]时有问题,如果有人知道请指教一下
                count++;
                if(count==numRows){
                    count-=2;
                    forward=false;
                    reverse=true;
                }
            }
            else if(reverse){
                words[count]=words[count]+""+s[i];
                count--;
                if(count<0){
                    count+=2;
                    reverse=false;
                    forward=true;
                }
            }
                        
        }
         for(int j=0;j<numRows;j++){
             res+=words[j];
         }
        return res;
    }
};

Leetcode_6_ZigZag Conversion

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41408021 看完这篇文章,你可能会学到到知识如下: ...
  • pistolove
  • pistolove
  • 2014-11-23 09:45:55
  • 2782

[Java]leetcode6 ZigZag Conversion

[Java]leetcode ZigZag Conversion
  • fumier
  • fumier
  • 2015-04-12 11:17:26
  • 690

6. ZigZag Conversion Leetcode Python

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you...
  • hyperbolechi
  • hyperbolechi
  • 2015-01-28 02:21:14
  • 1031

[Leetcode]6. ZigZag Conversion @python

题目The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (y...
  • qian2729
  • qian2729
  • 2016-01-13 00:32:16
  • 844

LeetCode 6 ZigZag Conversion (C,C++,Java,Python)

Problem: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l...
  • runningtortoises
  • runningtortoises
  • 2015-05-06 22:55:25
  • 1140

【python】【leetcode】【算法题目6—ZigZag Conversion】

一、题目描述 题目原文:   The string "PAYPALISHIRING" is written in a zigzag pattern on a given number...
  • u014615155
  • u014615155
  • 2016-11-26 15:44:31
  • 478

leetcode 6 ZigZag Conversion C++

这道题最大的难点在理解题意。。。 它是一个折线图样子的。 string convert(string s, int numRows) { string result = ""; ...
  • a2331046
  • a2331046
  • 2016-06-28 10:52:31
  • 247

[LeetCode][6]ZigZag Conversion解析与特殊解法 -Java实现

Q: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this...
  • u014629433
  • u014629433
  • 2016-06-06 14:29:53
  • 2204

【LeetCode】LeetCode——第6题:ZigZag Conversion

【LeetCode】LeetCode——第6题:ZigZag Conversion
  • hujingshuang
  • hujingshuang
  • 2016-04-19 10:09:57
  • 1572

LeetCode-6. ZigZag Conversion (JAVA)(之字形转换)

LeetCode-6. ZigZag Conversion (JAVA)(之字形转换)
  • mine_song
  • mine_song
  • 2017-04-17 09:41:03
  • 441
收藏助手
不良信息举报
您举报文章:【LeetCode】6. ZigZag Conversion
举报原因:
原因补充:

(最多只允许输入30个字)