标签:
一直直到bug-free。不能错任何一点。
思路不清晰:刷两天。
做错了,刷一天。
直到bug-free。高亮,标红。
1, two sum - https://leetcode.com/problems/two-sum/
1. Two Sum QuestionEditorial Solution My Submissions Total Accepted: 260442 Total Submissions: 1044409 Difficulty: Easy Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
Cur: 第一天:没有bug-free。因为map.containsKey.
思路:利用hashmap来存下他对应的目标值,和他的位置。
public class Solution { public int[] twoSum(int[] nums, int target) { int[] result = new int[2]; HashMap<Integer, Integer> map = new HashMap(); for (int i = 0; i < nums.length; ++i) { if (!map.containsKey(nums[i])) { // if (!map.contains(nums[i])) { map.put(target - nums[i], i); } else { result[0] = map.get(nums[i]); result[1] = i; break; } } return result; } }
2,3sum--https://leetcode.com/problems/3sum/
15. 3Sum QuestionEditorial Solution My Submissions Total Accepted: 129979 Total Submissions: 668400 Difficulty: Medium 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: 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] ]
Cur : 思路不清晰。
思路:先sort,然后利用一个i来循环,然后用双指针找他后面的那些能够和他组合在一起的。注意的是,当找的时候left++并且right--,并且要看他的相邻是否有重复元素,有的话要跳过他们。
public class Solution { public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> result = new ArrayList(); if (nums == null || nums.length < 3) { return result; } Arrays.sort(nums); for (int i = 0; i < nums.length - 2; ++i) { if (i != 0 && nums[i] == nums[i - 1]) { continue; } int left = i + 1; int right = nums.length - 1; while (left < right) { int sum = nums[i] + nums[left] + nums[right]; if (sum == 0) { List<Integer> list = new ArrayList(); list.add(nums[i]); list.add(nums[left]); list.add(nums[right]); result.add(list); // left++; left++; right--; while (left < right && nums[left] == nums[left - 1]) { left++; } while (left < right && nums[right] == nums[right + 1]) { right--; } } else if (sum > 0) { right--; } else { left++; } } } return result; } }
3,3sum closest---https://leetcode.com/problems/3sum-closest/
16. 3Sum Closest QuestionEditorial Solution My Submissions Total Accepted: 85414 Total Submissions: 287898 Difficulty: Medium 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).
Cur: 思路不清晰。
思路:先sort,然后外层循环i,然后双指针找他后面的跟他组合之后最接近的值。如果当前的sum 与target的值接近的话就更新bestSum。当前sum < target, left++;
public class Solution { public int threeSumClosest(int[] nums, int target) { Arrays.sort(nums); int bestSum = nums[0] + nums[1] + nums[2]; for (int i = 0; i < nums.length - 2; ++i) { int left = i + 1; int right = nums.length - 1; while (left < right) { int curSum = nums[i] + nums[left] + nums[right]; if (Math.abs(target - curSum) < Math.abs(target - bestSum)) { bestSum = curSum; } if (curSum == target) { return target; } else if (curSum < target) { left++; } else { right--; } } } return bestSum; } }
4, 4sum---https://leetcode.com/problems/4sum/
18. 4Sum QuestionEditorial Solution My Submissions Total Accepted: 79948 Total Submissions: 327112 Difficulty: Medium 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: The 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] ]
Cur:思路不清晰。
思路:先sort,然后外层是i,然后是j= i + 1,然后left,right双指针来找。
public class Solution { public List<List<Integer>> fourSum(int[] nums, int target) { List<List<Integer>> results = new ArrayList(); if (nums == null || nums.length < 4) { return results; } Arrays.sort(nums); for (int i = 0; i < nums.length - 3; ++i) { if (i != 0 && nums[i] == nums[i - 1]) { continue; } for (int j = i + 1; j < nums.length - 2; ++j) { if (j != i + 1 && nums[j] == nums[j - 1]) { continue; } int left = j + 1; int right = nums.length - 1; while (left < right) { int sum = nums[i] + nums[j] + nums[left] + nums[right]; if (sum == target) { List<Integer> list = new ArrayList(); list.add(nums[i]); list.add(nums[j]); list.add(nums[left]); list.add(nums[right]); results.add(list); left++; right--; while (left < right && nums[left] == nums[left - 1]) { left++; } while (left < right && nums[right] == nums[right + 1]) { right--; } } else if (sum < target) { left++; } else { right--; } } } } return results; } }
标签:
原文地址:http://www.cnblogs.com/yueyebigdata/p/5689812.html