Accepted Swift solution


  • 0
    M

    Probably not the best but it works fine

    class Solution {
        func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
            var arr: [Int] = []
    
            for i in 0..<nums.count{
                var tmp = nums[i]
                if(i < nums.count-1){
                    for j in i+1..<nums.count{
    
                        if(tmp + nums[j] == target){
                            arr.append(i)
                            arr.append(j)
                        }
                    }
                }
            }
            return arr
        }
    }
    

Log in to reply
 

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