[Python] LeetCode Python TwoSums

Discussão em 'Python' iniciado por Stack, Março 2, 2025.

  1. Stack

    Stack Membro Participativo

    I am absolutely a noob on python, and just started to practice on leetcode. Anyway take a look at this TwoSum exercise: Given an array of integers, find two numbers such that they add up to a specific target number.

    Here is my code for this exercise:

    class Solution(object):

    def __init__(self, nums, target):
    self.nums = nums
    self.target = target

    def twoSum(self):
    for i in range(len(self.nums)):
    for j in range(i+1, len(self.nums)):
    if self.nums + self.nums[j] == self.target:
    print "index1=" + str(i) + ", index2=" + str(j)

    sample = Solution([2, 8, 7, 15], 9)
    sample.twoSum()


    Anyone can help me how should the leetcode algorithm answer be look like? Will this one be OK for an interview? Thanks

    Continue reading...

Compartilhe esta Página