পাঠসূচি
  1. 11. Two Sum - বাংলায় LeetCode - পাইথন
  2. 2118. Pascal's Triangle - বাংলায় LeetCode - Python
  3. 3217. Contains Duplicate - বাংলায় LeetCode - পাইথন
  4. 4242. Valid Anagram - বাংলায় LeetCode - পাইথন

1. Two Sum - বাংলায় LeetCode - পাইথন

LeetCode · পাঠ 1 · English · ভিডিও বাংলায়

1. Two Sum - বাংলায় LeetCode - পাইথনWatch on YouTube

ভিডিওটির ভাষা বাংলা।

Contents

[A1] - Brute Force

Time: O(n^2)

Space: O(1)

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        for i in range(len(nums)):
            for j in range(i+1, len(nums)):
                if nums[i]+nums[j]==target:
                    return [i,j]
        return

[A2] - Hash Table

Time: O(n)

Space: O(n)

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        dict = {}
        for i,e in enumerate(nums):
            dict[e]=i
        
        for i in range(len(nums)):
            diff = target-nums[i]
            if diff in dict and i!=dict[diff]:
                return [i,dict[diff]]
        return

Comment: There is another way where you can solve the problem using a single iteration. Try to do it by yourself.

কোনো ভুল পেয়েছেন? LinkedIn-এ জানান।