Java 8 line solution, two iterative LinkedList


  • 2

    It is obvious that the Matching Pattern is Strongest v.s. Weakest. Thus we keep create Matching Group until there is only 1 group left.

    public class Solution {
        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);
        }
    }
    

  • 0
    Z

    I think
    tmp.add("(" + l.remove(0) + "," + r.remove(r.size() - 1) + ")");

    should be

    tmp.add("(" + res.remove(0) + "," + res.remove(r.size() - 1) + ")");

    ?


  • 0

    @zzwcsong Thanks, I copied a wrong code. Fixed.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.