题目描述
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0
public class Solution {
public int searchInsert(int[] A, int target) {
int a = 0;
int b = A.length - 1;
while(a <= b) {
int n = (a + b) / 2;
if(A[n] == target) {
return n;
} else if(A[n] < target) {
a = n + 1;
} else {
b = n - 1;
}
}
return a;
}
}
题目描述
Given a sorted array of integers, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return[-1, -1].
For example,
Given[5, 7, 7, 8, 8, 10]and target value 8,
return[3, 4].
public class Solution {
public int[] searchRange(int[] A, int target) {
int a = 0;
int b = A.length - 1;
while(a <= b) {
int n = (a + b) / 2;
if(A[n] == target) {
int m = n;
while (n > 0) {
if(A[n - 1] == target) {
n--;
} else {
break;
}
}
while (m < A.length - 1) {
if(A[m + 1] == target) {
m++;
} else {
break;
}
}
int[] res = {n, m};
return res;
} else if(A[n] < target) {
a = n + 1;
} else {
b = n - 1;
}
}
int[] res = {-1, -1};
return res;
}
}
题目描述
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
public class Solution {
public int search(int[] A, int target) {
int a = 0;
int b = A.length - 1;
while(a <= b) {
int n = (a + b) / 2;
if(A[n] == target) {
return n;
} else if((A[n] < target && A[n] < A[b] && target <= A[b]) || (A[n] > A[b] && !(A[a] <= target && target < A[n])) ) {
a = n + 1;
} else {
b = n - 1;
}
}
return -1;
}
}
题目描述
Given a string containing just the characters'('and')', find the length of the longest valid (well-formed) parentheses substring.
For"(()", the longest valid parentheses substring is"()", which has length = 2.
Another example is")()())", where the longest valid parentheses substring is"()()", which has length = 4.
import java.util.Stack;
public class Solution {
public int longestValidParentheses(String s) {
char[] c = s.toCharArray();
int result = 0;
int pre = -1;
Stack<Integer> stack = new Stack<>();
for(int i = 0; i < c.length; i++) {
if(c[i] == '(') {
stack.push(i);
} else {
if(stack.size() == 0) {
pre = i;
} else {
stack.pop();
if(!stack.isEmpty()) {
result = result > (i - stack.peek()) ? result : (i - stack.peek());
} else {
result = result > (i - pre) ? result : (i - pre);
}
}
}
}
return result;
}
}
题目描述
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. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
import java.util.Arrays;
public class Solution {
public int threeSumClosest(int[] num, int target) {
int result = Integer.MAX_VALUE;
if (num == null || num.length < 0) {
return result;
}
Arrays.sort(num);
int temp = 0;
int low = 0;
int high = 0;
int close = Integer.MAX_VALUE;
for (int i = 0; i < num.length - 2; i++) {
low = i + 1;
high = num.length - 1;
while (low < high) {
temp = num[low] + num[high] + num[i];
if(temp == target) {
return target;
}
if(temp > target) {
high--;
} else {
low++;
}
if(close > Math.abs(temp - target)) {
result = temp;
close = Math.abs(temp - target);
}
}
}
return result;
}
}
题目描述
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)
- The 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)
import java.util.ArrayList;
import java.util.Arrays;
public class Solution {
public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
ArrayList<ArrayList<Integer>> result = new ArrayList<>();
if (num == null || num.length < 0) {
return result;
}
Arrays.sort(num);
int temp = 0;
int low = 0;
int high = 0;
for (int i = 0; i < num.length - 2; i++) {
if (i != 0 && num[i] == num[i - 1]) {
continue;
}
low = i + 1;
high = num.length - 1;
while (low < high) {
temp = num[low] + num[high];
if (temp + num[i] == 0) {
ArrayList<Integer> solution = new ArrayList<>();
solution.add(num[i]);
solution.add(num[low]);
solution.add(num[high]);
result.add(solution);
if(num[low] == num[high]) {
break;
}
low++;
high--;
while (low < high && num[low] == num[low - 1]) {
low++;
}
while (low < high && num[high] == num[high + 1]) {
high--;
}
} else if (temp + num[i] < 0) {
low++;
} else {
high--;
}
}
}
return result;
}
}