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. Windows 7上QTP11破解及java等插件破解方法

    QTP11破解方法: 1.准备文件 注册机mgn-mqt82.exe 2.安装QTP11 3.运行注册机mgn-mqt82.exe 如果运行mgn-mqt82.exe 没有反应-,请注意关掉暂时关掉杀 ...

  2. 8.5.zookeeper应用案例_分布式应用HA

    1.实现分布式应用(主节点HA)及客户端动态更新主节点状态 需求:某分布式系统中,主节点可以有多台,服务器可以动态(变化)上下线,任意一台客户端都能实时感知到主节点服务器的上下线 思路:架设Zooke ...

  3. Capture 设置pin name 不可见

    在器件库文件页面空白处双击,自动弹出OPTIONS对话框,再设计PIN NAME 可见属性为FLASE. 双击管脚.不管用,改不了.

  4. 【杭电多校第七场】A + B = C

    原题: Given a,b,c, find an arbitrary set of x,y,z such that a*10^x+b*10^y=c*10^z and 0≤x,y,z≤10^6. 给你三 ...

  5. [CSS] .class1.class2和.class1 .class2的区别

    .class1.class2(中间没有空格)匹配的是同时拥有class1和class2的元素 <div class="class1 class2">...</di ...

  6. myBatis整合Spring理论

    整合目标 更少的编码 更少的配置 足够的灵活性 1.更少的编码 只写接口,不写实现(myBatis帮我们实现接口) 2-1.更少的配置-别名 在java的规范当中,表示一个类型是通过包名+类名,但是整 ...

  7. 【hdu5517】Triple

    题目大意:给定一个二元组集合A{<a, b>}, 一个三元组集合B{<c, d, e>}, 定义 C 为 A 和 B 在 {b = e} 上的连接,求 C 集合中凸点的个数,即 ...

  8. java8新特性学习:stream与lambda

    Streams api 对 Stream 的使用就是实现一个 filter-map-reduce 过程,产生一个最终结果,或者导致一个副作用(side effect). 流的操作类型分为两种: Int ...

  9. Js基础知识(五) - 前端性能优化总结

    前端性能优化总结 资源优化 缓存 最好的资源优化就是不加载资源.缓存也是最见效的优化手段.说实话,虽然说客户端缓存发生在浏览器端,但缓存主要还是服务端来控制,与我们前端关系并不是很大.但还是有必要了解 ...

  10. Python模拟浏览器多窗口切换

    # 模拟浏览器多窗口切换 # 代码中引入selenium版本为:3.4.3 # 通过Chrom浏览器访问发起请求 # Chrom版本:59 ,chromdriver:2.3 # 需要对应版本的Chro ...