solution.md 4.3 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 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 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 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
# Z 字形变换
以下错误的选项是?
## 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;
    }
};
```