0% found this document useful (0 votes)
4 views

基础班 02 Array & ArrayList

Uploaded by

susij
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

基础班 02 Array & ArrayList

Uploaded by

susij
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 89

Array & ArrayList

1
What is Array?
A data structure consisting of a collection of elements (values or variables), each
identified by at least one array index or key.

2
What is Array?
A data structure consisting of a collection of elements (values or variables), each
identified by at least one array index or key.

...

Memory

...

3 . 1
What is Array?
A data structure consisting of a collection of elements (values or variables), each
identified by at least one array index or key.

...
Element
Memory Element
Element
Element
Element
Element
...

3 . 2
What is Array?
A data structure consisting of a collection of elements (values or variables), each
identified by at least one array index or key.

...
Element
Memory Element Array
Element
Element
Element
Element
...

3 . 2
What is Array?
A data structure consisting of a collection of elements (values or variables), each
identified by at least one array index or key.
...
... 5
Element 3 ...
Memory Element Array 4 'c'
Element ... 'b'
Element 'r'
...
Element ...
true
Element
false
...
false
... 3 . 3
What is Array?
A data structure consisting of a collection of elements (values or variables), each
identified by at least one array index or key.

...
Element
Memory Element Array
Element This could be 1D array, or 2D
Element array, or even 3D array, etc.
Element
Element
...

3 . 4
How to represent Array
C++
int a[5], char c[3][6], etc.
no way to get length.
Java
int[] a = new int[5];
char[][] c= new char[3][];
a.length = 5

4 . 1
How to represent Array
C++
int a[5], char c[3][6], etc.
no way to get length.
Java
int[] a = new int[5];
char[][] c= new char[3][];
a.length = 5
Library

4 . 1
How to represent Array
C++
int a[5], char c[3][6], etc.
no way to get length.
Java
int[] a = new int[5];
char[][] c= new char[3][];
a.length = 5
Library
ArrayList<Integer> a = new ArrayList<Integer>(5);

4 . 1
How to represent Array
C++
int a[5], char c[3][6], etc.
no way to get length.
Java
int[] a = new int[5];
char[][] c= new char[3][];
a.length = 5
Library
ArrayList<Integer> a = new ArrayList<Integer>(5);
vector<int> a;

4 . 1
How to represent Array
int[] arr = new int[3] int[][] arr = new int[3][]
arr[0] = new int[3]
arr[1] = new int[5]
arr[2] = new int[4]

4 . 2
How to represent Array
int[] arr = new int[3]

length

Object Header

4 . 3
Array Features
Continuous space
addr = base_addr + offset (=index * sizeof(elem))
Same type elements
int[] arr = new int[10];
Identified by index
a[2], a[10]
Accessing any element in the array only needs
CONSTANT time, O(1)

5
Array is the Basic
Continuous space Linked List Tree
Same type elements
Identified by index String Graph

Set
HashMap

Stack/Queue
Heap

6
WHY use array?

7
WHY use array?
To make code simple and clean

7
Read 10 numbers and print out reversely
int a, b, c, ..., j; int a[10];
for (int i = 0; i < 10; i++)
cin >> a; cin >> a[i];
cin >> b;
cin >> d; for (int i = 9; i >= 0; i--)
.... cout << a[i];

cout << j << endl;


cout << i << endl;
...
cout << a << endl;

8
Warmup Questions
Sum of the array numbers
Minimum element of the array
Second minimum element of the array
Swap two elements in an array

9 . 1
Warmup Questions
Sum of the array numbers
int sum(int[] nums) {
int result = 0;
for (int i = 0; i < nums.length; i++) {
result += nums[i];
}
return result;
}

9 . 2
Warmup Questions
Minimum element of the array
int minimum(int a, int b) {
if (a < b) {
return a;
}
return b;
}

int minimum(int[] nums) {


int min = Integer.MAX_VALUE;
for (int i = 0; i < nums.length; i++) {
if (nums[i] < min) {
min = nums[i];
}
}
return min;
}

9 . 3
Warmup Questions
Second minimum element of the array
int secondMinimum(int[] nums) {
int min = Integer.MAX_VALUE;
for (int i = 0; i < nums.length; i++) {
if (nums[i] < min) {
min = nums[i];
}
}
int secondMin = Integer.MAX_VALUE;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == min) {
continue;
}
if (nums[i] < secondMin) {
secondMin = nums[i]
}
}
return secondMin;
}

