Two Sum Problem (Python) - HOW TO NAIL LeetCode Interview Questions

preview_player
Показать описание
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.

***************************************

***************************************
ADDITIONAL RESOURCES:

6 Python Tips and Tricks YOU Should Know ►

How to NAIL LeetCode Questions– Valid Parentheses ►

Sqlite 3 Python Tutorial in 5 minutes - Creating Database, Tables and Querying►

***************************************
Don’t forget to subscribe :)

STAY TUNED:
Рекомендации по теме
Комментарии
Автор

These videos are great! I suggest talking a bit slower though so less advanced coders like myself have an easier time following :)

ashleyhaa
Автор

your runtime will decrease significantly if you use for i in range(0, len(nums)): rather than enumerate

satyamagrawal
Автор

Probably the best explanation out of the 4 videos I've invested watching

cvxcfv
Автор

Hi, at what point did the elements get added to the dictionary

njokuiyke
Автор

Why do we map the elements and indexes to the dict after our if statement??

faisalabdulkadir
Автор

Can someone explain line 14? When did anything get added to the dictionary? To me, it seems to be empty so I don't yet understand why we're checking if the compliment is there :o

Samantha-noes
Автор

You can have more than one pair for the same target. The enhanced version of your code follows:
1 #!/usr/bin/python
2
3 from collections import namedtuple
4 import os
5 import sys
6
7 Pair = namedtuple('Pair', 'x1, x2, v1, v2')
8
9 def two_sum(list, target):
10 values = dict()
11 pairs = []
12 for i, elem in enumerate(list):
13 comp = target - elem
14 if comp in values:
15 pairs.append(Pair(values[comp], i, list[values[comp]], list[i]))
16 pass
17 values[elem] = i
18 pass
19 return pairs
20 pass
21
22 def main():
23 arr = [1, 2, 3, 4, 5, 6, 7]
24 tgt = 8
25 results = two_sum(arr, tgt)
26
27 for p in results:
28 print("Numbers are [{}]={} and [{}]={} to add {}".format(
29 p.x1, p.v1, p.x2, p.v2, tgt))
30 pass
31 pass
32
33 if __name__ == '__main__':
34 main()
35 print("** END **")
36 pass
37 pass

RegiiPad
Автор

so what was the point of this? to show everyone you can talk fast?

pomii
Автор

Hi
Your solution does not apply for an array like [3, 2, 4] and a target of 6
When you enter your if statement target minus 3 return 3 which is already in your array and stop the loop

SayfSentinel
Автор

I was under the impression line 18 would include the elif statement, and an else statement for line 20. How comes this isn't the case?

seemeyeah
Автор

values[elem] = i ; ====> why is that?

marufmazumder
Автор

reading the answers at that pace with not detail or visuals it no better than handing me the code, no need for a video for that.

frenchmike
Автор

I think you have over complicated the problem just run a nested loop where the second loop starts from one position after the current iterator value of the outer loop add the values stored in both the indexes and check if the equal your target that's it
for i in range(0, len(arr)):
for j in range(i, len(arr)):
If are[i]+arr[j]==target:
return i, j
return False

aakashjana