my c++ solution


  • 0
    W
    class Solution {
    public:
        vector<int> constructArray(int n, int k) {
            vector<int> ret;
            vector<int> flag(n, 0);
            int count = 0;
            while (ret.size() != n) {
                int val = 0;
                if (count % 2 == 0) {
                    val = count / 2 + 1;
                } else {
                    val = k + 1 - count / 2 > 0 ? k + 1 - count / 2 : count / 2;
                }
                if (val <= n && flag[val - 1] == 0) {
                    ret.push_back(val);
                    flag[val - 1] = 1;
                }
                count++;
            }
            return ret;
        }
    };
    

Log in to reply
 

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