想到的:

这k个差的取值最大是n-1,最小是1

 

解题思路:

667. Beautiful Arrangement II

class Solution {
    public int[] constructArray(int n, int k) {
        if( k >= n )return null;
        int[] res = new int[n];
        int pre = 1;
        int rear = n;
        int i = 0;
        while( i < k ){
            res[ i ++ ] = pre++;
            if( i < k )res[ i ++ ] = rear--;
        }
        if( k % 2 == 0 ){
            while( i < n )
            res[i ++ ] = rear --;
        }
        if( i % 2 == 1 ){
            while( i < n )
            res[ i ++ ] = pre ++;
        }
        return res;
    }
}

 

相关文章: