solution.md 7.3 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1
# Z 字形变换
每日一练社区's avatar
每日一练社区 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
<div class="notranslate">
    <p>将一个给定字符串 <code>s</code> 根据给定的行数 <code>numRows</code> ,以从上往下、从左到右进行&nbsp;Z 字形排列。</p>

    <p>比如输入字符串为 <code>"PAYPALISHIRING"</code>&nbsp;行数为 <code>3</code> 时,排列如下:</p>

    <pre>
    P   A   H   N
    A P L S I I G
    Y   I   R</pre>

    <p>之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:<code>"PAHNAPLSIIGYIR"</code>。</p>

    <p>请你实现这个将字符串进行指定行数变换的函数:</p>

    <pre>string convert(string s, int numRows);</pre>

    <p>&nbsp;</p>

    <p><strong>示例 1:</strong></p>

    <pre><strong>输入:</strong>s = "PAYPALISHIRING", numRows = 3
<strong><br />输出:</strong>"PAHNAPLSIIGYIR"
</pre>
    <strong>示例 2:</strong>

    <pre><strong>输入:</strong>s = "PAYPALISHIRING", numRows = 4
<strong><br />输出:</strong>"PINALSIGYAHRPI"
<strong><br />解释:</strong>
P     I    N
A   L S  I G
Y A   H R
P     I
</pre>

    <p><strong>示例 3:</strong></p>

    <pre><strong>输入:</strong>s = "A", numRows = 1
<strong><br />输出:</strong>"A"
</pre>

    <p>&nbsp;</p>

    <p><strong>提示:</strong></p>

    <ul>
        <li><code>1 &lt;= s.length &lt;= 1000</code></li>
        <li><code>s</code> 由英文字母(小写和大写)、<code>','</code> 和 <code>'.'</code> 组成</li>
        <li><code>1 &lt;= numRows &lt;= 1000</code></li>
    </ul>
</div>
每日一练社区's avatar
每日一练社区 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
<p><div class="notranslate">
    <p>将一个给定字符串 <code>s</code> 根据给定的行数 <code>numRows</code> ,以从上往下、从左到右进行&nbsp;Z 字形排列。</p>

    <p>比如输入字符串为 <code>"PAYPALISHIRING"</code>&nbsp;行数为 <code>3</code> 时,排列如下:</p>

    <pre>
    P   A   H   N
    A P L S I I G
    Y   I   R</pre>

    <p>之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:<code>"PAHNAPLSIIGYIR"</code>。</p>

    <p>请你实现这个将字符串进行指定行数变换的函数:</p>

    <pre>string convert(string s, int numRows);</pre>

    <p>&nbsp;</p>

    <p><strong>示例 1:</strong></p>

    <pre><strong>输入:</strong>s = "PAYPALISHIRING", numRows = 3
<strong><br />输出:</strong>"PAHNAPLSIIGYIR"
</pre>
    <strong>示例 2:</strong>

    <pre><strong>输入:</strong>s = "PAYPALISHIRING", numRows = 4
<strong><br />输出:</strong>"PINALSIGYAHRPI"
<strong><br />解释:</strong>
P     I    N
A   L S  I G
Y A   H R
P     I
</pre>

    <p><strong>示例 3:</strong></p>

    <pre><strong>输入:</strong>s = "A", numRows = 1
<strong><br />输出:</strong>"A"
</pre>

    <p>&nbsp;</p>

    <p><strong>提示:</strong></p>

    <ul>
        <li><code>1 &lt;= s.length &lt;= 1000</code></li>
        <li><code>s</code> 由英文字母(小写和大写)、<code>','</code> 和 <code>'.'</code> 组成</li>
        <li><code>1 &lt;= numRows &lt;= 1000</code></li>
    </ul>
</div>
<p>以下错误的选项是?</p></p>
每日一练社区's avatar
每日一练社区 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
    Solution test;

    string ret;
    string s = "LEETCODEISHIRING";
    int numrows = 3;
    ret = test.convert(s, numrows);
    cout << ret << endl;
    return 0;
}

```

## 答案
```cpp
class Solution
{
public:
    string convert(string s, int numRows)
    {
        string result;
        int num1 = numRows * 2 - 2;
        if (s.size() < 2 || num1 == 0)
        {
            result = s;
            return result;
        }
        int num = (s.size() / num1);
        string str[100];
        int count = 0;
        int i = 0;
        for (int j = 0; j < s.size(); j++)
        {
            if (count == num1)
            {
                i++;
                count = 0;
            }
            str[i].push_back(s[j]);
        }

        for (int n = 0; n < num; n++)
        {
            result.push_back(str[n][0]);
        }
        for (int m = 1; m < numRows; m++)
        {
            for (int n = 0; n < num; n++)
            {
                if (m < str[n].size())
                    result.push_back(str[n][m]);
                if ((num1 - m) < str[n].size() && m != (num1 - m))
                    result.push_back(str[n][num1 - m]);
            }
        }
        return result;
    }
};

```
## 选项

### A
```cpp
class Solution
{
public:
    string convert(string s, int numRows)
    {
        string result;
        int i = 0;
        int j = 0;
        int tem = numRows != 1 ? 2 * (numRows - 1) : 1;
        for (i = 0; i <= tem / 2; ++i)
        {
            j = i;
            while (j < s.size())
            {
                result += s[j];
                if (j % tem != 0 && j % tem != tem / 2 && j + tem - 2 * i < s.size())
                {
                    result += s[j + tem - 2 * i];
                }
                j += tem;
            }
        }
        return result;
    }
};
```

### B
```cpp
class Solution
{
public:
    string convert(string s, int numRows)
    {
        if (numRows == 1 || s.size() <= numRows)
            return s;
        vector<string> rows(numRows);
        int curRow = 0;
        bool goingDown = false;
        for (char c : s)
        {
            rows[curRow] += c;
            if (curRow == 0 || curRow == numRows - 1)
                goingDown = !goingDown;
            curRow += goingDown ? 1 : -1;
        }
        string ret;
        for (string row : rows)
            ret += row;
        return ret;
    }
};
```

### C
```cpp
class Solution
{
public:
    string convert(string s, int numRows)
    {
        if (s.size() <= numRows || numRows <= 1)
            return s;
        int len = s.size();
        vector<vector<char> > pos(len, vector<char>(numRows));
        int k = 0;
        int flag = numRows - 1;
        for (int i = 0; i < len; i++)
        {
            for (int j = 0; j < numRows; j++)
            {
                if (i % (numRows - 1) == 0 || numRows == 2)
                {
                    pos[i][j] = s[k];
                    k++;
                }
                else if (j == flag - 1)
                {
                    pos[i][j] = s[k];
                    k++;
                    flag--;
                }
                else
                    continue;
                if (k == len)
                    break;
            }
            if (k == len)
                break;
            if (flag == 1)
                flag = numRows - 1;
        }
        string res;
        for (int j = 0; j <= numRows; j++)
        {
            int count = 0;
            for (int i = 0; i < len; i++)
            {
                if (j % (numRows - 1) == 0 && pos[i][j] != '\0')
                {
                    res = res + pos[i][j];
                    i = i + numRows - 2;
                    count = 0;
                    continue;
                }
                else if (j % (numRows - 1) != 0 && pos[i][j] != '\0')
                {
                    res = res + pos[i][j];
                    count = 0;
                    continue;
                }
                else
                    count++;
                if (count >= numRows)
                    break;
            }
            if (res.size() == len)
                break;
        }
        return res;
    }
};
```