1、Container With Most Water
鏈接:https://leetcode.com/problems/container-with-most-water/
思路:從數組頭尾向中間移動。取較小的那邊移動,因爲隔板盛水的最大值,在距離極可能大的情況下,另一邊>=當前高度 時取得。
public int maxArea(int[] height) {
int len = height.length,area = 0, i= 0, j = len - 1;
while(i < j){
area = area > Math.min(height[i], height[j]) *(j - i) ? area : Math.min(height[i], height[j]) *(j - i);
if(height[i] > height[j])
j--;
else
i++;
}
return area;
}
2、Two Sum
鏈接:https://leetcode.com/problems/two-sum/
思路:引入map,縮短時間
public int[] twoSum(int[] nums, int target) {
int[] result = new int[2];
int i = 0;
HashMap<Integer,Integer> map = new HashMap<>();
for(int j : nums){
map.put(j,i++);
}
for(int j = 0; j < nums.length; j++){
int temp = target - nums[j];
if(map.containsKey(temp) && map.get(temp) != j){
result[0] = j;
result[1] = map.get(temp);
return result;
}
}
return result;
}
3、3Sum
鏈接:https://leetcode.com/problems/3sum/
思路:先排序;確定一個數,遍歷時,頭尾開始,中途跳過一些數
public List<List<Integer>> threeSum(int[] nums) {
int len = nums.length;
Arrays.sort(nums);
List<List<Integer>> list = new ArrayList<>();
int i, j, k;
for(i = 0; i < len - 2; i++){
j = i+1;
k = len -1;
while(j < k){
if(nums[i] + nums[j] + nums[k] == 0){
List<Integer> listnew = new ArrayList<>();
listnew.add(nums[i]);
listnew.add(nums[j]);
listnew.add(nums[k]);
list.add(listnew);
j++;
k--;
while(j < k && nums[j] == nums[j-1]){
j++;
}
while(k > j && nums[k] == nums[k+1]){
k--;
}
}else if(nums[i] + nums[j] + nums[k] > 0){
k--;
}else j++;
while(i < len-2 && nums[i] == nums[i+1]){
i++;
}
}
}
return list;
}
4、3Sum Closest
鏈接:https://leetcode.com/problems/3sum-closest/
思路:排序,確定一個數,頭尾開始遍歷
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int len = nums.length;
int a = Math.abs(nums[0] + nums[1] + nums[2] - target);
int sum, n, j, k, result = nums[0] + nums[1] + nums[2];
for(int i = 0; i < len -2; i++){
j = i + 1;
k = len - 1;
while(j < k){
sum = nums[i] + nums[j] + nums[k];
if(sum == target){
return sum;
}else if(sum > target){
k--;
}else{
j++;
}
n = Math.abs(sum-target);
if(n < a){
a = n;
result = sum;
}
}
}
return result;
}
4、4Sum
鏈接:https://leetcode.com/problems/4sum/
思路:先排序,與前面思路一樣,固定兩個,頭尾開始遍歷,注意不能重複,可以使用set避免重複,或者通過跳過相同值避免重複。
public List<List<Integer>> fourSum(int[] nums, int target) {
Arrays.sort(nums);
int len = nums.length;
List<List<Integer>> list = new ArrayList<>();
for(int i = 0; i < len - 3; i++){
for(int j = i + 1; j < len - 2; j++){
int p = j + 1, q = len - 1;
while( p < q ){
if(nums[i] + nums[j] + nums[p] + nums[q] < target){
p++;
}else if(nums[i] + nums[j] + nums[p] + nums[q] > target){
q--;
}else{
List<Integer> list1 = new ArrayList<>();
list1.add(nums[i]);
list1.add(nums[j]);
list1.add(nums[p]);
list1.add(nums[q]);
list.add(list1);
do{
p++;
}while( p < q && nums[p] == nums[p-1]);
do{
q--;
}while( p < q && nums[q] == nums[q+1]);
}
}
while( j < len - 2 && nums[j] == nums[j+1]){
j++;
}
}
while( i < len - 3 && nums[i] == nums[i+1]){
i++;
}
}
return list;
}
public List<List<Integer>> fourSum(int[] nums, int target) {
Arrays.sort(nums);
int len = nums.length;
HashSet<List<Integer>> set = new HashSet<>();
for(int i = 0; i < len - 3; i++){
for(int j = i + 1; j < len - 2; j++){
int p = j + 1, q = len - 1;
while( p < q ){
if(nums[i] + nums[j] + nums[p] + nums[q] == target){
List<Integer> list1 = new ArrayList<>();
list1.add(nums[i]);
list1.add(nums[j]);
list1.add(nums[p]);
list1.add(nums[q]);
set.add(list1);
p++;
q--;
}else if(nums[i] + nums[j] + nums[p] + nums[q] > target){
q--;
}else p++;
}
}
}
return new ArrayList<>(set);
}
5、Set Matrix Zeroes
鏈接:https://leetcode.com/problems/set-matrix-zeroes/
思路:先遍歷第一行,第一列是否有零,保存標識;將其餘行、列存在的0,保存在第一行、第一列中,然後遍歷第一行、第一列;最後處理先前保存的標識。
public void setZeroes(int[][] matrix) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0)
return;
int leni = matrix.length, lenj = matrix[0].length;
boolean zerorow = false, zerocol = false;
for(int i = 0; i < leni; i++)
if(matrix[i][0] == 0){
zerocol = true;
break;
}
for(int j = 0; j < lenj; j++)
if(matrix[0][j] == 0){
zerorow = true;
break;
}
for(int i = 1; i < leni; i++)
for(int j = 1; j < lenj; j++){
if(matrix[i][j] == 0){
matrix[0][j] = 0;
matrix[i][0] = 0;
}
}
for(int i = 1; i < leni; i++)
for(int j = 1; j < lenj; j++){
if(matrix[i][0] == 0 || matrix[0][j] == 0)
matrix[i][j] = 0;
}
if(zerorow){
for(int j= 0; j < lenj; j++)
matrix[0][j] = 0;
}
if(zerocol){
for(int i= 0; i < leni; i++)
matrix[i][0] = 0;
}
}