Copyright © 直通硅谷
http://www.zhitongguigu.com/
INCEPTION
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Just consider it as an Object
Linked List
Tree
String
Set
HashMap
Stack/Queue
Heap
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Graph
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Accessing any element in the array, using CONSTANT time. O(1) == fast
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Single-dimension Array
int[] arr = new int[3];
Two-dimensional Array
int[ ][ ] arr = new int[3][ ];
arr[0] = new int[3];
arr[1] = new int[5];
arr[2] = new int[4];
@4e25154f
@4e25154f
Copyright © 直通硅谷
http://www.zhitongguigu.com/
int a0, a1, a2, ..., a9;
a0 = 1;
a1 = 2;
a2 = 3;
...
System.out.println(a9);
System.out.println(a8);
System.out.println(a7);
...
int[] a = new int[10];
for (int i = 0; i < 10; i++) {
a[i] = i + 1;
}
for (int i = 9; i >= 0; i--) {
System.out.println(a[i]);
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Give me an array of ...
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public void moveZeroes(int[] nums) {
}
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
For example,
given nums = [0, 1, 0, 3, 9], after calling your function,
nums should be [1, 3, 9, 0, 0].
Move Zeros
Copyright © 直通硅谷
http://www.zhitongguigu.com/
index: [0, 1, 2, 3, 4]
input: [0, 1, 0, 3, 9]
output: [1, 3, 9, 0, 0]
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
For example,
given nums = [0, 1, 0, 3, 9], after calling your function,
nums should be [1, 3, 9, 0, 0].
Move Zeros
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public void moveZeroes(int[] nums) {
if (nums == null || nums.length == 0) {
return;
}
int p = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 0) {
nums[p] = nums[i];
p++;
}
}
for (; p < nums.length; p++) {
nums[p] = 0;
}
}
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
For example,
given nums = [0, 1, 0, 3, 9], after calling your function,
nums should be [1, 3, 9, 0, 0].
Move Zeros
null & 0
Two pointers
Clean up
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public void moveZeroes(int[] nums) {
if (nums == null || nums.length == 0) {
return;
}
int p = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 0) {
nums[p++] = nums[i];
}
}
while (p < nums.length) {
nums[p++] = 0;
}
}
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
For example,
given nums = [0, 1, 0, 3, 9], after calling your function,
nums should be [1, 3, 9, 0, 0].
Move Zeros
null & 0
Two pointers
Clean up
Copyright © 直通硅谷
http://www.zhitongguigu.com/
public void moveZeroes(int[] nums) {
if (nums == null || nums.length == 0) return;
int p = 0;
for (int i = 0; i < nums.length; i++)
if (nums[i] != 0) nums[p++] = nums[i];
while (p < nums.length) nums[p++] = 0;
}
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
For example,
given nums = [0, 1, 0, 3, 9], after calling your function,
nums should be [1, 3, 9, 0, 0].
Move Zeros
Please Don't
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
char c = 'a';
System.out.println(c);
System.out.println(c + 1);
48 = 32 + 16
65 = 64 + 1
97 = 64 + 32 + 1
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
int a = 5;
int[] arr = new int[6];
Object obj = new Object();
a = 5
arr = @4e25154f
@4e25154f
obj
int b = a;
int[] arr2 = arr;
Object obj2 = obj;
@9d1c81a8
0 1 2 3 4 5
Copyright © 直通硅谷
http://www.zhitongguigu.com/
int a = 5;
int[] arr = new int[6];
Object obj = new Object();
a = 5
arr = @4e25154f
@4e25154f
obj = @9d1c81a8
@9d1c81a8
int b = a;
int[] arr2 = arr;
Object obj2 = obj;
b = 5
arr2 = @4e25154f
obj2 = @9d1c81a8
THINK
a = 10;
// what about b?
arr[0] = 8;
// what about arr1[1]
obj = null;
// what about obj2?
0 1 2 3 4 5
Copyright © 直通硅谷
http://www.zhitongguigu.com/
void caller() {
int a = 5;
int[] arr = new int[6];
Human hu = new Human(15);
change(a, arr, hu);
System.out.println(a);
System.out.println(arr[1]);
System.out.println(hu.age);
}
void change(int b, int[] a_, Human animal) {
b = 10;
a[1] = animal.age - 4;
animal.age = 22;
}
class Human {
int age;
public Human(int age) {
this.age = age;
}
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
void caller() {
int a = 5;
int[] arr = new int[6];
Human hu = new Human(15);
change(a, arr, hu);
System.out.println(a);
System.out.println(arr[1]);
System.out.println(hu.age);
}
void change(int b, int[] a_, Human animal) {
b = 10;
a[1] = animal.age - 4;
animal.age = 22;
}
class Human {
int age;
public Human(int age) {
this.age = age;
}
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
arr
a = 5
hu
a_
b
animal
age = 15
0
0
0
0
0
0
void caller() {
int a = 5;
int[] arr = new int[6];
Human hu = new Human(15);
change(a, arr, hu);
System.out.println(a);
System.out.println(arr[1]);
System.out.println(hu.age);
}
void change(int b, int[] a_, Human animal) {
b = 10;
a[1] = animal.age - 4;
animal.age = 22;
}
class Human {
int age;
public Human(int age) {
this.age = age;
}
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
arr
a = 5
hu
a_
b = 10
animal
age = 22
11
0
0
0
0
0
Primitive Type
Passing the COPY of primitive value.
Reference Type
Passing the COPY of object address.
void caller() {
int a = 5;
int[] arr = new int[6];
Human hu = new Human(15);
change(a, arr, hu);
System.out.println(a);
System.out.println(arr[1]);
System.out.println(hu.age);
}
void change(int b, int[] a_, Human animal) {
b = 10;
a[1] = animal.age - 4;
animal.age = 22;
}
class Human {
int age;
public Human(int age) {
this.age = age;
}
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
arr
a = 5
hu
age = 22
11
0
0
0
0
0
Primitive Type
Passing the COPY of primitive value.
Reference Type
Passing the COPY of object address.
void caller() {
int a = 5;
int[] arr = new int[6];
Human hu = new Human(15);
change(a, arr, hu);
System.out.println(a);
System.out.println(arr[1]);
System.out.println(hu.age);
}
void change(int b, int[] a_, Human animal) {
b = 10;
a[1] = animal.age - 4;
animal.age = 22;
}
class Human {
int age;
public Human(int age) {
this.age = age;
}
}
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Copyright © 直通硅谷
http://www.zhitongguigu.com/
String a = "abc";
String b = "abc";
System.out.println(a == b); // true
String c = new String("abc");
System.out.println(a == c); // false
System.out.println(a.equals(c)); // true
String d = new String("abc");
System.out.println(c == d); // false
System.out.println(d.equals(c)); // true
Copyright © 直通硅谷
http://www.zhitongguigu.com/
Address of the real object.
Array
ArrayList
Operation | Input | Output | Time |
---|---|---|---|
Get | <Index> | value | O(1) |
Set | <index, value> | void/ArrayList | O(1) |
Add | <[index], value> | void/ArrayList | O(n) |
Remove | <index/value> | void/ArrayList | O(n) |
Find | <value> | boolean/index | O(n) |
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) {
// TODO: implement this method.
}
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) {
// TODO: implement this method.
}
}
public class ArrayList{
private int capacity;
private int size;
private int[] data;
public ArrayList(int capacity_) {
capacity = capacity_;
size = 0;
data = new int[capacity];
}
}
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[] temp_data = new int[size];
for (int i = 0; i < size; i++) {
temp_data[i] = data[i];
}
data = new int[capacity];
for (int i = 0; i < size; i++) {
data[i] = temp_data[i];
}
}
}
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!!!
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.
Example:
Input: numbers={2, 7, 11, 15}, target=9
Output: {2, 7}
public int[] twoSum(int[] nums, int target) {
// TODO: implement this function.
}
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.
Example:
Input: numbers={2, 7, 11, 15}, target=9
Output: {2, 7}
public int[] twoSum(int[] nums, int target) {
// TODO: implement this function.
}
// sudo code
for i from 0 to n - 1
for j from i + 1 to n - 1
if nums[i] + nums[j] == target
return
public 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;
}
Big O?
public int[] twoSum(int[] nums, int target) {
int[] result = new int[2];
for (int i = 0; i < nums.length-1; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
return (nums[i] < nums[j]) ? new int[] {nums[i], nums[j]} : new int[] {nums[j], nums[i]};
}
}
}
return result;
}
public 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;
}
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.
}
twoSum
public int[] threeSum(int[] nums, int target) {
// Sudo code
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;
}
method {
m1();
m2();
}
public 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;
}
Given an array, reverse all the numbers in the array.
Example:
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.
}
Inplace?
What is Inplace?
Given an array, reverse all the numbers in the array.
Example:
Input: {1, 2, 3, 4, 5, 6, 7}
Output: {7, 6, 5, 4, 3, 2, 1}
public void reverseArray(int[] nums) {
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;
}
Swap without third var?
Given an array, reverse all the numbers in the array.
Example:
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;
}
Follow up:
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.
Example:
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.
Example:
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 (nums[first] % 2 == 1) {
first++;
}
while (nums[second] % 2 == 0) {
second--;
}
if (first < second) {
swap(nums, first++, second--);
}
}
}
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.
Example:
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--);
}
}
}
No inplace
Traverse 1 time
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.
Example:
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 target) {
// 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.
Example:
Input: {4, 9, 5, 2, 1, 11, 0, 8, 6, 3}, 7
Output: {4, 3, 5, 2, 1, 6, 0, 8, 11, 9}
public static void pivotSort(int[] nums, int target) {
int first = 0, second = nums.length - 1;
while (first < second) {
while (first < second && nums[first] <= target) {
first++;
}
while (first < second && nums[second] > target) {
second--;
}
if (first < second) {
swap(nums, first++, second--);
}
}
}
public static 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.
Example:
Input: {4, 9, 5, 2, 1, 11, 0, 8, 6, 3}, 7
Output: {4, 3, 5, 2, 1, 6, 0, 8, 11, 9}
public static void pivotSort(int[] nums, int target) {
int first = 0, second = nums.length - 1;
while (first < second) {
while (first < second && nums[first] <= target) {
first++;
}
while (first < second && nums[second] > target) {
second--;
}
if (first < second) {
swap(nums, first++, second--);
}
}
}
public static void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
QuickSort
[Homewrok]
Follow up
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.
Example:
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.
Example:
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) {
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]
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.
Example:
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) {
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]
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.
Example:
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) {
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.
Example:
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) {
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.
nums[index] = nums[len]; // Keep the possible valid element.
} 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.
Example:
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) {
int index = 0, len = nums.length;
while (index < len) {
nums[index] = nums[index] == val ? nums[--len] : nums[index++];
}
return len;
}
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}
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: Homework
- Merge Two Sorted Linked List
- Merge K Sorted Array
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);
Given an array nums of integers and an int k, partition the array (i.e move the elements in "nums") such that:
Return the partitioning index, i.e the first index i nums[i] >= k.
Java:
public int partitionArray(int[] nums, int k);
C++:
int partitionArray(vector<int> nums, int k);