首页 > 其他 > 详细

LeetCode 667: Beautiful Arrangement II

时间:2017-09-10 14:01:00      阅读:265      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!