原题

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. 报错:Unable to read additional data from client sessionid 0x36ab52d38c20b20, likely client has closed socket

    报错背景: CDH集群中,将kafka和Flume整合,将kafka的数据发送给Flume消费. 启动kafka的时候正常,但是启动Flume的时候出现了报错现象. 但是我检查了Flume,Flume ...

  2. IIS+PHP本地开发环境配置

    打开Win7系统自带IIS.如图只要点击两下,CGI一定要勾选上!完成后打开浏览器输入127.0.0.1测试一下,如果能打开页面说明iis开启成功. 安装PHP.不同版本的PHP会有所不同,这里使用的 ...

  3. Difference between Process and thread?

    What are the differences between a process and a thread? How are they similar? How can 2 threads com ...

  4. Python3之内建模块base64

    Base64是一种用64个字符来表示任意二进制数据的方法. 用记事本打开exe.jpg.pdf这些文件时,我们都会看到一大堆乱码,因为二进制文件包含很多无法显示和打印的字符,所以,如果要让记事本这样的 ...

  5. robot:根据条件主动判定用例失败或者通过

    场景: 当用例中的断言部分需要满足特定条件时才会执行,如果不满足条件时,可以主动判定该用例为passed状态,忽略下面的断言语句. 如上图场景,当每月1号时,表中才会生成上月数据,生成后数据不会再有改 ...

  6. golang 学习 (八)协程

    一: 进程.线程 和 协程 之间概念的区别:        对于 进程.线程,都是有内核进行调度,有 CPU 时间片的概念,进行 抢占式调度(有多种调度算法)    (补充: 抢占式调度与非抢占(轮询 ...

  7. Swoole练习 TCP

    TCP <?php $serv = new swoole_server("127.0.0.1", 9501); //监听连接进入事件 $serv->on('connec ...

  8. C++ 顺序容器(vector,list、deque,stack,queue)

    顺序容器的种类有:vector,list.deque 顺序容器适配器: stack     //先进后出   栈 queue   //先进先出   队列 priority_queue   //也优先管 ...

  9. samtools获取uniq reads

    参考地址: https://www.biostars.org/p/56246/ -q INT only include reads with mapping quality >= INT [0] ...

  10. TCP/IP学习笔记9--以太网之基本概念1: 分类,连接方式

    时间是变化的财富.时钟模仿它,却只有变化而无财富. -- 泰戈尔 以太网(Ethernet)一词源于Ether(以太), 是介质的意思.在爱因斯坦哥们提出量子力学之前,人们普遍认为宇宙空间充满以太,并 ...