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.

答案

class Solution {
    public String findContestMatch(int n) {
        String[] pair = new String[n];
        for(int i = 0; i < pair.length; i++)
            pair[i] = Integer.toString(i + 1);
        
        // First round there is 8 team, second round there is 
        for(; n > 1; n = n /2) {
            for(int i = 0; i < n / 2; i++) {
                int j = n - i - 1;
                // pair up i and j, put new string in i
                pair[i] = "(" + pair[i] + "," + pair[j] + ")";
            }            
        }
        return pair[0];
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Description During the NBA playoffs, we always arrange th...
    Nancyberry阅读 446评论 0赞 0
  • During the NBA playoffs, we always arrange the rather str...
    matrxyz阅读 213评论 0赞 0
  • 前几天沸沸扬扬的江歌遇害案,我看了局面的所有采访,三个多小时,给我触动最大的,是江歌的母亲。 一个因为生了女儿却因...
    毛小雨阅读 757评论 2赞 1
  • 文‖大漠烟云 一个王朝的名字从刀光剑锋中磨出 又一个王朝在天下苍生的瞳孔里丟失 ·· 历史的天空,云彩嫁向万里江山...
    大漠烟云阅读 418评论 0赞 0
  • 收梅子的时节到了,江南小镇的烟雨也来了。 细细密密的雨丝,总也下不大,就像是那深闺姑娘的心思,淡淡的...
    木樨苜蓿阅读 302评论 0赞 1

友情链接更多精彩内容