0% found this document useful (0 votes)
74 views3 pages

Java ArrayList LeetCode Problems

The document presents solutions to five LeetCode problems related to ArrayLists in Java, including 'Two Sum', 'Pascal's Triangle', 'Summary Ranges', 'Spiral Matrix', and 'Merge Intervals'. Each problem includes a brief description, a link to the LeetCode page, and a Java implementation of the solution. The solutions utilize various Java collections and algorithms to address the specific challenges posed by each problem.

Uploaded by

i3156618
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views3 pages

Java ArrayList LeetCode Problems

The document presents solutions to five LeetCode problems related to ArrayLists in Java, including 'Two Sum', 'Pascal's Triangle', 'Summary Ranges', 'Spiral Matrix', and 'Merge Intervals'. Each problem includes a brief description, a link to the LeetCode page, and a Java implementation of the solution. The solutions utilize various Java collections and algorithms to address the specific challenges posed by each problem.

Uploaded by

i3156618
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

LeetCode ArrayList Problems in Java

1■■ Two Sum (#1)


LeetCode Link: [Link]
Find two numbers in an array that add up to a given target. Return their indices.
import [Link].*;

class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < [Link]; i++) {
int complement = target - nums[i];
if ([Link](complement)) {
return new int[] { [Link](complement), i };
}
[Link](nums[i], i);
}
return new int[]{};
}
}

2■■ Pascal’s Triangle (#118)


LeetCode Link: [Link]
Generate the first n rows of Pascal’s Triangle using ArrayList of ArrayLists.
import [Link].*;

class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> triangle = new ArrayList<>();
for (int i = 0; i < numRows; i++) {
List<Integer> row = new ArrayList<>();
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i)
[Link](1);
else
[Link]([Link](i - 1).get(j - 1) + [Link](i - 1).get(j));
}
[Link](row);
}
return triangle;
}
}

3■■ Summary Ranges (#228)


LeetCode Link: [Link]
Summarize a sorted array of integers into the smallest list of ranges covering all numbers.
import [Link].*;

class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> list = new ArrayList<>();
if ([Link] == 0) return list;

int start = nums[0];


for (int i = 1; i <= [Link]; i++) {
if (i == [Link] || nums[i] != nums[i - 1] + 1) {
if (start == nums[i - 1])
[Link]([Link](start));
else
[Link](start + "->" + nums[i - 1]);
if (i < [Link]) start = nums[i];
}
}
return list;
}
}

4■■ Spiral Matrix (#54)


LeetCode Link: [Link]
Return all elements of a matrix in spiral order using ArrayList.
import [Link].*;

class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> result = new ArrayList<>();
if ([Link] == 0) return result;

int top = 0, bottom = [Link] - 1;


int left = 0, right = matrix[0].length - 1;

while (top <= bottom && left <= right) {


for (int i = left; i <= right; i++) [Link](matrix[top][i]);
top++;

for (int i = top; i <= bottom; i++) [Link](matrix[i][right]);


right--;

if (top <= bottom)


for (int i = right; i >= left; i--) [Link](matrix[bottom][i]);
bottom--;

if (left <= right)


for (int i = bottom; i >= top; i--) [Link](matrix[i][left]);
left++;
}
return result;
}
}

5■■ Merge Intervals (#56)


LeetCode Link: [Link]
Merge overlapping intervals and return the result as a list of intervals.
import [Link].*;

class Solution {
public int[][] merge(int[][] intervals) {
[Link](intervals, (a,b) -> a[0] - b[0]);
List<int[]> list = new ArrayList<>();

int[] current = intervals[0];


[Link](current);

for (int[] interval : intervals) {


if (interval[0] <= current[1]) {
current[1] = [Link](current[1], interval[1]);
} else {
current = interval;
[Link](current);
}
}
return [Link](new int[[Link]()][]);
}
}

You might also like