Given two integers n and k, you need to construct a list which contains n different positive integers ranging from 1 to n and obeys the following requirement: 
Suppose this list is [a1, a2, a3, ... , an], then the list [|a1 - a2|, |a2 - a3|, |a3 - a4|, ... , |an-1 - an|] has exactly kdistinct integers.

If there are multiple answers, print any of them.

Example 1:

Input: n = 3, k = 1
Output: [1, 2, 3]
Explanation: The [1, 2, 3] has three different positive integers ranging from 1 to 3, and the [1, 1] has exactly 1 distinct integer: 1.

Example 2:

Input: n = 3, k = 2
Output: [1, 3, 2]
Explanation: The [1, 3, 2] has three different positive integers ranging from 1 to 3, and the [2, 1] has exactly 2 distinct integers: 1 and 2.

Note:

  1. The n and k are in the range 1 <= k < n <= 104.

Runtime: 28 ms, faster than 39.80% of C++ online submissions for Beautiful Arrangement II.

构造题

最小,k=1是顺序或者倒序的排列。

最大,k=n-1是依此从两头挑数的排列。

根据k来判断要排多少个头尾数。

#include <vector>
#include <iostream>
using namespace std;
class Solution {
public:
vector<int> constructArray(int n, int k) {
int tmp = k/;
vector<int> ret;
int idx = ;
while(tmp){
ret.push_back(idx);
ret.push_back(n+ - idx);
idx++;
tmp--;
}
if(k % == ){
for(int i = idx; i<= n+-idx; i++) ret.push_back(i);
}else for(int i=n+-idx; i>=idx; i--) ret.push_back(i);
return ret;
}
};

LC 667. Beautiful Arrangement II的更多相关文章

  1. 667. Beautiful Arrangement II

    Given two integers n and k, you need to construct a list which contains n different positive integer ...

  2. 【leetcode】667. Beautiful Arrangement II

    题目如下: Given two integers n and k, you need to construct a list which contains ndifferent positive in ...

  3. 【LeetCode】667. Beautiful Arrangement II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. [LeetCode] Beautiful Arrangement II 优美排列之二

    Given two integers n and k, you need to construct a list which contains n different positive integer ...

  5. LeetCode Beautiful Arrangement II

    原题链接在这里:https://leetcode.com/problems/beautiful-arrangement-ii/description/ 题目: Given two integers n ...

  6. LC 526. Beautiful Arrangement

    uppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is constr ...

  7. [Swift]LeetCode667. 优美的排列 II | Beautiful Arrangement II

    Given two integers n and k, you need to construct a list which contains n different positive integer ...

  8. LeetCode Beautiful Arrangement

    原题链接在这里:https://leetcode.com/problems/beautiful-arrangement/description/ 题目: Suppose you have N inte ...

  9. [LeetCode] Beautiful Arrangement 优美排列

    Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...

随机推荐

  1. sql 存储过程笔记2

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sys_Page_v2]') and OBJECTPROPE ...

  2. swoole 定时器 swoole_time_tick 和 swoole_time_after

    <?php class myticker{ public $server = null; CONST host = '127.0.0.1'; CONST port = 9502; public ...

  3. linux命令集锦 基于centos7---优化linux的命令

    sed -i ‘s###g’ /etc/selinux/config  3个# 用于更改selinux配置文件:sed -i 's#SELINUX=enforcing#SELINUX=disabled ...

  4. 【代码备份】ORACLE数据库表同步DBLINK

    ---创建目标数据库连接DBLINKcreate database link testdblink connect to xxx identified by "xxx"using ...

  5. MySQL-InnoDB锁(一)

    本文主要记录InnoDB存储引擎中锁的关键点,下篇文章通过实例确认加锁的范围. InnoDB中的锁 1. 锁提供数据完整性和一致性 2. InnoDB行级锁:共享锁(S)和排他锁(X). 为了支持多粒 ...

  6. 【51nod 1245】Binomial Coefficients Revenge

    题目大意 C(M,N) = M! / N! / (M - N)! (组合数).给出M和质数p,求C(M,0), C(M,1)......C(M,M)这M + 1个数中,有多少数不是p的倍数,有多少是p ...

  7. js 获取对应的url参数

    举例:http://localhost:5000/?sss=sdf&fdd=123 1.设置或获取整个 URL 为字符串 window.location.href 值:  http://loc ...

  8. [人物存档]【AI少女】【捏脸数据】1223今日份的推荐

    点击下载(城通网盘):AISChaF_20191112214754919.png 点击下载(城通网盘):AISChaF_20191111205924765.png

  9. ajax上传文件(javaweb)

    前台:FormData,     formData.append("fileName",$("#file")[0].files[0];); https://ww ...

  10. 学习shell(二)

    条件分支:  (条件表达式的中括号里面 空格不可以省略) = ] then echo '2 = 2'; else echo '2 != 2'; fi # 上面的代码不使用缩进, 并不会出错, 但不应该 ...