【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 ...
随机推荐
- Qt编写气体安全管理系统1-项目框架
一.前言 说到项目框架,也叫代码框架,其实是非常重要的,随着编程经验的增加,相信每个程序员都会有一个自己的习惯的项目框架,这个东西跟建房子一样,先把架子搭好,然后挨个往里边填充内容,据说牛逼的架构师都 ...
- k8s记录-flanneld+docker网络部署(四)
1)程序准备tar xvf flannel-v0.10.0-linux-amd64.tar.gz mkdir -p /data/projects/common/kubernetes/{bin,cfg, ...
- 3. Longest Substring Without Repeating Characters 无重复字符的最长子串
1. 原始题目 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 &quo ...
- 《Learning a Discriminative Feature Network for Semantic Segmentation》解读
旷世18年的CVPR,论文链接:https://arxiv.org/abs/1804.09337 Motivation:针对分割中的“类内不一致”和“类间一致性”的两大问题,设计了结合Smooth n ...
- ubuntu18.04手动安装二进制MySQL8.0
wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz tar xvJf ...
- jquery创建一个新的节点对象(自定义结构/内容)的好方法
jq创建一个新的节点对象,这对一些自定义功能很有帮助,而且可以随意控制对象的结构与内容,何乐而不为呢,看到这里,相信有些朋友已经按耐不住了,好记下来为大家介绍实现方法,感兴趣的朋友可以了解下哦 < ...
- egg.js 通过 form 和 ajax 两种方式上传文件并自定义目录和文件名
egg.js 通过 form 和 ajax 两种方式上传文件并自定义目录和文件名 评论:10 · 阅读:8437· 喜欢:0 一.需求 二.CSRF 校验 三.通过 form 表单上传文件 四.通过 ...
- 记录 centos samba 安装
https://www.ruletree.club/archives/4/ https://www.linuxidc.com/Linux/2017-03/141390.htm
- axios 使用入门
[Vue 牛刀小试]:第十五章 - 传统开发模式下的 axios 使用入门 一.前言# 在没有接触 React.Angular.Vue 这类 MVVM 的前端框架之前,无法抛弃 Jquery 的重 ...
- ubuntu18.10 修改MySQL编码为utf-8
首先登陆mysql的交互shell,输入下面的命令查看当前数据库的编码方式 show variables like '%character%'; 从上面可以看出,mysql数据库装上之后初始并不均是u ...