码迷,mamicode.com
首页 > 其他好文 > 详细

BUG-FREE-For Dream

时间:2016-07-20 22:54:35      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:

一直直到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].
View Code

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;
    }
}
View Code

 

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]
]
View Code

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;
    }
}
View Code

 

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).
View Code

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;
    }
}
View Code

 

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]
]
View Code

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;
    }
}
View Code

 

BUG-FREE-For Dream

标签:

原文地址:http://www.cnblogs.com/yueyebigdata/p/5689812.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!