原题

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. Qt编写气体安全管理系统5-数据监控

    一.前言 本项目对设备的监控有四种视图模式,可以任意切换,数据监控.地图监控.设备监控.曲线监控,其中数据监控是最常用的,所以在主界面导航中也排在第一位,综合观察分析了很多气体安全或者组态监控软件,大 ...

  2. 毫无PS痕迹 你的第一本Photoshop书 完整版

    毫无PS痕迹 你的第一本Photoshop书 目录 <毫无PS痕迹-你的本Photoshop书>全书分为四大部分: 第1.2章讲解色彩和图像的原理与基础知识要点. 第3至11章全面讲解了使 ...

  3. Django自定义用户认证系统之自定义用户模型

    参考文档:http://python.usyiyi.cn/django/topics/auth/customizing.html Django 自带的认证系统足够应付大多数情况,但你或许不打算使用现成 ...

  4. Python - Django - FBV 和 CBV

    FBV: Function Base View,基于函数的视图 views.py: from django.shortcuts import render, HttpResponse # FBV de ...

  5. LeetCode_303. Range Sum Query - Immutable

    303. Range Sum Query - Immutable Easy Given an integer array nums, find the sum of the elements betw ...

  6. 迅速生成项目-vue-cli-service

    推荐指数:

  7. js中的eval方法

    eval(string) eval函数接收一个参数string,如果string不是字符串,则直接返回string.否则执行string语句.如果string语句执行结果是一个值,则返回此值,否则返回 ...

  8. Python扫描器-端口扫描

    结合渗透测试最常见就是单个域名扫指纹,自动子域名查找.获取所有子域名的IP,自动C段IP查找相同子域名,利用有效IP扫端口. 常见端口库扫描 service_list = { 21:"FTP ...

  9. 【嵌入式硬件Esp32】ESP32 正确下载姿势

    程序的正确下载步骤,以8M flash为例子: 一.硬件连接 ESP32 的运行状态主要由 GPIO0 决定 二.ESP32 Flash 地址配置 ESP32 在编译时,通过 make menucon ...

  10. php+memcache高速缓存

    原文地址:https://blog.csdn.net/qq_33571752/article/details/86494667 #在lnmp基础上修改 依赖libevent程序库 cd lnmp/ta ...