美文网首页
twoSum && threeSum

twoSum && threeSum

作者: Ewitter | 来源:发表于2019-09-14 22:41 被阅读0次

from leetcode.com/problems

twoSum

Description:

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, 
  and you may not use the same element twice.

Example:
  Given nums = [2, 7, 11, 15], target = 9,
  Because nums[0] + nums[1] = 2 + 7 = 9,
  return [0, 1].

Possible Ans:

/* 1) insert all elements of array into a multimap<elementType, int, less<elementType> >, 
        parameter of int means index
 * 2) when inserting ith element,find element equals to target-array[i] in multimap
 * OR
 * 1) insert all elements into multimap<elementType, int, less<elementType> >, 
        parameter of int means index;
 * 2) beg = multimap.beg(), end = multimap();
 * 3) if *beg+*end==target, return vector<int>({beg->second,end->second}).
 */
vector<int> twoSum(vector<int>& nums, int target)
{
    if (nums.size() > 1)
    {
        int s = nums.size();
        multimap<int, int> mmap;
        for (int i = 0; i < s; ++i)
        {
            multimap<int, int>::iterator iter = mmap.find(target - nums[i]);
            if (iter != mmap.end())
            {
                return vector<int>({ iter->second,i });
            }
            mmap.insert(make_pair(nums[i], i));
        }
    }
    return vector<int>();
}

threeSum

Description:

Given an array nums of n integers, 
    are there elements a, b, c in nums 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.

Example:
  Given array nums = [-1, 0, 1, 2, -1, -4],
  A solution set is:
    [
      [-1, 0, 1],
      [-1, -1, 2]
    ]

Possible Ans:

/* 1) sort ASC
 * 2) ith element, low = i +1, high = len(array)-1;
 * 3) in array[low,high] , this is some like twoSum problem.
 */
vector<vector<int> > threeSum(vector<int>& nums) {
    if (nums.size() < 3)
    {
        return vector<vector<int> >();
    }
    int len = nums.size();
    sort(nums.begin(), nums.end());

    vector<vector<int> > res;

    for (int i = 0; i < len - 2; ++i)
    {
        if (nums[i] > 0)
        {
            break;
        }
        if (i > 0 && nums[i] == nums[i - 1] )
        {
            continue;
        }
        int low = i + 1;
        int high = len - 1;
        int target = -nums[i];
        while (low < high)
        {
            while (low < high && nums[low] + nums[high] < target)
            {
                ++low;
            }
            while (low < high && nums[low] + nums[high] > target)
            {
                --high;
            }
            if (low < high && nums[low] + nums[high] == target)
            {
                res.push_back({ nums[i], nums[low],nums[high] });
                ++low;
                --high;
                /* deal with duplicate elements */
                while (low < high && nums[low] == nums[low - 1])
                {
                    ++low;
                }
                while (low < high && nums[high] == nums[high + 1])
                {
                    --high;
                }
            }
        }
    }
    return res;
}

相关文章

  • twoSum && threeSum

    from leetcode.com/problems twoSum Description: Possible A...

  • task

    task1.python实现三数之和,(正整数) class threesum: def threeSum(s...

  • 1.4.15 ThreeSum

    来见证不同算法的性能差距吧吧:

  • ThreeSum问题

    ThreeSum问题:计算所有不同的整数的三元组的和,统计和为0的数量。上述代码是最简单直接的求解方式。 通过简单...

  • 3Sum

    class Solution {public: vector> threeSum(vector& nums)...

  • JavaScript三数之和

    var threeSum = function (nums) {if (nums.length < 3) {ret...

  • 3Sum

    /*dfs 算法 时间超时 class Solution { public List> threeSum(i...

  • TwoSum

    题目大意: 找到数组中两个元素相加等于指定数的所有组合 情况一:给定数组中不含重复元素,且均为正整数 思路: 使用...

  • twoSum

    Problem Given an array of integers, return indices of the...

  • TwoSum

    刷题当然要从TwoSum开始了~~python刷题果然容易~~~class Solution(object):de...

网友评论

      本文标题:twoSum && threeSum

      本文链接:https://www.haomeiwen.com/subject/onvzectx.html