c++ O(n) easy solution


  • 0
    W

    fetch 1~k + 1 to satisfy the requirement(like 1 , k + 1, 2, k, 3, k - 1,.....)
    '''
    class Solution {
    public:
    vector<int> constructArray(int n, int k) {
    int x = 1, y = k + 1;
    std::vector<int> v;
    while(x < y)
    {
    v.push_back(x);
    v.push_back(y);
    x++;
    y--;
    }
    if(x == y)
    v.push_back(x);
    for(int i = k + 2; i <= n; i++)
    v.push_back(i);
    return v;
    }
    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.