【Leet Code】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从头到尾按下面的方式排放(以四行为例):

然后再以{‘1’‘7’‘13’} {‘2’‘6’‘8‘’12‘} {’3‘’5‘’9‘’11‘} {’4‘’10‘},给定的行数已经在传递参数的时候确定了,我们要分开处理每一行;

注意看两个纵列{1,2,3,4}和{7,8,9,10},相差刚好为 2 * nRows - 2;

(而除了第一行和最后一行,其他行中间还隔着一个字符,而这个与字符的位置刚好相差 (nRows  - i - 1) * 2 , i 是当前行<0开始>)

再加入越界的判断,我们可以得到下面算法:

class Solution {
public:
    string convert(string s, int nRows)
    {
        if( nRows <= 1 )
        {
            return s;
        }
        string ret{};
        int len = s.length();
        int add = 2 * nRows - 2;
        for( int i = 0; i < nRows; i++ )
        {
            for(int j = i; j < len; j+=add)
            {
                ret += s[j];
                if (i != 0 && i != nRows - 1 && j + (nRows-i-1) * 2 < s.length())
                {
                    ret += s[j + (nRows-i-1) * 2];
                }
            }
        }
        return ret;
    }
};
时间: 2024-08-01 21:41:21

【Leet Code】ZigZag Conversion的相关文章

【Leet Code】Palindrome Number

Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an integer is a palindrome. Do this without extra space. 判断一个数整数是不是回文?例如121,1221就是回文,好吧,直接利用前面写过的[Leet Code]Reverse Integer--"%"你真的懂吗? 不过这里要考虑翻转后,数值

【Leet Code】Median of Two Sorted Arrays

Median of Two Sorted Arrays Total Accepted: 17932 Total Submissions: 103927My Submissions There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n

【Leet Code】Longest Substring Without Repeating Characters

Longest Substring Without Repeating Characters Total Accepted: 20506 Total Submissions: 92223My Submissions Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating le

【Leet Code】Add Two Numbers

Add Two Numbers Total Accepted: 20255 Total Submissions: 88115My Submissions You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numb

【Leet Code】Reverse Integer——“%”你真的懂吗?

Reverse Integer Total Accepted: 27372 Total Submissions: 68133My Submissions Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 题目咋看起来很简单,其实,真的很简单: class Solution { public: int reverse(int x) { int ret = 0; bo

【Leet Code】String to Integer (atoi) ——常考类型题

String to Integer (atoi) Total Accepted: 15482 Total Submissions: 106043My Submissions Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yours

【Leet Code】Longest Palindromic Substring ——传说中的Manacher算法

Longest Palindromic Substring Total Accepted: 17474 Total Submissions: 84472My Submissions Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindr

【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 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:  "PAHNAPLSI

【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 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: "PAHNA