http://bookshadow.com/weblog/2017/03/19/leetcode-output-contest-matches/
https://discuss.leetcode.com/topic/83463/java-8-line-solution-two-iterative-linkedlist
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:
Example 2:
Example 3:
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> matches = new ArrayList<>();
for(int i = 1; i <= n; i++) matches.add(String.valueOf(i));
while(matches.size() != 1){
List<String> newRound = new ArrayList<>();
for(int i = 0; i < matches.size()/2; i++)
newRound.add("(" + matches.get(i) + "," + matches.get(matches.size() - i - 1) + ")");
matches = newRound;
}
return matches.get(0);
}
Same idea. Indeed, we can even do this in an in-place fashion.
public String findContestMatch(int n) {
String[] res = new String[n];
for (int i = 1; i <= n; i++) res[i - 1] = "" + i;
for (; n > 1; n /= 2)
for (int i = 0; i < n / 2; i++)
res[i] = "(" + res[i] + "," + res[n - 1 - i] + ")";
return res[0];
}
Furthermore, if one uses a LinkedList to implement the string, the code can run in
Update: The (long) implementation is shown as follows, which uses the
O(n)
time.Update: The (long) implementation is shown as follows, which uses the
ListNode
that is pre-defined by LeetCode. public string FindContestMatch(int n) {
string[] arr = new string[n];
for (int i = 0; i < n; i++) arr[i] = (i + 1).ToString();
int left = 0;
int right = n - 1;
while (left < right)
{
while (left < right)
{
arr[left] = "(" + arr[left] + "," + arr[right] + ")";
left++;
right--;
}
left = 0;
}
return arr[0];
}
https://discuss.leetcode.com/topic/83457/c-java-clean-code public String findContestMatch(int n) {
String[] m = new String[n];
for (int i = 0; i < n; i++) {
m[i] = String.valueOf(i + 1);
}
while (n > 1) {
for (int i = 0; i < n / 2; i++) {
m[i] = "(" + m[i] + "," + m[n - 1 - i] + ")";
}
n /= 2;
}
return m[0];
}
https://discuss.leetcode.com/topic/83463/java-8-line-solution-two-iterative-linkedlist
public String findContestMatch(int n) {
LinkedList<String> res = new LinkedList<>();
for (int i = 1; i <= n; i++) res.add(i + "");
while (res.size() > 1) {
LinkedList<String> tmp = new LinkedList<>();
while (!res.isEmpty()) {
tmp.add("(" + res.remove(0) + "," + res.remove(res.size() - 1) + ")");
}
res = tmp;
}
return res.get(0);
}