LeetCode 667: Beautiful Arrangement II

Posted keepshuatishuati

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 667: Beautiful Arrangement II相关的知识,希望对你有一定的参考价值。

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;
    }
}

 

以上是关于LeetCode 667: Beautiful Arrangement II的主要内容,如果未能解决你的问题,请参考以下文章

[leetcode-667-Beautiful Arrangement II]

LeetCode 667. Beautiful Arrangement II

[Leetcode]667.Beautiful Arrangement II

LeetCode 667. 优美的排列 II Beautiful Arrangement II (Medium)

667. Beautiful Arrangement II

667. Beautiful Arrangement II-- 类比526 但不能用back tracking