munch2024 / munch

2 stars 11 forks source link

Code refactoring Task2.1 #118

Closed Fabrizv closed 8 months ago

Fabrizv commented 8 months ago

I got this code from my LeetCode. It is a two sum question that leetCode asks. The issue is that it has redundancies. Goals:

  1. Fix the redundancies
  2. Optimize the code

Code Snippet:

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        end = len(nums)

        for i in range(len(nums)):
            myst = target - nums[i]
            next = i+1

            while next < end:
                if myst == nums[next]:
                    return [i, next]
                else:
                    next+=1