2 sum, 3 sum, 3 sum close

Source: Internet
Author: User

1. The Sum of the

Given an array of integers, return indices of the both numbers such that they add-to a specific target.

You may assume this each input would has exactly one solution.

Example:

Given nums = [2, 7, one, 2], target = 9,because nums[0] + nums[1] = + 7 = 9,return [0, 1].
 Public classSolution { Public int[] Twosum (int[] Nums,inttarget) {        int[] res =New int[2]; if(Nums = =NULL|| Nums.length = = 0)returnRes; HashMap<integer, integer> map =NewHashmap<>();  for(inti = 0; i < nums.length; i++){            if(Map.containskey (target-Nums[i])) {res[0] = Map.get (target-Nums[i]); res[1] =i;        } map.put (Nums[i], i); }        returnRes; }}

3 Sum

Given an array S of n integers, is 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]]
 Public classSolution { PublicList<list<integer>> Threesum (int[] nums) {List<List<Integer>> res =NewArraylist<>();        Arrays.sort (Nums);  for(inti = 0; i < nums.length-2; i++) {List<Integer> list =NewArraylist<>(); intleft = i + 1; intright = Nums.length-1; intsum = 0-Nums[i];  while(Left <Right ) {                if(Nums[left] + nums[right] = =sum)                    {List.add (nums[i]);                    List.add (Nums[left]);                    List.add (Nums[right]); if(!res.contains (list)) Res.add (NewArraylist<>(list));                    List.clear ();  while(Left < right && nums[left] = = nums[left+1]) left++;  while(Left < right && nums[right] = = Nums[right-1]) right--; Left++; Right--; }                Else if(Nums[left] + Nums[right] <sum) { Left++; }                Else Right--; }        }        returnRes; }}

3Sum Closest

Given an array S of n integers, find three integers in S such so the sum is closest to a give n number, target. Return the sum of the three integers. You may assume this each input would has exactly one solution.

    For example, given array S = {-1 2 1-4}, and target = 1.    The sum is closest to the target is 2. (-1 + 2 + 1 = 2).
 Public classSolution { Public intThreesumclosest (int[] Nums,inttarget)      {Arrays.sort (nums); intMin =Integer.max_value;  for(inti = 0; i < nums.length-2; i++){          intleft = i + 1; intright = Nums.length-1; intValue = target-Nums[i];  while(Left <Right ) {                if(Nums[left] + nums[right] = =value)returnTarget; if(min = = Integer.max_value | | Math.Abs (Nums[i] + nums[left] + nums[right]-target) < Math.Abs (Min-target)) Min= Nums[i] + nums[left] +Nums[right]; if(Nums[left] + nums[right] >value) right--; Else Left++ ; }            }                returnmin; }}

2 sum, 3 sum, 3 sum close

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.