File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ public class Solution {
2+ public int [ ] TwoSum ( int [ ] nums , int target ) {
3+ Dictionary < int , int > lookup = new Dictionary < int , int > ( ) ;
4+ for ( var i = 0 ; i < nums . Length ; i ++ ) {
5+ if ( lookup . ContainsKey ( target - nums [ i ] ) ) {
6+ return new int [ ] { lookup [ target - nums [ i ] ] , i } ;
7+ }
8+ lookup [ nums [ i ] ] = i ;
9+ }
10+ return new int [ ] { } ;
11+ }
12+ }
Original file line number Diff line number Diff line change 1+ # Time: O(n^2), n is the number of disctinct A[i]
2+ # Space: O(n)
3+
4+ import collections
5+ import itertools
6+
7+
8+ class Solution(object):
9+ def threeSumMulti(self, A, target):
10+ """
11+ :type A: List[int]
12+ :type target: int
13+ :rtype: int
14+ """
15+ count = collections.Counter(A)
16+ result = 0
17+ for i, j in itertools.combinations_with_replacement(count, 2):
18+ k = target - i - j
19+ if i == j == k:
20+ result += count[i] * (count[i]-1) * (count[i]-2) // 6
21+ elif i == j != k:
22+ result += count[i] * (count[i]-1) // 2 * count[k]
23+ elif max(i, j) < k:
24+ result += count[i] * count[j] * count[k]
25+ return result % (10**9 + 7)
You can’t perform that action at this time.
0 commit comments