9 . 4
Warmup Questions
Second minimum element of the array
int secondMinimum(int[] nums) {
int min = Integer.MAX_VALUE;
for (int i = 0; i < nums.length; i++) {
if (nums[i] < min) {
min = nums[i];
}
}
int secondMin = Integer.MAX_VALUE;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == min) {
continue;
}
if (nums[i] < secondMin) {
secondMin = nums[i]
}
}
return secondMin;
}

There are at least two numbers in the array and all numbers are distinct.

9 . 4
Warmup Questions
Second minimum element of the array
int secondMinimum(int[] nums) {
int min = Math.min(nums[0], nums[1]);
int secondMin = Math.max(nums[0], nums[1]);
for (int i = 2; i < nums.length; i++) {
if (nums[i] < min) {
secondMin = min;
min = nums[i];
} else if (nums[i] == min) {
secondMin = min;
} else if (nums[i] > min && nums[i] < secondMin) {
secondMin = nums[i];
} else if (nums[i] == secondMin) {
continue;
} else {
continue;
}
}
return secondMin;
}

9 . 5
Warmup Questions
Second minimum element of the array
int secondMinimum(int[] nums) {
int min = Math.min(nums[0], nums[1]);
int secondMin = Math.max(nums[0], nums[1]);
for (int i = 2; i < nums.length; i++) {
if (nums[i] < min) {
secondMin = min;
min = nums[i];
} else if (nums[i] < secondMin) {
secondMin = nums[i];
}
}
return secondMin;
}

9 . 6
Warmup Questions
Swap two elements in an array
{
int a = 5, b = 3;
int c = a;
a = b;
b = c;
// a = 3, b = 5;
}

void swap(int[] nums, int i, int j) {


int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}

9 . 7
Two Sum
Given an array of integers (no duplicate), find two numbers such that they add up
to a specific target number.

The function twoSum should return the two numbers such that they add up to
the target, where number1 must be less than number2.

You may assume that each input would have exactly one solution.

Examples:
Input: numbers={2, 7, 11, 15}, target=9
Output: {2, 7}

public int[] twoSum(int[] nums, int target) {


// TODO: implement this function.
}

10
Always ASK before
you write code.

11
Two Sum
Given an array of integers (no duplicate), find two numbers such that they add
up to a specific target number.

The function twoSum should return the two numbers such that they add up to
the target, where number1 must be less than number2.

You may assume that each input would have exactly one solution.

Examples:
Input: numbers={2, 7, 11, 15}, target=9
Output: {2, 7}

public int[] twoSum(int[] nums, int target) {


// TODO: implement this function.
}

12
Solution-1
Try one number (a), check all other numbers to see if
their sum is expected.
After trying all 'a', we can certainly find the pair.

13
Solution-1
int[] twoSum(int[] numbers, int target) {
int[] result = new int[2];
if (numbers.length < 2) {
return result;
}
for (int i = 0; i < numbers.length-1; i++) {
for (int j = i+1; j < numbers.length; j++) {
if (numbers[i] + numbers[j] == target) {
if (numbers[i] < numbers[j]) {
result[0] = numbers[i];
result[1] = numbers[j];
} else {
result[0] = numbers[j];
result[1] = numbers[i];
}
return result;
}
}
}
return result;
}

14
Solution-1-Summary

15
Solution-1-Summary
Direct, Easy

15
Solution-1-Summary
Direct, Easy
Time Complexity, O(n^2).

15
Solution-1-Summary
Direct, Easy
Time Complexity, O(n^2).
Inefficient
Numbers : {1, 4, 20, 15, 8, 6, 3 }, Target = 10.
First loop, 1 + 20 > 10
Second loop, 4 + 20, NO NEED!

15
Solution-1-Summary
Direct, Easy
Time Complexity, O(n^2).
Inefficient
Numbers : {1, 4, 20, 15, 8, 6, 3 }, Target = 10.
First loop, 1 + 20 > 10
Second loop, 4 + 20, NO NEED!
Sort

15
Solution-2
Sort the array
Try one number (i) from the beginning, check the other
one (j) from the end.
if sum == target, RESOLVED.
if sum > target, j--.
if sum < target, i++.
Numbers : {1, 4, 20, 15, 8, 6, 3 }, Target = 10.
After sorting, {1, 3, 4, 6, 8, 15, 20}.
There won't even be comparisons like 4+20 ? 10
Time Complexity: O(nlogn) + O(n) = O(nlogn).

