码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 667: Beautiful Arrangement II

时间:2017-09-10 14:23:34      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:public   case   range   array   ber   solution   diff   beautiful   could   

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

标签:public   case   range   array   ber   solution   diff   beautiful   could   

原文地址:http://www.cnblogs.com/shuashuashua/p/7500911.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!