practice mode on phones
tap the code to type with your on-screen keyboard. phone runs are practice only — they aren't scored or added to the leaderboard.
public int[][] merge(int[][] intervals) {
Java · leetcode · type the snippet below to practice
def fib(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
about this snippet
This is the mergeintervals snippet in Java — a classic LeetCode interview problem. Type it through to build the muscle memory you need to write it under interview pressure. DeveloperType scores you on net WPM and accuracy and only counts runs above 95% accuracy on the leaderboard, so practice rewards precision, not speed alone.
view source (13 lines)
public int[][] merge(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
List<int[]> out = new ArrayList<>();
for (int[] iv : intervals) {
if (!out.isEmpty() && iv[0] <= out.get(out.size() - 1)[1]) {
out.get(out.size() - 1)[1] = Math.max(out.get(out.size() - 1)[1], iv[1]);
} else {
out.add(iv);
}
}
return out.toArray(new int[0][]);
}
more Java snippets:
browse all Java snippets →