16
Solution-2
int[] twoSum(int[] numbers, int target) {
int[] result = new int[2];
Arrays.sort(numbers);
int first = 0, second = numbers.length - 1;
while (first < second) {
if (numbers[first] + numbers[second] == target) {
result[0] = numbers[first];
result[1] = numbers[second];
return result;
}
if (numbers[first] + numbers[second] > target) {
second--;
}
if (numbers[first] + numbers[second] < target) {
first++;
}
}
return result;
}

17
Two Pointer
Use TWO pointers, instead of one, to
traverse the array in the same/opposite
direction.

18
Two Pointer
Mostly, SORTED.
in number order.
in sequence order.
linked list order.
To find two numbers, or two set of numbers, which
are subject to some conditions.

19
Three Sum
Given an array of integers (no duplicate), find THREE numbers such that they
add up to a specific target number.

The function threeSum should return the three numbers such that they add up
to the target, where three numbers are in increasing order.

You may assume that each input would have exactly one solution.

Examples:
Input: numbers={-1, 0, 1, 2, -4}, target=0
Output: {-1, 0, 1}

public int[] threeSum(int[] nums, int target) {


// TODO: implement this function.
}

20
Three Sum
Given an array of integers (no duplicate), find THREE numbers such that they
add up to a specific target number.

The function threeSum should return the three numbers such that they add up
to the target, where three numbers are in increasing order.

You may assume that each input would have exactly one solution.

Examples:
Input: numbers={-1, 0, 1, 2, -4}, target=0
Output: {-1, 0, 1}

public int[] threeSum(int[] nums, int target) {


// TODO: implement this function.
COMMUNICATION!!!
}

20
Solution
Try first two numbers,
check the third number in all other numbers to
see if their sum are expected (target-num1-num2).

21
Solution
Try first two numbers,
check the third number in all other numbers to
see if their sum are expected (target-num1-num2).
Try the first number,
check the other two in all other numbers to see if
their sum are expected (target-num1).

22
Solution
Try first two numbers,
check the third number in all other numbers to
see if their sum are expected (target-num1-num2).
Try the first number,
check the other two in all other numbers to see if
their sum are expected (target-num1).

twoSum

23
Solution
Try first two numbers,
check the third number in all other numbers to
see if their sum are expected (target-num1-num2).
Try the first number,
check the other two in all other numbers to see if
their sum are expected (target-num1).
Time Complexity
twoSum
Sort + twoSum for n different first number.
O(nlogn) + n*O(n) = O(n^2)

23
Solution
// pseudo code.
int[] threeSum(int[] nums, int target) {
int[] result = new int[3];
if (nums.length < 3) {
return nums;
}
Arrays.sort(nums);
for (int i = 0; i < nums.length-2; i++) {
twoSum(nums[i+1..], target-nums[i]);
if (twoSum has results) {
result = {nums[i], (twoSum result)}
}
}
return result;
}

24
Solution
int[] threeSum(int[] nums, int target) {
int[] result = new int[3];
if (nums.length < 3) {
return nums;
}
Arrays.sort(nums);
for (int i = 0; i < nums.length-2; i++) {
int first = i+1, second = nums.length-1, new_target = target-nums[i];
while (first < second) {
if (nums[first] + nums[second] == new_target) {
result[0] = nums[i];
result[1] = nums[first];
result[2] = nums[second];
return result;
}
if (nums[first] + nums[second] > new_target) {
second--;
}
if (nums[first] + nums[second] < new_target) {
first++;
}
}
}
return result;
}

25
Summary for k-Sum
Sort.
Try first number, use (k-1)-Sum.
Time Complexity
2-Sum: O(nlogn) + O(n) = O(nlogn)
3-Sum: O(nlogn) + O(n^2) = O(n^2)
4-Sum: O(nlogn) + O(n^3) = O(n^3)
k-Sum: O(nlogn) + O(n^(k-1)) = O(n^(k-1))

26
Reverse Array
Given an array, reverse all the numbers in the array.

Examples:
Input: {1, 2, 3, 4, 5, 6, 7}

Output: {7, 6, 5, 4, 3, 2, 1}

public void reverseArray(int[] nums) {


// TODO: implement this function.
}

27 . 1
Reverse Array
Given an array, reverse all the numbers in the array.

