Leetcode zigzag Conversion

Source: Internet
Author: User
class Solution {public:    string convert(string s, int nRows) {        int len = s.length();        if (len < 2 || nRows == 1 || len < nRows) return s;        vector<string> cols;        int si = 0;        int ri = 0;                while (si < len) {            bool inacol = cols.size() % (nRows-1) == 0;                        if (!inacol) {                for (int i=0; i<nRows - 2 && si <len; i++, si++) {                    cols.push_back(s.substr(si, 1));      // a char as a single column                }                continue;            }                        cols.push_back(string());            for (int i=0; i<nRows && si<len; i++, si++) {                cols.back().push_back(s[si]);   // all char in a column            }        }        string res;        int nCols = cols.size();        int stepa = nRows - 1;        int stepb = 0;                for (int i=0; i<nRows; i++) {            bool usea = false;            int last = -1;            for (int j = 0; j < nCols; j += usea ? stepa : stepb) {                usea = !usea;                if (j == last) continue;                last = j;                                int r = i, c = j;                                if (cols[c].length() < 1) break;                                if (c % (nRows - 1) != 0) {                    r = 0;                } else if (cols[c].length() - 1 < r) {                    break;                }                res.push_back(cols[c][r]);            }            --stepa, ++stepb;        }        cols.clear();        return res;    }};

In general, this is so long, and the time is 100 MS + code is not good!

 

The following code finds a problem that has not been noticed before:

    string a("abc");    string b;    b.push_back(a[10]);    cout<<b.length()<<endl;

Compiling and running will not report an error. Although the [10] operation is obviously out of bounds, different from the array, when the subscript operation of the string is out of bounds, a null character will be returned. Go to http://www.cplusplus.com/and check the following statement:

IfPosIs equal to the string length, the function returns a reference to a null character (Chart ()).

From the actual test, it is not only when Pos = Str. Length () in STR [POS], but also when it is larger than the string length. Although this process is helpful, sometimes it makes it harder to discover errors. The following code:

    string a("abc");    string b(a);    b.push_back(a[10]);        cout<<a<<","<<b<<endl;    cout<<(a == b)<<endl;

When outputting A and B, they are exactly the same. However, because B is more NULL than a, It is very confusing to have them equal in the equivalent comparison. Of course, the root cause is that codenong is not careful...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.