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. Java注解【二、Java中的常见注解】

    JDK自带注解 @Override 重写 @Deprecated 已过期 @Suppvisewarnings 压制警告 Demo: public interface Person { public S ...

  2. @font-face字图标问题

    链接:http://www.exp99.com/htmlcss/htmlcss_229.html

  3. MyBatis 分页插件PageHelper 后台报错

    今天遇到一个问题,使用MyBatis 分页插件PageHelper 进行排序分页后,能正常返回正确的结果,但后台却一直在报错 net.sf.jsqlparser.parser.ParseExcepti ...

  4. 2019.8.30 记录一个Swiper的使用

    导入     flutter_swiper: ^1.1.6 引入     import 'package:flutter_screenutil/flutter_screenutil.dart'; 已下 ...

  5. Java 包装类及其与String转换、进制转换

    一.包装类 1.基本类型和引用类型 Java中的基本类型我们都知道有8种,但是作为基本类型限制功能的发挥,例如整形转String类型等可能需要类方法实现会更加简便.那么八个基本类型对应八个包装类,即引 ...

  6. programble blending --frame buffer fetch

    https://developer.arm.com/-/media/Files/pdf/graphics-and-multimedia/Efficient%20Rendering%20with%20T ...

  7. New!Devexpress WPF各版本支持VS和SQL Server版本对应图

    点击获取DevExpress v19.2.3完整版试用下载 本文主要为大家介绍DevExpress WPF各大版本支持的VS版本和支持的.Net版本图,Devexpress WPF v19.2.3日前 ...

  8. 一分钟学会在IDEA中使用sqlite数据库

    第一步:打开IDEA: 第二步: 第三步: 第四步: 第五步: 我们也使用idea来操作sqlite语法

  9. 服务消费(LoadBalancerClient、Ribbon、Feign)

    转自:https://www.jianshu.com/p/562045489d9d 4.1使用LoadBalancerClient 在Spring Cloud Commons中提供了大量的与服务治理相 ...

  10. python中的list,tuple,dict,set简介---陈雨童

    变量和对象 变量把对象和自己连接起来(指针连接对象空间),引用建立了变量和对象之间的映射关系,这就是引用.引用完成,就实现了赋值.变量通过对象的内存地址指向对象,类似于软链接 将变量a赋值给变量b,其 ...