题目地址:https://leetcode-cn.com/problems/output-contest-matches/

题目描述

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:

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

题目大意

在 NBA 季后赛中,我们总是安排较强的队伍对战较弱的队伍,例如用排名第 1 的队伍和第 n 的队伍对决,这是一个可以让比赛更加有趣的好策略。现在,给你 n 支队伍,你需要以字符串格式输出它们的 最终 比赛配对。

n 支队伍按从 1 到 n 的正整数格式给出,分别代表它们的初始排名(排名 1 最强,排名 n 最弱)。我们用括号(’(’, ‘)’)和逗号(’,’)来表示匹配对——括号(’(’, ‘)’)表示匹配,逗号(’,’)来用于分割。 在每一轮的匹配过程中,你都需要遵循将强队与弱队配对的原则。

解题方法

遍历

理解这个题的一个点是强队一定会战胜弱队。先安排最强和最弱,然后每次把剩余的最强和最弱安排到一起。

我们或许可以从第一轮里面得到灵感:

First round: (1,8),(2,7),(3,6),(4,5)

每个括号里面的第一个元素是1,2,3,4是递增的,第二个元素是8,7,6,5是递减的。

使用一个vector,里面存储的是各个队,然后每次把最后面的队伍弹出,放入最前面的队伍中,这样循环拼接即可。

再看第二轮:

Second round: ((1,8),(4,5)),((2,7),(3,6))

(1,8)是第一轮的第一个,(4,5)是第一轮的最后一个,这两个构成了一个新的组,说明也是同样的最前和最后的组合。

这样会得到第三轮:

Third round: (((1,8),(4,5)),((2,7),(3,6)))

把第二轮剩下的两组再拼到一起。

C++代码如下:

class Solution {
public:
string findContestMatch(int n) {
vector<string> nums(n, "");
for (int i = 1; i <= n; ++i) {
nums[i - 1] = to_string(i);
}
while (n) {
for (int i = 0; i < n / 2; ++i) {
nums[i] = "(" + nums[i] + "," + nums.back() + ")";
nums.pop_back();
}
n /= 2;
}
return nums[0];
}
};

参考资料:https://leetcode-cn.com/problems/output-contest-matches/solution/shu-chu-bi-sai-pi-pei-dui-by-leetcode/

日期

2019 年 9 月 24 日 —— 梦见回到了小学,小学已经芳草萋萋破败不堪

【LeetCode】544. Output Contest Matches 解题报告 (C++)的更多相关文章

  1. [LeetCode] 544. 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 te ...

  3. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  4. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  5. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  6. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  7. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  8. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  9. 【LeetCode】870. Advantage Shuffle 解题报告(Python)

    [LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

随机推荐

  1. python-django-请求响应对象

    用户请求终端的信息: 包括使用的ip地址,浏览器类型等 cookie: 测试测试: def print_request(request): print(request) print("!!! ...

  2. R语言与医学统计图形-【15】ggplot2几何对象之线图

    ggplot2绘图系统--几何对象之线图 曲线:点连线.路径曲线.时间序列曲线.模型拟合曲线...... 直线:水平直线.垂直直线.斜线. 1.曲线 对象及其参数. #路径图 geom_path(ma ...

  3. Golang使用validator进行数据校验及自定义翻译器

    Golang使用validator进行数据校验及自定义翻译器 包下载:go get github.com/go-playground/validator/v10 一.概述 在接口开发经常会遇到一个问题 ...

  4. Excel-同个工作簿中,多个工作表中有格式一致的表格,做汇总透视合并信息

    12.同个工作簿中,多个工作表中有格式一致的表格,做汇总透视合并信息 在工作簿中新建一个工作表-汇总表->按Alt+D+P调出数据透视表和数据透视图向导->选择"多重合并计算数据 ...

  5. CAN总线常见的两种编码格式(Intel/Motorola)

    在汽车电子行业的开发或者测试中,我们经常会看到CAN总线信号的常见的两种编码格式:Intel格式与Motorola格式. 讲解这两种格式之前,我们先来了解一些大端模式和小端模式,会对后面理解这两种编码 ...

  6. [云原生]Docker - 简介

    目录 什么是Docker? 为什么使用Docker? 对比传统虚拟机总结 什么是Docker? Docker是一个开源项目,诞生于2013年初,最初是dotCloud公司内部的一个业务项目.它基于Go ...

  7. 用户体验再升级!Erda 1.2 版本正式发布

    来源|尔达 Erda 公众号 Erda v1.2 Changelog: https://github.com/erda-project/erda/blob/master/CHANGELOG/CHANG ...

  8. 从for循环到机器码

    def p(*x): print(x) p(type(range), dir(range)) r = range(2); i = iter(r) try: p(next(i)); p(next(i)) ...

  9. Fragment放置后台很久(Home键退出很长时间),返回时出现Fragment重叠解决方案

    后来在google查到相关资料,原因是:当Fragment长久不使用,系统进行回收,FragmentActivity调用onSaveInstanceState保存Fragment对象.很长时间后,再次 ...

  10. 软件测试人员必备的linux命令

    1 目录与文件操作1.1 ls(初级)使用权限:所有人功能 : 显示指定工作目录下之内容(列出目前工作目录所含之档案及子目录). 参数 : -a 显示所有档案及目录 (ls内定将档案名或目录名称开头为 ...