Hi, this is my accepted Python3 solution.


  • 0
    T
    class Solution:
        def twoSum(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: List[int]
            """
            temp = sorted(nums)
    
            count = len(temp)
            for i in range(int(count / 2 + 1)):
                for j in range(count - 1, i, -1):
                    aaa = temp[i] + temp[j]
                    if aaa == target:
                        a = nums.index(temp[i])
                        if temp[i] != temp[j]:
                            b = nums.index(temp[j])
                        else:
                            b = nums.index(temp[j], a + 1)
                        if a > b:
                            a, b = b, a
                        return [a, b]
                    elif aaa < target:
                        break
            return None
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.