Note:

k different could be [1, k+1] are in wiggle sort order. Then there are k different numbers. But in this case, the last difference definitely will be 1. So folllowing difference will duplicate with previous.

 

class Solution {
    public int[] constructArray(int n, int k) {
        int[] result = new int[n];
        int maxValue = k + 1;
        for (int i = 0; i < k + 1; i++) {
            if (i % 2 == 0) {
                result[i] = i / 2 + 1;
            } else {
                result[i] = maxValue - i / 2;
            }
        }
        
        for (int i = k + 1; i < n; i++) {
            result[i] = i + 1;
        }
        return result;
    }
}

 

posted on 2017-09-10 13:38  keepshuatishuati  阅读(148)  评论(0编辑  收藏  举报