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 k
distinct 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
n
andk
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的更多相关文章
- 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 ...
随机推荐
- Java十进制转二进制
闲着没事写了个简单的十进制转二进制的算法,很简单,个人记录一下,不妥之处请指正. public static String toBinaryString(int j) { if (j < 0) ...
- mysql 中的 tinyint 字段
只能存储 -128 ~ 127 之间的数字
- zookeeper--为分布式应用提供协调服务
1.概述 zookeeper是一个开源的.分布式的.为分布式应用提供协调服务的Apache项目 zookeeper的工作机制 zookeeper从设计模式角度来理解:是一个基于观察者模式设计的分布式服 ...
- CAN学习方法(知乎)
作者:心机之花链接:https://www.zhihu.com/question/26776219/answer/244433861来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...
- Spring源码解析--Spring的整体架构
概述 Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用.Spring是于2003 年兴起的一个轻量级的Java 开发框 ...
- javac & java
# 没有 package, 没有 import 的情况 * 源文件 public class HelloWorld{ public static void main(String[] args){ S ...
- .NET平台的发展
.NET平台的发展.NET从1.0到.NET Core3.0:C#从1.0到8.0: ASP.NET从1.0到Core3.0: ASP.NET MVC1.0到ASP.NET MVC6.0,
- 12-SSMS图形化工具中不允许保存修改的解决办法
1.报出的警告 2.解决办法 工具-->选项-->设计器--->表设计和数据库设计器-->阻止保存要求重新创建表的更改 的勾去掉就OK 了
- 批量升级 CentOS bash
#! /usr/bin/env python #coding=utf-8 from fabric.api import * from fabric.state import * env.rol ...
- puppet完全攻略(二)让puppet代码支持vim高亮显示
puppet完全攻略(二)让puppet代码支持vim高亮显示 2012-06-10 13:33:01 标签:puppet viong puppet完全攻略 原创作品,允许转载,转载时请务必以超链接形 ...