LC 667. Beautiful Arrangement II
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:
- The
nandkare 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的更多相关文章
- 667. Beautiful Arrangement II
Given two integers n and k, you need to construct a list which contains n different positive integer ...
- 【leetcode】667. Beautiful Arrangement II
题目如下: Given two integers n and k, you need to construct a list which contains ndifferent positive in ...
- 【LeetCode】667. Beautiful Arrangement II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode] Beautiful Arrangement II 优美排列之二
Given two integers n and k, you need to construct a list which contains n different positive integer ...
- LeetCode Beautiful Arrangement II
原题链接在这里:https://leetcode.com/problems/beautiful-arrangement-ii/description/ 题目: Given two integers n ...
- LC 526. Beautiful Arrangement
uppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is constr ...
- [Swift]LeetCode667. 优美的排列 II | Beautiful Arrangement II
Given two integers n and k, you need to construct a list which contains n different positive integer ...
- LeetCode Beautiful Arrangement
原题链接在这里:https://leetcode.com/problems/beautiful-arrangement/description/ 题目: Suppose you have N inte ...
- [LeetCode] Beautiful Arrangement 优美排列
Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...
随机推荐
- python 匿名函数lambda使用
lambda函数语法格式: lambda函数 后面参数可以有一个或多个,冒号后面是python表达式: lambda 参数1,参数2,参数3...:表达式 # 一个参数情况: a = lambda x ...
- 从 Android 源码到 apk 的编译打包流程
文中涉及到的工具所在目录:Android/sdk/build-tools.下面开始分解并逐步实现对源码的打包. 编译流程 1. 生成仅包含资源文件的 apk 包和 R.java 文件 根据资源文件和 ...
- Linux Shell Web超级终端工具shellinabox
Shell是Linux内核应用程序,是指“为使用者提供操作界面”的软件,也是命令解析器,它类似于Windows操作系统DOS下的cmd.exe应用程序.它接收用户命令,然后调用相应的应用程序,用户一般 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(22)|宏Macro]
[易学易懂系列|rustlang语言|零基础|快速入门|(22)|宏Macro] 实用知识 宏Macro 我们今天来讲讲Rust中强大的宏Macro. Rust的宏macro是实现元编程的强大工具. ...
- CSS基础学习-10.CSS伸缩盒(老版本)
- JAVA NIO 文件部分
NIO java使用NIO的目的是为了提升性能,实际上老的io程序也已经优化过了,速度也有相应的提升. NIO主要有三大核心部分:Channel(通道),Buffer(缓冲区), Selector.传 ...
- 论文参考文献中的M R J意义
1 期刊作者.题名[J].刊名,出版年,卷(期):起止页码 2 专著作者.书名[M].版本(第一版不著录).出版地:出版者,出版年.起止页码 3 论文集作者.题名[C].//编者.论文集名.出版地:出 ...
- Vue数据通信详解
如果有需要源代码,请猛戳源代码 希望文章给大家些许帮助和启发,麻烦大家在GitHub上面点个赞!!!十分感谢 一.前言 组件是 vue.js最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着 ...
- js自定义事件CustomEvent、Event、TargetEvent
1.Event Event 对象代表事件的状态,比如事件在其中发生的元素.键盘按键的状态.鼠标的位置.鼠标按钮的状态. 事件通常与函数结合使用,函数不会在事件发生前被执行! Event的事件都是系统自 ...
- laravel 之 cookie 使用
<?php namespace App\Http\Controllers\Admin; use Illuminate\Http\Request; use Illuminate\Http\Resp ...