Examples:
Input: {1, 2, 3, 4, 5, 6, 7}

Output: {7, 6, 5, 4, 3, 2, 1}
public void reverseArray(int[] nums) {
// TODO: implement this function.
int first = 0, end = nums.length - 1;
while (first < end) {
swap(nums, first++, end--);
}
}

private void swap(int[] nums, int first, int second) {


int temp = nums[first];
nums[first] = nums[second];
nums[second] = temp;
}

27 . 2
Reverse Array
Given an array, reverse all the numbers in the array.

Examples:
Input: {1, 2, 3, 4, 5, 6, 7}

Output: {7, 6, 5, 4, 3, 2, 1}

Follow up:

- Reverse Number (1234 -> 4321)

- Palindrome Number/String ('abcd' -> false; 343 -> true)

- Odd Even Sort, Pivot Sort

- etc.

27 . 3
Odd Even Sort
Given an array of integers, sort them so that all odd integers come before even
integers.

The order of elements can be changed. The order of sorted odd numbers and
even numbers doesn't matter.

Examples:
Input: {4, 3, 5, 2, 1, 11, 0, 8, 6, 9}

Output: {9, 3, 5, 11, 1, 2, 0, 8 , 6, 4}

public void oddEvenSort(int[] nums) {


// TODO: implement this function. After sorting,
// nums should start with odd numbers and then
// even numbers.
}

28 . 1
Odd Even Sort
Given an array of integers, sort them so that all odd integers come before even
integers.

The order of elements can be changed. The order of sorted odd numbers and
even numbers doesn't matter.

Examples:
public void oddEvenSort(int[] nums) {
int first = 0, second = nums.length - 1;
while (first < second) {
Input: {4, 3, 5, 2, 1, 11, 0, 8, 6, 9} while (first < second && nums[first] % 2 == 1) {
first++;
}
Output: {9, 3, 5, 11, 1, 2, 0, 8 , 6, 4} while (first < second && nums[second] % 2 == 0) {
second--;
}
if (first < second) {
swap(nums, first++, second--);
}
}
}

28 . 2
Pivot Sort
Given an array of integers and a target number, sort them so that all numbers
that are smaller than the target always come before the numbers that are larger
than the target.

The order of elements can be changed.

Examples:
Input: {4, 9, 5, 2, 1, 11, 0, 8, 6, 3}, 7

Output: {4, 3, 5, 2, 1, 6, 0, 8, 11, 9}

public void pivotSort(int[] nums, int pivot) {


// TODO: implement this function.
}

29 . 1
Pivot Sort
Given an array of integers and a target number, sort them so that all numbers
that are smaller than the target always come before the numbers that are larger
than the target. void pivotSort(int[] nums, int pivot) {
int first = 0, second = nums.length - 1;
The order of elements can be changed. while (first < second) {
while (first < second && nums[first] <= pivot) {

Examples: }
first++;

while (first < second && nums[second] > pivot) {


Input: {4, 9, 5, 2, 1, 11, 0, 8, 6, 3}, 7 second--;
}
if (first < second) {
Output: {4, 3, 5, 2, 1, 6, 0, 8, 11, 9} swap(nums, first++, second--);
}
public void pivotSort(int[] nums, int pivot) { }
}
// TODO: implement this function.
void swap(int[] nums, int i, int j) {
} int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}

29 . 2
Pivot Sort
Given an array of integers and a target number, sort them so that all numbers
that are smaller than the target always come before the numbers that are larger
than the target.

The order of elements can be changed.

Examples:
Input: {4, 9, 5, 2, 1, 11, 0, 8, 6, 3}, 7

Output: {4, 3, 5, 2, 1, 6, 0, 8, 11, 9}

follow up: Quicksort

29 . 3
Remove Element
Given an array and a value, remove all instances of that value in place and return
the new length.

The order of elements can be changed. It doesn't matter what you leave beyond
the new length.

Examples:
Input: {10, 9, 5, 3, 9, 9, 8, 6, 7}, 9

Output: {10, 5, 3, 8, 6, 7, X, X, X}, 6


public int removeElement(int[] nums, int val) {
// TODO: implement this function. After removing,
// nums should contain only instances which
// is not equal to val (from 0 to new length).
}

30 . 1
Remove Element
Given an array and a value, remove all instances of that value in place and return
the new length.

