前言
每天五道LeetCode,算是为了明年的春招做点准备吧。在此分享出来和大家一起学习。如果有什么疑问可以在下面评论,我看到了会及时回复的。
今天是第二天。
Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
For example, Given [100, 4, 200, 1, 3, 2], e longest consecutive elements sequence is [1,
2, 3, 4]. Return its length: 4.
Your algorithm should run in O(n) complexity.
/**
* 2.1.6 Longest Consecutive Sequence
*/
public static int getLongestConsecutiveSequence(int[] A) {
if (A == null) {
return 0;
}
Set<Integer> set = new HashSet<Integer>();
for (int e : A) {
set.add(e);
}
int left = 0;
int right = 0;
int max = 1;
for (int e : A) {
int count = 0;
left = e - 1;
right = e + 1;
while (set.contains(e)) {
count++;
set.remove(e);
}
//上边界
while (set.contains(left)) {
count++;
set.remove(left);
left--;
}
//下边界
while (set.contains(right)) {
count++;
set.remove(right);
right++;
}
max = Math.max(max, count);
}
return max;
}
public static void getLongestConsecutiveSequenceTest() {
int[] A = {100, 4, 200, 1, 3, 2, 0, 5};
int length = getLongestConsecutiveSequence(A);
System.out.println("length : " + length);
}
3Sum
Given an array S of n integers, are there elements a, b, c in S such that a+b+c = 0? Find all unique
triplets in the array which gives the sum of zero.
Note:
• Elements in a triplet (a, b, c) must be in non-descending order. (ie, a ≤ b ≤ c)
• e solution set must not contain duplicate triplets.
For example, given array S = {-1 0 1 2 -1 -4}.
A solution set is:
(-1, 0, 1)
(-1, -1, 2)
/**
* 2.1.7 3Sum
*/
public static Set<List<Integer>> findAllSumOfZero(int[] A) {
Set<List<Integer>> set = new HashSet<List<Integer>>();
if (A == null) {
return null;
}
//排序
Arrays.sort(A);
for (int i = 0; i < A.length; i++) {
int prev = i + 1;
int last = A.length - 1;
while (prev < last) {
int sum = A[i] + A[prev] + A[last];
if (sum == 0) {
List<Integer> zero = new ArrayList<Integer>();
zero.add(A[i]);
zero.add(A[prev]);
zero.add(A[last]);
if (!set.contains(zero)) {
set.add(zero);
}
prev++;
last--;
} else if (sum > 0) {
last--;
} else {
prev++;
}
}
}
return set;
}
public static void findAllSumOfZeroTest() {
int[] A = {-1, 0, 1, 2, -1, -4};
Set<List<Integer>> list = findAllSumOfZero(A);
System.out.println(list);
}
3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given number,
target. Return the sum of the three integers. You may assume that each input would have exactly one
solution.
For example, given array S = {-1 2 1 -4}, and target = 1.
e sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
/**
* 2.1.8 3Sum Closest
*/
public static int findClosestSumOfTarget(int[] A, int target) {
if (A == null) {
return 0;
}
Arrays.sort(A);
int diff = 0; //目标值和三数之和的差值
int closest = A[0] + A[1] + A[2]; //设定初始值
for (int i = 0; i < A.length; i++) {
int prev = i + 1;
int last = A.length - 1;
while (prev < last) {
int sum = A[i] + A[prev] + A[last];
diff = target - sum;
if (diff > 0) {
prev++;
} else if (diff < 0) {
last--;
} else {
last--;
prev++;
}
if (Math.abs(target - closest) > Math.abs(diff)) {
closest = sum;
}
}
}
return closest;
}
public static void findClosestSumOfTargetTest() {
int[] A = {-1, 2, 1, -4};
int closest = findClosestSumOfTarget(A, 1);
System.out.println("closest : " + closest);
}
4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a+b+c+d = target?
Find all unique quadruplets in the array which gives the sum of target.
Note:
• Elements in a quadruplet (a, b, c, d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
• e solution set must not contain duplicate quadruplets.
For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
A solution set is:
(-1, 0, 0, 1)
(-2, -1, 1, 2)
(-2, 0, 0, 2)
/**
* 2.1.9 4Sum
*/
public static Set<List<Integer>> findAllSumOfTarget(int[] A, int target) {
if (A == null || A.length < 4) {
return null;
}
Set<List<Integer>> set = new HashSet<List<Integer>>();
Arrays.sort(A);
for (int i = 0; i < A.length - 3; i++) {
for (int j = i + 1; j < A.length - 2; j++) {
int prev = j + 1;
int last = A.length - 1;
while (prev < last) {
int sum = A[i] + A[j] + A[prev] + A[last];
if (sum == target) {
List<Integer> list = new ArrayList<Integer>();
list.add(A[i]);
list.add(A[j]);
list.add(A[prev]);
list.add(A[last]);
if (!set.contains(list)) {
System.out.println(list);
set.add(list);
}
prev++;
last--;
} else if (sum > target) {
last--;
} else {
prev++;
}
}
}
}
return set;
}
public static void findAllSumOfTargetTest() {
int[] A = {1, 0, -1, 0, -2, 2};
int target = 0;
Set<List<Integer>> set = findAllSumOfTarget(A, target);
}
Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.
e order of elements can be changed. It doesn’t maer what you leave beyond the new length.
/**
* 2.1.10 Remove Element
*/
public static int removeElement(int[] A, int value) {
if (A == null) {
return -1;
}
int index = 0;
for (int i =0; i< A.length; i++) {
if (A[i] != value) {
A[index++] = A[i];
}
}
return index+1;
}
public static void removeElementTest(){
int[] A = {1,2,3,4,5,5,5,6,7};
int value = 5;
int index = removeElement(A,value);
System.out.println("index : "+index);
}