数组是一组元素(数值或者变量)的集合,每一个元素都被数组索引(array index)或者键值(key)所标识.
数组是一组元素(数值或者变量)的集合,每一个元素都被数组索引(array index)或者键值(key)所标识.
| ... |
|---|
| ... |
Memory
A data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key.
| ... |
|---|
| Element |
| Element |
| Element |
| Element |
| Element |
| Element |
| ... |
Memory
Array
A data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key.
| ... |
|---|
| Element |
| Element |
| Element |
| Element |
| Element |
| Element |
| ... |
Memory
Array
| ... |
|---|
| 5 |
| 3 |
| 4 |
| ... |
| ... |
|---|
| 'c' |
| 'b' |
| 'r' |
| ... |
| ... |
|---|
| true |
| false |
| false |
| ... |
A data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key.
| ... |
|---|
| Element |
| Element |
| Element |
| Element |
| Element |
| Element |
| ... |
Memory
Array
This could be 1D array, or 2D array, or even 3D array, etc.
数组是一组元素(数值或者变量)的集合,每一个元素都被数组索引(array index)或者键值(key)所标识.
| ... |
|---|
| Element |
| Element |
| Element |
| Element |
| Element |
| Element |
| ... |
Memory
数组是一组元素(数值或者变量)的集合,每一个元素都被数组索引(array index)或者键值(key)所标识.
| ... |
|---|
| Element |
| Element |
| Element |
| Element |
| Element |
| Element |
| ... |
Memory
| ... |
|---|
| 5 |
| 3 |
| 4 |
| ... |
| ... |
|---|
| true |
| false |
| false |
| ... |
| ... |
|---|
| 'c' |
| 'b' |
| 'r' |
| ... |
Array
数组是一组元素(数值或者变量)的集合,每一个元素都被数组索引(array index)或者键值(key)所标识.
| ... |
|---|
| Element |
| Element |
| Element |
| Element |
| Element |
| Element |
| ... |
Memory
可以是1维数组, 2维数组或者3维数组等
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]
int[] arr = new int[3]
| length |
|---|
Object Header
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]
int[] arr = new int[3]
Linked List
Tree
String
Set
HashMap
Stack/Queue
Heap
Graph
int a, b, c, ..., j;
cin >> a;
cin >> b;
cin >> d;
....
cout << j << endl;
cout << i << endl;
...
cout << a << endl;
int a[10];
for (int i = 0; i < 10; i++)
cin >> a[i];
for (int i = 9; i >= 0; i--)
cout << a[i];int sum(int[] nums) {
int result = 0;
for (int i = 0; i < nums.length; i++) {
result += nums[i];
}
return result;
}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;
}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.
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;
}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;
}{
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;
}在一个没有重复数字的整数数组中, 找出两个数字, 使得它们的和等于给定的数字
函数twoSum返回两个数字且它们的和等于给定的数字, 其中第1个数字小于第2个数字.
假定每组输入都有解.
例如:
Input: numbers={2, 7, 11, 15}, target=9
Output: {2, 7}
public int[] twoSum(int[] nums, int target) {
// TODO: implement this function.
}
在一个没有重复数字的整数数组中, 找出两个数字, 使得它们的和等于给定的数字.
函数twoSum返回两个数字使得它们的和等于给定的数字, 其中第1个数字小于第2个数字.
假定每组输入都只有唯一解.
Examples:
Input: numbers={2, 7, 11, 15}, target=9
Output: {2, 7}
public int[] twoSum(int[] nums, int target) {
// TODO: implement this function.
}
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;
}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;
}在一个没有重复数字的整数数组中, 找出3个数字, 使得它们的和等于给定的数字.
函数threeSum 返回3个数字使得它们的和等于给定的数字, 返回结果按照升序排列.
假定每组输入都只有唯一解.
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!!!
twoSum
// 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;
}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;
}把一个数组的元素反向排列.
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.
}
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;
}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.
把一个数组中的所有奇数排列到所有偶数的前面.
排序之后奇数部分或偶数部分各自的元素不需要保持原有的顺序,也不要求按序排列.
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.
}
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) {
int first = 0, second = nums.length - 1;
while (first < second) {
while (first < second && nums[first] % 2 == 1) {
first++;
}
while (first < second && nums[second] % 2 == 0) {
second--;
}
if (first < second) {
swap(nums, first++, second--);
}
}
}给定一个数字, 把数组中所有小于该数字的元素排列到大于该数字的元素的前面.
排序之后元素之间原有的相对顺序可以改变.
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.
}
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.
}
void pivotSort(int[] nums, int pivot) {
int first = 0, second = nums.length - 1;
while (first < second) {
while (first < second && nums[first] <= pivot) {
first++;
}
while (first < second && nums[second] > pivot) {
second--;
}
if (first < second) {
swap(nums, first++, second--);
}
}
}
void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}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
给定一个数组和一个数字,在原数组中将等于该数字的元素原地删除,并且返回删除元素之后的数组长度.
删除元素之后数组元素的顺序可以改变. 删除元素之后留下的位置可以是任意值.
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).
}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;
}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;
}[3, 2, 2, 3], 3
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;
}[2, 2, 3, 3], 3
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;
}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;
}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;
}给定两个升序排列的数组,将它们归成一个升序数组.
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.
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.
public int[] removeElement(int[] arr1, int[] arr2) {
int[] result = new int[arr1.length + arr2.length];
int index = 0, index1 = 0, index2 = 0;
while (index1 < arr1.length && index2 < arr2.length) {
if (arr1[index1] < arr2[index2]) {
result[index++] = arr1[index1++];
} else {
result[index++] = arr2[index2++];
}
}
for (int i = index1; i < arr1.length; i++) {
result[index++] = arr1[i];
}
for (int i = index2; i < arr2.length; i++) {
result[index++] = arr2[i];
}
}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.
这些操作都很常见但是却需要额外写很多行的代码 !
Array
ArrayList
| 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) |
public class ArrayList{
private int capacity;
private int size;
private int[] data;
}public class ArrayList{
public ArrayList(int capacity_) {
capacity = capacity_;
size = 0;
data = new int[capacity];
}
}
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) {
}
}public class ArrayList{
public int get(int index) {
return data[index];
}
}public class ArrayList{
public int get(int index) {
if (index < 0 || index >= size) {
// throw Exception
}
return data[index];
}
}public class ArrayList{
public void set(int index, int value) {
if (index < 0 || index >= size) {
// throw Exception
}
data[index] = value;
}
}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;
}
}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;
}
}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];
}
}
}very very very important!!!
在一个(没有重复数字)的整数数组中, 找出两个数字, 使得它们的和等于给定的数字.
函数twoSum返回两个数字使得它们的和等于给定的数字, 其中第1个数字小于第2个数字.
假定每组输入都只有唯一解.
Java:
public ArrayList<ArrayList<Integer>> twoSum(int[] nums, int target);
C++:
vector<vector<int>> twoSum(vector<int>& nums, int target);