667. Beautiful Arrangement II
Given two integers
nandk, you need to construct a list which containsndifferent positive integers ranging from1tonand 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 exactlykdistinct 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.
Approach #1: Math. [Java]
class Solution {
public int[] constructArray(int n, int k) {
int[] res = new int[n];
for (int i = 0, l = 1, r = n; l <= r; ++i) {
res[i] = (k > 1) ? (k-- % 2 != 0 ? l++ : r--) : l++;
}
return res;
}
}
Analysis:
667. Beautiful Arrangement II的更多相关文章
- LC 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 ...
- [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 ...
- [Swift]LeetCode526. 优美的排列 | Beautiful Arrangement
Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...
随机推荐
- datagridview 如何禁止行被选中
如题,如何规定特定的行,光标不能定位,也不能被选中,就好想Button中的Enable属性那样,变灰,而且点击也没有反应那种,这样的效果,如何实现. datagridview [解决办法]dataGr ...
- AE教程:学会这个,你做的Logo就可以单独出道了
一.确定所需要做的动效 1.制作logo背景形状动效 2.制作U的动效 3.制作I的动效 4.制作消失动效 二.制作logo背景形状动效1.”合成 - 新建合成“ 新建一个1000*800的合成 2. ...
- mysql 1045 access denied for user********
另一个方法Windows: 1. 管理员登陆系统,停止mysql服务或者结束mysqld-nt进程2. 进入命令行,来到mysql的安装目录.假设安装目录为 d:/mysql/ , CMD进入命令行3 ...
- Android教程:wifi热点问题
http://www.linuxidc.com/Linux/2012-05/60718.htm 现在很多移动设备都提供wifi hostpot功能,使用方便,也省下了原来无线路由器的成本.wifi和w ...
- 修改Swing窗口风格
String look; java: look = "javax.swing.plaf.metal.MetalLookAndFeel"; Windows: look = ...
- python之零碎知识
一 join方法 主要是做字符串的拼接:join后面跟的类型必须是要可迭代得到对象 for循环的对象是可迭代对象 # result = "".join(li) # print(re ...
- 2018.06.30 BZOJ1857: [Scoi2010]传送带(三分套三分)
1857: [Scoi2010]传送带 Time Limit: 1 Sec Memory Limit: 64 MB Description 在一个2维平面上有两条传送带,每一条传送带可以看成是一条线段 ...
- arduino一些内容
arduino 套件使用说明书V1.0.pdf, 步进电机 DHT11 传感器另外一脚要接A0 /* Web client This sketch connects to a website (htt ...
- html转jsp部分css不可用
解决方法 <%String path = request.getContextPath();String basePath = request.getScheme()+"://&quo ...
- python+django+mysql配置步骤
安装python 详细步骤见:地址 1. 从 http://www.python.org/download/ 下载最新的python版本 (我用的是python2.6, 当时最稳定的) 2. 然后一路 ...