Skip to content

Commit 305c520

Browse files
author
Partho Biswas
committed
no message
1 parent 69b1155 commit 305c520

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-9
lines changed

leetcode.com/python/15_3Sum.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,36 @@ def threeSum(self, nums):
5454
return result
5555

5656

57-
sol = Solution()
58-
# input = [82597,-9243,62390,83030,-97960,-26521,-61011,83390,-38677,12333,75987,46091,83794,19355,-71037,-6242,-28801,324,1202,-90885,-2989,-95597,-34333,35528,5680,89093,-90606,50360,-29393,-27012,53313,65213,99818,-82405,-41661,-3333,-51952,72135,-1523,26377,74685,96992,92263,15929,5467,-99555,-43348,-41689,-60383,-3990,32165,65265,-72973,-58372,12741,-48568,-46596,72419,-1859,34153,62937,81310,-61823,-96770,-54944,8845,-91184,24208,-29078,31495,65258,14198,85395,70506,-40908,56740,-12228,-40072,32429,93001,68445,-73927,25731,-91859,-24150,10093,-60271,-81683,-18126,51055,48189,-6468,25057,81194,-58628,74042,66158,-14452,-49851,-43667,11092,39189,-17025,-79173,13606,83172,92647,-59741,19343,-26644,-57607,82908,-20655,1637,80060,98994,39331,-31274,-61523,91225,-72953,13211,-75116,-98421,-41571,-69074,99587,39345,42151,-2460,98236,15690,-52507,-95803,-48935,-46492,-45606,-79254,-99851,52533,73486,39948,-7240,71815,-585,-96252,90990,-93815,93340,-71848,58733,-14859,-83082,-75794,-82082,-24871,-1
59-
input = [-1, 0, 1, 2, -1, -4]
60-
triplets = sol.threeSum(input)
61-
print("Result: ", triplets)
57+
58+
# My solution during Mock
59+
class Solution(object):
60+
def threeSum(self, nums):
61+
"""
62+
:type nums: List[int]
63+
:rtype: List[List[int]]
64+
"""
65+
if not nums or len(nums) < 2:
66+
return []
67+
sortedNums = sorted(nums)
68+
results = []
69+
resultMap = {}
70+
for first in range(0, len(sortedNums) - 2):
71+
firstNum = sortedNums[first]
72+
second, third = first + 1, len(sortedNums) - 1
73+
while second < third:
74+
secondNum, thirdNum = sortedNums[second], sortedNums[third]
75+
if (firstNum + secondNum + thirdNum) == 0:
76+
key = str(firstNum) + "-" + str(secondNum) + "-" + str(thirdNum)
77+
if key not in resultMap:
78+
results.append([firstNum, secondNum, thirdNum])
79+
resultMap[key] = 1
80+
second += 1
81+
third -= 1
82+
elif (firstNum + secondNum + thirdNum) > 0:
83+
third -= 1
84+
else:
85+
second += 1
86+
return results
6287

6388

6489

leetcode.com/python/17_Letter_Combinations_of_a_Phone_Number.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,42 @@ def backtrack(self, currentCombination, leftoverDigits, alphaDigitalMap, allComb
3131
currentCombination = currentCombination[:-1] # Backtrack, remove last character from currentCombination
3232

3333

34+
# My solution during mock
35+
class Solution(object):
36+
def letterCombinations(self, digits):
37+
"""
38+
:type digits: str
39+
:rtype: List[str]
40+
"""
41+
if not digits:
42+
return []
43+
digitsMap = {
44+
'2': 'abc',
45+
'3': 'def',
46+
'4': 'ghi',
47+
'5': 'jkl',
48+
'6': 'mno',
49+
'7': 'pqrs',
50+
'8': 'tuv',
51+
'9': 'wxyz'
52+
}
53+
54+
letters = []
55+
for digit in digits:
56+
letters.append(digitsMap[digit])
57+
58+
allCombinations = []
59+
self.letterCombinationsHelper(letters, 0, [], allCombinations)
60+
return allCombinations
61+
62+
def letterCombinationsHelper(self, letters, currentIdx, currentComb, allComb):
63+
if currentIdx >= len(letters):
64+
allComb.append("".join(currentComb))
65+
return
66+
67+
for letter in letters[currentIdx]:
68+
currentComb.append(letter)
69+
self.letterCombinationsHelper(letters, currentIdx + 1, currentComb, allComb)
70+
currentComb.pop() # backtrack
3471

3572

36-
sol = Solution()
37-
input = "23"
38-
output = sol.letterCombinations(input)
39-
print('Res: ', output)

0 commit comments

Comments
 (0)