The order of elements can be changed. It doesn't matter what you leave beyond
the new length.
public int removeElement(int[] nums, int val) {
if (nums.length == 0) {
return 0;
}
int first = 0, second = nums.length - 1;
while (first < second) {
while (first < second && nums[first] != val) {
first++;
}
while (first < second && nums[second] == val) {
second--;
}
if (first < second) {
swap(nums, first++, second--);
}
}
return first;
}

30 . 2
Remove Element
Given an array and a value, remove all instances of that value in place and return
the new length.

The order of elements can be changed. It doesn't matter what you leave beyond
the new length.
public int removeElement(int[] nums, int val) {
if (nums.length == 0) {
return 0;
}
int first = 0, second = nums.length - 1;
while (first < second) {
while (first < second && nums[first] != val) {
first++;
}
while (first < second && nums[second] == val) {
second--;
}
if (first < second) {
swap(nums, first++, second--);
[3, 2, 2, 3], 3
}
}
return first;
}

30 . 3
Remove Element
Given an array and a value, remove all instances of that value in place and return
the new length.

The order of elements can be changed. It doesn't matter what you leave beyond
the new length.
public int removeElement(int[] nums, int val) {
if (nums.length == 0) {
return 0;
}
int first = 0, second = nums.length - 1;
while (first < second) {
while (first < second && nums[first] != val) {
first++;
}
while (first < second && nums[second] == val) {
second--;
}
if (first < second) { [2, 2, 3, 3], 3
swap(nums, first++, second--);
}
}
return first;
}

30 . 4
Remove Element
Given an array and a value, remove all instances of that value in place and return
the new length.

The order of elements can be changed. It doesn't matter what you leave beyond
the new length.
public int removeElement(int[] nums, int val) {
if (nums.length == 0) {
return 0;
}
int first = 0, second = nums.length - 1;
while (first < second) {
while (first < second && nums[first] != val) {
first++;
}
while (first < second && nums[second] == val) {
second--;
}
if (first < second) {
swap(nums, first++, second--);
}
}
return nums[first] != val ? first+1 : first;
}

30 . 5
Remove Element
Given an array and a value, remove all instances of that value in place and return
the new length.

The order of elements can be changed. It doesn't matter what you leave beyond
the new length.

public int removeElement(int[] nums, int val) {


int index = 0, len = nums.length;
// len is the valid length of remaining array.
while (index < len) {
if (nums[index] == val) {
len--; // remove one element.
// Keep the possible valid element.
nums[index] = nums[len];
} else {
index++;
}
}
return len;
}

30 . 6
Remove Element
Given an array and a value, remove all instances of that value in place and return
the new length.

The order of elements can be changed. It doesn't matter what you leave beyond
the new length.

public int removeElement(int[] nums, int val) {


int index = 0, len = nums.length;
while (index < len) {
nums[index] = nums[index] == val ?
nums[--len] : nums[index++];
}
return len;
}

30 . 7
Merge Two Sorted Array
Given two sorted arrays of integer, both with increasing order. Please merge
them into one sorted array, with increasing order.

Examples:
Input: {1, 3, 5}, {2, 4, 6}

Output: {1, 2, 3, 4, 5, 6}

31 . 1
Merge Two Sorted Array
Given two sorted arrays of integer, both with increasing order. Please merge
them into one sorted array, with increasing order.

Examples:
Input: {1, 3, 5}, {2, 4, 6}

Output: {1, 2, 3, 4, 5, 6}

Solution:
Iterate two arrays at the same time, and always pick the smaller element from
two numbers to put into the result array.

31 . 1
Merge Two Sorted Array
Given two sorted arrays of integer, both with increasing order. Please merge
them into one sorted array, with increasing order.

Examples:
Input: {1, 3, 5}, {2, 4, 6}public int[] removeElement(int[] arr1, int[] arr2) {
int[] result = new int[arr1.length + arr2.length];
int index = 0, index1 = 0, index2 = 0;
Output: {1, 2, 3, 4, 5, 6} while (index1 < arr1.length && index2 < arr2.length) {
if (arr1[index1] < arr2[index2]) {
Solution: result[index++] = arr1[index1++];
} else {
result[index++] = arr2[index2++];
Iterate two arrays at the same time,
} and always pick the smaller element from
}
two numbers to put into the result array.
for (int i = index1; i < arr1.length; i++) {
result[index++] = arr1[i];
}
for (int i = index2; i < arr2.length; i++) {
result[index++] = arr2[i];
}
}

