关闭

6. ZigZag Conversion

标签: string
146人阅读 评论(0) 收藏 举报
分类:

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

class Solution {
public:
    string convert(string s, int r) {
        if(r == 1) return s;
        string res;
        int n = s.size();
        for(int i = 0; i < r; i++){
            int start = i, interval1 , interval2;
            int gap = 2 * r - 2;
            if(i == 0 || i == r - 1) interval1 = interval2 = gap;
            else{
                interval1 = gap - 2 * i;
                interval2 = gap - interval1;
                
            }
            int k = 0;
            while(1){
                int pos = start + interval1 * ((k + 1) / 2) + interval2 * (k / 2);
                if( pos < n){
                    res.push_back(s[pos]);
                    k++;
                }
                else
                    break;
            }
        }
        return res;
    }
};
一种很聪明的解法

class Solution {
public:
    string convert(string s, int numRows) {
        vector<string> vs(numRows, "");
        int n = s.length(), i = 0;
        while (i < n) {
            for (int j = 0; j < numRows && i < n; j++)
                vs[j].push_back(s[i++]);
            for (int j = numRows - 2; j >= 1 && i < n; j--)
                vs[j].push_back(s[i++]);
        }
        string zigzag;
        for (string v : vs) zigzag += v;
        return zigzag;
    } 
};


0
0
查看评论

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

Problem: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to d...
  • runningtortoises
  • runningtortoises
  • 2015-05-06 22:55
  • 1070

Leetcode_6_ZigZag Conversion

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41408021 看完这篇文章,你可能会学到到知识如下: (1)对于给定的算法题,如何正确对其进行分析,而不是通过穷举的方式来得到答案。 (2)当解题逻辑非...
  • pistolove
  • pistolove
  • 2014-11-23 09:45
  • 2558

leetcode 6 ZigZag Conversion C++

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

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

LeetCode-6. ZigZag Conversion (JAVA)(之字形转换)
  • mine_song
  • mine_song
  • 2017-04-17 09:41
  • 201

ZigZag Conversion (C实现)

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 f...
  • u014056175
  • u014056175
  • 2015-09-08 14:40
  • 320

[Java]leetcode6 ZigZag Conversion

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

[Leetcode]6. ZigZag Conversion @python

题目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 fon...
  • qian2729
  • qian2729
  • 2016-01-13 00:32
  • 710

【LeetCode】ZigZag Conversion 解题报告

【题目】 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display t...
  • ljiabin
  • ljiabin
  • 2014-10-26 16:49
  • 22424

【小熊刷题】ZigZag Conversion <Leetcode 6, Java>

QuestionThe string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows return read row by row.
  • huilaner
  • huilaner
  • 2015-09-11 03:29
  • 305

leetcode6-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 pa...
  • lp2hsf
  • lp2hsf
  • 2014-07-17 20:34
  • 318
    个人资料
    • 访问:15172次
    • 积分:1105
    • 等级:
    • 排名:千里之外
    • 原创:99篇
    • 转载:5篇
    • 译文:0篇
    • 评论:0条