Skip to content

Instantly share code, notes, and snippets.

@marsp0
Created April 17, 2018 14:49
Show Gist options
  • Select an option

  • Save marsp0/e32bebb0e6da9ecd1853fa4bc52f2fbd to your computer and use it in GitHub Desktop.

Select an option

Save marsp0/e32bebb0e6da9ecd1853fa4bc52f2fbd to your computer and use it in GitHub Desktop.

Revisions

  1. Martin Spasov created this gist Apr 17, 2018.
    19 changes: 19 additions & 0 deletions two_sum_2.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    package main

    //Naive approach
    func twoSumNaive(numbers []int, target int) []int {
    var toReturn = []int{}
    for i := 0; i < len(numbers); i++ {
    var current = numbers[i]
    var toFind = target - current
    for j := i + 1; j < len(numbers); j++ {
    if numbers[j] == toFind {
    toReturn = append(toReturn, i+1)
    toReturn = append(toReturn, j+1)
    } else if numbers[j] > toFind {
    break
    }
    }
    }
    return toReturn
    }