31 . 2
Merge Two Sorted Array
Given two sorted arrays of integer, both with increasing order. Please merge
them into one sorted array, with increasing order.

Examples:
Input: {1, 3, 5}, {2, 4, 6}

Output: {1, 2, 3, 4, 5, 6}

Follow up:

- Merge Two Sorted Linked List

- Merge K Sorted Array

- etc.

31 . 3
Limited Operation
Get value by index & Get length
We need more operations
add a number into array
remove a number
check if a number is in the array
etc.

32
Limited Operation
Get value by index & Get length
We need more operations
add a number into array
remove a number
check if a number is in the array
etc.
They are all very commonly used and need
many lines of code !

32
ArrayList

33
ArrayList
Array ArrayList

34
Basic Operations
Operation Input Output Time
Get index value O(1)
Set index, value void O(1)
Add [index], value void O(n)
Remove index/value void O(n)
Find value boolean O(n)

35
ArrayList is an Object
Fields
Store basic data, information about the Object
Functions
Get access to or modify the fields

36
ArrayList Implementation
Define fields
Define functions
constructor function, new ArrayList();
other functions

37 . 1
ArrayList Implementation - Fields
public class ArrayList{

private int capacity;


private int size;
private int[] data;

37 . 2
ArrayList Implementation - Functions
public class ArrayList{

public ArrayList(int capacity_) {


capacity = capacity_;
size = 0;
data = new int[capacity];
}

37 . 3
ArrayList Implementation - Functions
public class ArrayList{
// TODO: implement this class.
public int get(int index) {
// TODO: implement this method.
}
public void set(int index, int value) {
// TODO: implement this method.
}
public void add(int value) {
}
public void add(int index, int value) {
// TODO: implement this method.
}
public void remove(int index) {
// TODO: implement this method.
}
public void remove(int value) {
}
}

37 . 4
ArrayList Implementation - Get
public class ArrayList{

public int get(int index) {


return data[index];
}

37 . 5
ArrayList Implementation - Get
public class ArrayList{

public int get(int index) {


if (index < 0 || index >= size) {
// throw Exception
}
return data[index];
}

37 . 6
ArrayList Implementation - Set
public class ArrayList{

public void set(int index, int value) {


if (index < 0 || index >= size) {
// throw Exception
}
data[index] = value;
}

37 . 7
ArrayList Implementation - Add
public class ArrayList{

public void add(int index, int value) {


if (index < 0 || index > size) {
// throw Exception
}
size++;
for (int i = size-1; i >= index+1; i--) {
data[i] = data[i-1];
}
data[index] = value;
}
}

37 . 8
ArrayList Implementation - Add
public class ArrayList{

public void add(int index, int value) {


if (index < 0 || index > size) {
// throw Exception
}
if (size == capacity) {
resize();
}
size++;
for (int i = size-1; i >= index+1; i--) {
data[i] = data[i-1];
}
data[index] = value;
}
private void resize() {
capacity *= 2;
int[] new_data = new int[capacity];
for (int i = 0; i < size; i++) {
new_data[i] = data[i];
}
data = new_data;
}
}

37 . 9
ArrayList Implementation - Remove
public class ArrayList{

public void remove(int index) {


if (index < 0 || index >= size) {
// throw Exception
}
size--;
for (int i = index; i < size; i++) {
data[i] = data[i+1];
}
}
}

37 . 10
ArrayList Implementation - Keys
Key data storage, data[]
Initialize with capacity
Always Check Bound
Resize

source code for Java ArrayList

38
ArrayList Implementation - Keys
Key data storage, data[]
Initialize with capacity
Always Check Bound
Resize
very very very important!!!

source code for Java ArrayList

38
Homework

39
Two Sum - follow up
Given an array of integers (no duplicate), find two numbers such that they add
up to a specific target number.

The function twoSum should return the two numbers such that they add up to
the target, where number1 must be less than number2.

You may assume that each input would have exactly one solution.

Java:

public ArrayList<ArrayList<Integer>> twoSum(int[] nums, int target);

C++:

vector<vector<int>> twoSum(vector<int>& nums, int target);

40
Homework (Optional)
Move Zeroes
Missing Number
3 Sum Closest
Max Consecutive Ones
Rotate Array

Homework on OA will be assigned separately.

41

You might also like