[經典] 指定數目與和問題

來源:互聯網
上載者:User

標籤:

Two Sum I

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].

分析:如果數組已經排序完,那麼只需要雙指標從兩端往中間夾即可。但是沒有排序的話,先排序複雜度就已經O(nlogn)了,所以沒法用排序法。事實上是有O(n)的hash法:首先遍曆一遍,全部放入hashtable(unordered_set)中,複雜度為O(n);然後再對於每個數x,都在hashtable中找target - x的數在不在其中,如果在,就結束,因為題目說假設只有一個答案,複雜度為O(n);總複雜度為O(n)。

 

Two Sum II - Input array is sorted 

不用排序的排序法,因為數組已經排序完,從兩端往中間夾的複雜度與hash法一樣也為O(n);但理論時間開銷應該比hash法小的。

 

Two Sum III - Data structure design

略

 

3 Sum

與Two Sum題目類似,二元換成了三元。兩種解法,複雜度均為O(n^2)

  1. hash法,把兩兩的和放到一個hashmap(unordered_multimap<int,pair<int,int>>),三個int分別為“和”,“第一個index”,“第二個index”。從頭到尾匹配,遇到index在兩個之中的,則非解;不在兩個中的,則為合法解。
  2. 排序法,然後對任何一個選取x,都在x後面從兩邊往中間夾,選取和為target - x的

 

3 Sum Closest

這個3 Sum的排序法,可解出,複雜度為O(n ^ 2)。沒法用hash法。

 

3 Sum Smaller

Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target.

For example, given nums = [-2, 0, 1, 3], and target = 2.

Return 2. Because there are two triplets which sums are less than 2:

[-2, 0, 1][-2, 0, 3]

Follow up: Could you solve it in O(n^2) runtime?

暴力解法是O(n^3)。用Sum的排序法,可接出,複雜度為O(n ^ 2)。

沒法用hash法,頂多用multimap做,但複雜度為O(n^2*logn)。

 

4 Sum

從二元到三元再到四元

  1. 用排序法,複雜度有O(n^3)
  2. 用hash法,複雜度為O(n^2),通過兩個unorder_multiple<int, pair<int, int>>,然後比對pair中的index是否重複了,來決定結果是否符合要求。

 

【總結】到這裡為止,我們基本也可以總結出這NSum類型的解法:

  1. 排序法是萬能的,複雜度為O(max(nlogn, n^(N-1));
  2. hash法能也不錯,複雜度為O(n^(ceiling(log2N)))

 

【Combination Sum I】

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

The same repeated number may be chosen from C unlimited number of times.

Note:

  • All numbers (including target) will be positive integers.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.

For example, given candidate set 2,3,6,7 and target 7, 
A solution set is: 
[7] 
[2, 2, 3]

 

【Combination Sum II】

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

Each number in C may only be used once in the combination.

For example, given candidate set 10,1,2,7,6,1,5 and target 8, 
A solution set is: 
[1, 7] 
[1, 2, 5] 
[2, 6] 
[1, 1, 6] 

 

【Combination Sum III】

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.

Ensure that numbers within the set are sorted in ascending order.

Example:

Input: k = 3, n = 9

Output:

[[1,2,6], [1,3,5], [2,3,4]]

思路

[經典] 指定數目與和問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.