Skip to content

Commit 171e9c7

Browse files
Merge pull request #42 from siyapandeyvsp/master
leetcode C# problem
2 parents 059ec0c + 63551ad commit 171e9c7

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

create two-sum.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
}

sum-with-multiplicity

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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)

0 commit comments

Comments
 (0)