【leetcode】544. 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.
解析
输出比赛配对
最强和最弱匹配,次强和次若匹配,一轮后,继续按此规则配对,输出最终的配对规则
思路
递归
解法
public String findContestMatch(int n) {
List<String> teams = new ArrayList<>();
for (int i = 1; i <= n; i++) {
teams.add(String.valueOf(i));
}
return match(teams);
}
private String match(List<String> teams) {
List<String> newTeams = new ArrayList<>();
for (int i = 0; i < teams.size() / 2; i++) {
newTeams.add(String.format("(%s,%s)", teams.get(i), teams.get(teams.size() - i - 1)));
}
if (newTeams.size() > 1) {
return match(newTeams);
}
return newTeams.get(0);
}
【leetcode】544. Output Contest Matches的更多相关文章
- 【LeetCode】544. Output Contest Matches 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- [LeetCode] 544. Output Contest Matches 输出比赛匹配对
During the NBA playoffs, we always arrange the rather strong team to play with the rather weak team, ...
- 【LeetCode】Recursion(共11题)
链接:https://leetcode.com/tag/recursion/ 247 Strobogrammatic Number II (2019年2月22日,谷歌tag) 给了一个 n,给出长度为 ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
- 【leetcode】Find All Anagrams in a String
[leetcode]438. Find All Anagrams in a String Given a string s and a non-empty string p, find all the ...
- 【LeetCode】9、Palindrome Number(回文数)
题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...
随机推荐
- Debugging Kafka connect
1. setup debug configuration mainClass: org.apache.kafka.connect.cli.ConnectDistributed VMOption: -D ...
- DevExpress v18.1 下载和教程文档
http://www.zdfans.com/html/18682.html 教程文档 https://blog.csdn.net/AABBbaby/article/details/81094482 下 ...
- 【GStreamer开发】GStreamer播放教程03——pipeline的快捷访问
目的 <GStreamer08--pipeline的快捷访问>展示了一个应用如何用appsrc和appsink这两个特殊的element在pipeline中手动输入/提取数据.playbi ...
- Ubuntu18.04 安装MySQL
安装 #命令1 sudo apt-get update #命令2 sudo apt-get install mysql-server 初始化 sudo mysql_secure_installatio ...
- Python函数基础学习(定义、函数参数、递归函数)
1.本程序是测试函数的基础.函数的参数.递归函数的测试. 函数的参数有: 必选参数.默认参数.可变参数.命名关键字参数和关键字参数 #!/usr/bin/python # -*- coding: ut ...
- Python35之包的创建
包(package) 一.创建一个文件夹,用于存放相关的模块,文件夹的名字即包的名字 二.在文件夹中创建一个__init__.py的模块文件,内容可以为空 三将相关的模块放入文件夹中 这样就相当于创建 ...
- 剑指offer55:链表中环的入口结点
1 题目描述 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. 2 思路和方法 这是一个典型的链表中查找环的问题,基本思路是,首先设置两个快慢指针slow和fast,并且快指 ...
- smarty中常用的流程控制逻辑
if else {if $age > 18} <div>年满十八岁!</div> {else if $age > 16} <div>年满十六岁!< ...
- Python中遍历整个列表及注意点(参考书籍Python编程从入门到实践)
1. 利用for循环遍历整个列表 magicians = ['alice', 'dsvid', 'carolina'] # 遍历整个列表 for magician in magicians: prin ...
- python 之 面向对象基础(组合和封装)
7.4 组合 解决类与类之间代码冗余问题有两种解决方案: 1.继承:描述的是类与类之间,什么是什么的关系 2.组合:描述的是类与类之间的关系,是一种什么有什么的关系 一个类产生的对象,该对象拥有一个属 ...