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 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 convert(string s, int nRows) {
    if (nRows <= 1) {
        return s;
    }
    string *temps = new string[nRows];
    bool longmod = true;
    int row = 0;
    for (int i = 0; i < s.size(); ++i) {
        if (longmod) {
            temps[row].append(s.substr(i, 1));
            row++;
            if (row == nRows-1) {
                longmod = false;
            }
        } else {

            temps[row].append(s.substr(i, 1));
            row--;
            if (row == 0) {
                longmod = true;
            }
        }

    }
    string rets;
    for (int i = 0; i < nRows; ++i) {
        rets.append(temps[i]);
    }
    delete []temps;
    return rets;
}
时间: 2024-10-02 18:08:56

LeetCode6——ZigZag Conversion的相关文章

[Java]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 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: "PAHNAPLSII

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 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: "PAHN

No.006 ZigZag Conversion

6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy 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 bette

LeetCode ZigZag Conversion(将字符串排成z字型)

1 class Solution { 2 public: 3 string convert(string s, int nRows) { 4 string a=""; 5 int len=s.length(); 6 if(len<=nRows||nRows==1) return s; //只有n个,不足一个周期||排成一行 7 int teams=len/(nRows*2-2); //teams个完整的周期 8 int rest=len%(nRows*2-2); //最后一个不足

leetcode -day16 ZigZag Conversion

1.  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 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 b

【LeetCode】ZigZag Conversion

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

LeetCode【6】. ZigZag Conversion --思路图解与java实现

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

64. ZigZag Conversion

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

6. ZigZag Conversion

/* * 6. ZigZag Conversion * 2016-8-26 by Mingyang * 觉得这个题目面试的必要性不大,有规律,第一行和最后一行每两个相差2n-2 * 中间的每行除了2n-2有外,中间的一个也有2 * (n - 1 - i) */ public String convert(String s, int numRows) { if (numRows == 1) return s; StringBuilder sb = new StringBuilder(); // s