zoukankan      html  css  js  c++  java
  • 6. 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 s, int numRows);

    Example 1:

    Input: s = "PAYPALISHIRING", numRows = 3
    Output: "PAHNAPLSIIGYIR"
    

    Example 2:

    Input: s = "PAYPALISHIRING", numRows = 4
    Output: "PINALSIGYAHRPI"
    Explanation:
    
    P     I    N
    A   L S  I G
    Y A   H R
    P     I

    how to init the two dim vector:

    Use the std::vector::vector(count, value) constructor that accepts an initial size and a default value:

    std::vector<std::vector<int> > fog(
        A_NUMBER,
        std::vector<int>(OTHER_NUMBER)); // Defaults to zero initial value
    

      

    If a value other zero, say 4 for example, was required to be the default then:

    std::vector<std::vector<int> > fog(
        A_NUMBER,
        std::vector<int>(OTHER_NUMBER, 4));
    

      

    I should also mention uniform initialization is introduced in C++11, which permits the initialization of vector, and other containers, using {}:

    std::vector<std::vector<int> > fog { { 1, 1, 1 },
                                        { 2, 2, 2 } };
    

      

    how to merge two vector:

    AB.reserve( A.size() + B.size() ); // preallocate memory
    AB.insert( AB.end(), A.begin(), A.end() );
    AB.insert( AB.end(), B.begin(), B.end() );
    

      

    my code:

    class Solution {
    public:
        string convert(string s, int numRows) {
            if (numRows == 1) return s;
            int len = s.length();
            vector<vector<char> > v(numRows, vector<char>());
            string ans = "";
            int t = 0, dir = 1;
            for (int i = 0; i < len; ++i) {
                v[t].push_back(s[i]);
                t += dir;
                if (t >= numRows) {
                    dir = -1;
                    t -= 2;
                }
                if (t < 0) {
                    dir = 1;
                    t = 1;
                }
            }
            for (int i = 0; i < numRows; ++i) {
                for (int j = 0; j < v[i].size(); ++j) {
                    ans += v[i][j];
                }
            }
            return ans;
        }
    };
    

      

    Runtime: 24 ms, faster than 56.90% of C++ online submissions for ZigZag Conversion.

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    前后台验证字符串长度
    接口和抽象类该什么时候用?
    程序员常去网站汇总
    SQLServer复合查询条件(AND,OR,NOT)对NULL值的处理方法
    c#-轮询算法
    常用的SQL语句
    HTTP请求工具类
    asp.net mvc jQuery 城市二级联动
    ibatis动态多条件查询及模糊查询(oracle,mysql,sql)
    iBatis 中 Like 的写法实现模糊查询
  • 原文地址:https://www.cnblogs.com/h-hkai/p/9719997.html
Copyright © 2011-2022 走看看