原题链接在这里:https://leetcode.com/problems/output-contest-matches/description/

题目:

During the NBA playoffs, we always arrange the rather strong team to play with the rather weak team, like make the rank 1 team play with the rank nth team, which is a good strategy to make the contest more interesting. Now, you're given n teams, you need to output their final contest matches in the form of a string.

The n teams are given in the form of positive integers from 1 to n, which represents their initial rank. (Rank 1 is the strongest team and Rank n is the weakest team.) We'll use parentheses('(', ')') and commas(',') to represent the contest team pairing - parentheses('(' , ')') for pairing and commas(',') for partition. During the pairing process in each round, you always need to follow the strategy of making the rather strong one pair with the rather weak one.

Example 1:

Input: 2
Output: (1,2)
Explanation:
Initially, we have the team 1 and the team 2, placed like: 1,2.
Then we pair the team (1,2) together with '(', ')' and ',', which is the final answer.

Example 2:

Input: 4
Output: ((1,4),(2,3))
Explanation:
In the first round, we pair the team 1 and 4, the team 2 and 3 together, as we need to make the strong team and weak team together.
And we got (1,4),(2,3).
In the second round, the winners of (1,4) and (2,3) need to play again to generate the final winner, so you need to add the paratheses outside them.
And we got the final answer ((1,4),(2,3)).

Example 3:

Input: 8
Output: (((1,8),(4,5)),((2,7),(3,6)))
Explanation:
First round: (1,8),(2,7),(3,6),(4,5)
Second round: ((1,8),(4,5)),((2,7),(3,6))
Third round: (((1,8),(4,5)),((2,7),(3,6)))
Since the third round will generate the final winner, you need to output the answer (((1,8),(4,5)),((2,7),(3,6))).

Note:

  1. The n is in range [2, 212].
  2. We ensure that the input n can be converted into the form 2k, where k is a positive integer.

题解:

所有数字convert成string 放入queue中.

Keep polling first and last element in the queue and pair them and put into a next queue.

When it is empty, switch next queue and queue.

Keep this operation until queue size is 1.

Time Complexity: O(n). 数字convert成string放入res中用时O(n). 之后res每次size减半, 所以是n + n/2 + n/4 + ... + 1 < 2*n = O(n).

Space: O(n).

AC Java:

 class Solution {
public String findContestMatch(int n) {
if(n<2 || n%2!=0){
return "";
} LinkedList<String> que = new LinkedList<>();
for(int i = 1; i<=n; i++){
que.add(""+i);
} while(que.size()>1){
LinkedList<String> nextQue = new LinkedList<>();
while(!que.isEmpty()){
String head = que.pollFirst();
String last = que.pollLast();
nextQue.add("("+head+","+last+")");
} que = nextQue;
} return que.peek();
}
}

LeetCode Output Contest Matches的更多相关文章

  1. [LeetCode] Output Contest Matches 输出比赛匹配对

    During the NBA playoffs, we always arrange the rather strong team to play with the rather weak team, ...

  2. [LeetCode] 544. Output Contest Matches 输出比赛匹配对

    During the NBA playoffs, we always arrange the rather strong team to play with the rather weak team, ...

  3. 【leetcode】544. Output Contest Matches

    原题 During the NBA playoffs, we always arrange the rather strong team to play with the rather weak te ...

  4. 【LeetCode】544. Output Contest Matches 解题报告 (C++)

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

  5. LeetCode 544----Output Contest Matches

    During the NBA playoffs, we always arrange the rather strong team to play with the rather weak team, ...

  6. LeetCode Weekly Contest 24

    1, 543. Diameter of Binary Tree 维护左右子树的高度,同时更新结果,返回以该节点结束的最大长度.递归调用. /** * Definition for a binary t ...

  7. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  8. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  9. LeetCode Weekly Contest

    链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...

随机推荐

  1. Archimate

    archimate语言提供了一种用于表示企业体系结构的图形化语言,包括策略,转换和迁移规划,以及架构的动机和基本原理.该标准的设计尽可能紧凑,但仍可用于大多数企业体系结构建模需求.下图显示了Archi ...

  2. 06_Hadoop配置伪分布式模式详解

    查看IP地址,设为手动模式: 配置hadoop用户sudo权限 su切换到root身份,配置vim /etc/sudoers文件,加入 hadoop ALL=(root)NOPASSWD:ALL    ...

  3. zookeeper部署搭建

    zookeeper教程 1.先在linux系统中安装jdk并配置环境变量,可以参考下面的链接1 2.下载安装zookeeper软件 教程参考: 链接1:http://www.linuxidc.com/ ...

  4. 【HackerRank】 The Full Counting Sort

    In this challenge you need to print the data that accompanies each integer in a list. In addition, i ...

  5. P4121 [WC2005]双面棋盘

    题目 P4121 [WC2005]双面棋盘 貌似是刘汝佳出的题目?? 做法 线段树维护并查集 线段树分治\(1\)~\(n\)行,我们要考虑维护的肯定是黑.白各自的联通块数量 考虑区间合并,其实就与中 ...

  6. 2017 GDS 全球域名大会7月7日举行

    2017年域名行业历经产业波澜,引发域名圈内对域名价值衍生及商业模式的探索.如今无论域名注册商.域名交易平台.域名拍卖平台都在寻找更好的商业模式,开启域名行业新航向. 7月,在中国域名之都厦门将掀起一 ...

  7. BZOJ3244/UOJ122 [Noi2013]树的计数

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  8. Codeforces Round #370 (Div. 2) A , B , C 水,水,贪心

    A. Memory and Crow time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. Python-flask中数据库连接池DBUtils

    一.DBUtils DBUtils是Python的一个用于实现数据库连接池的模块. 连接池的三种模式: 第一种模式:             它的缺点:每一次请求反复创建数据库的链接,链接的次数太多 ...

  10. Http协议与生命周期

    一.Http知识:    1.基于socket        浏览器(格式一)        web服务器(格式一)        MYSQL客户端(格式二)        MYSQL服务端(格式三) ...