原题

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的更多相关文章

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

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

  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】Recursion(共11题)

    链接:https://leetcode.com/tag/recursion/ 247 Strobogrammatic Number II (2019年2月22日,谷歌tag) 给了一个 n,给出长度为 ...

  4. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  5. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

  6. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  7. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

  8. 【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 ...

  9. 【LeetCode】9、Palindrome Number(回文数)

    题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...

随机推荐

  1. ES6深入浅出-9 Promise-2.Promise的用法

    回调是不需要return 就可以传递数据.缺点就是嵌套多了就成了回调地狱 回调的另外一个问题拿不准应该怎么去传这个参数.以为node.js为例.nodejs有个readFile去读取文件,读取成功就用 ...

  2. Qt编写自定义控件72-提示进度条

    一.前言 我们在很多的安装包中,在安装过程中,经常可以在底部看到一个漂亮的进度条,上面悬浮着显示对应的进度,然后底部进度多种颜色渐变展示,Qt自带的进度条或者操作系统的进度条样式,不够炫,这次索性直接 ...

  3. 123457123456#3#----com.ppGame.konglongPuzzle78--前拼后广--konglongPT游戏_pp

    com.ppGame.konglongPuzzle78--前拼后广--konglongPT游戏_pp

  4. docker build doris-0.11.20-release source code

    1. pull doris dev docker image sudo docker pull apachedoris/doris-dev:build-env-1.1 2. dowload doris ...

  5. Django之Restful API

    理解Restful架构:http://www.ruanyifeng.com/blog/2011/09/restful RESTful设计指南:http://www.ruanyifeng.com/blo ...

  6. django 自定义 密码加密方式 及自定义验证方式

    在django1.6中,默认的加密方式是pbkdf_sha256,具体算法不表,一直以来用django的自带用户验证都十分顺手,但如果需要修改默认加密方式为md5,具体方法为: 在settings.p ...

  7. HTTPS小结 、TSL、SSL

    https://segmentfault.com/a/1190000009020635

  8. idea里面lombok要如何设置后才会生效

    16:31 Lombok Requires Annotation Processing Annotation processing seems to be disabled for the proje ...

  9. express url跳转(重定向)的实现:res.location() res.redirect()

    Express 是一个基于Node.js 实现的web框架,其响应HTTP请求的response对象中有两个响应url跳转方法res.location() res.redirect(),可以实现301 ...

  10. 常见的SQL注入检测语句(转载)

    0x00 前言 现在很多WAF都能拦截sqlmap.havij 等注入工具的发包注入,所以这时我们需要在浏览器上使用hackerbar 进行手工注入,或者说是手工绕过注入攻击 0x01 发现SQL 注 ...