LC 667. Beautiful Arrangement II

时间:2021-07-11 23:44:00

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&period; Beautiful Arrangement II

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

  2. 【leetcode】667&period; Beautiful Arrangement II

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

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

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

  4. &lbrack;LeetCode&rsqb; 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&period; Beautiful Arrangement

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

  7. &lbrack;Swift&rsqb;LeetCode667&period; 优美的排列 II &vert; 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. &lbrack;LeetCode&rsqb; Beautiful Arrangement 优美排列

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

随机推荐

  1. JSON与JSONP

    JSON JSON:一种用于在浏览器和服务器之间交换信息的基于文本的轻量级数据格式.是JS对象的字符串表示.例如:‘{''name":"aa","age&quo ...

  2. iOS navigationbar 透明

    [self.navigationController.navigationBar setTranslucent:YES]; //    为什么要加这个呢,shadowImage 是在ios6.0以后才 ...

  3. CentOS 6&period;3配置PPTP VPN的方法

    1.验证ppp 用cat命令检查是否开启ppp,一般服务器都是开启的,除了特殊的VPS主机之外. [root@localhost1 /]# cat /dev/ppp cat: /dev/ppp: No ...

  4. google map android api v2

    我在这主要列举几个需要注意的问题: 1.需要注意使用的api版本的问题,例如google map android api v1就和v2差别很大,包括申请key方面,所以在搜索资料的时候一定注意版本问题 ...

  5. 在后台代码中引入XAML的方法

    本文将介绍三种方法用于在后台代码中动态加载XAML,其中有两种方法是加载已存在的XAML文件,一种方法是将包含XAML代码的字符串转换为WPF的对象. 一.在资源字典中载入项目内嵌资源中的XAML文件 ...

  6. C&num; base和this&lbrack;转&rsqb;

    new关键字引起了大家的不少关注,尤其感谢Anders Liu的补充,让我感觉博客园赋予的交流平台真的无所不在.所以,我们就有必要继续这个话题,把我认为最值得关注的关键字开展下去,本文的重点是访问关键 ...

  7. SpringMVC表单标签

    SpringMVC学习系列(11) 之 表单标签   本篇我们来学习Spring MVC表单标签的使用,借助于Spring MVC提供的表单标签可以让我们在视图上展示WebModel中的数据更加轻松. ...

  8. opencv2 矩阵方式 resize图像缩放代码&lpar;转载&rpar;

    http://blog.sina.com.cn/s/blog_74a459380101r0yx.html opencv2 矩阵方式 resize图像缩放代码(转载) (2014-05-16 09:55 ...

  9. EOCS框架概述和剖析

    什么是EOCS? EOCS(Enterprise Operation Cross System),是一个基于eosio底层框架实现的企业级跨链操作系统,旨在实现和EOS主链通信的并行链,是真正意义的跨 ...

  10. MySQL DATE&lowbar;FORMAT函数使用

    DATE_FORMAT函数 一.定义和用法 DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据. 二.语法 DATE_FORMAT(date,format)date 参数是合法的日期. ...