2019-08-12【LeekCode题库problem-15】

CoffeeRabbit · · 1178 次点击 · · 开始浏览

GitHub 地址：coffeeTu-code

LeekCode 地址：题库

Language：Golang

/*

@题目

@示例:

[

[-1, 0, 1],

[-1, -1, 2]

]

@题目大意

@解题思路

*/

``````func threeSum(nums []int) [][]int {
res := [][]int{}
counter := make(map[int]int, len(nums))
for _, value := range nums {
counter[value]++
}

uniqNums := []int{}
for key, _ := range counter {
uniqNums = append(uniqNums, key)
}
sort.Ints(uniqNums)

for i := 0; i < len(uniqNums); i++ {
if (uniqNums[i] == 0) && counter[uniqNums[i]] >= 3 {
res = append(res, []int{uniqNums[i], uniqNums[i], uniqNums[i]})
}
for j := i + 1; j < len(uniqNums); j++ {
if (uniqNums[i]*2+uniqNums[j] == 0) && counter[uniqNums[i]] > 1 {
res = append(res, []int{uniqNums[i], uniqNums[i], uniqNums[j]})
}
if (uniqNums[j]*2+uniqNums[i] == 0) && counter[uniqNums[j]] > 1 {
res = append(res, []int{uniqNums[i], uniqNums[j], uniqNums[j]})
}
c := 0 - uniqNums[i] - uniqNums[j]
if c > uniqNums[j] && counter[c] > 0 {
res = append(res, []int{uniqNums[i], uniqNums[j], c})
}
}
}
return res
}

``````

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

GitHub 地址：coffeeTu-code

LeekCode 地址：题库

Language：Golang

/*

@题目

@示例:

[

[-1, 0, 1],

[-1, -1, 2]

]

@题目大意

@解题思路

*/

``````func threeSum(nums []int) [][]int {
res := [][]int{}
counter := make(map[int]int, len(nums))
for _, value := range nums {
counter[value]++
}

uniqNums := []int{}
for key, _ := range counter {
uniqNums = append(uniqNums, key)
}
sort.Ints(uniqNums)

for i := 0; i < len(uniqNums); i++ {
if (uniqNums[i] == 0) && counter[uniqNums[i]] >= 3 {
res = append(res, []int{uniqNums[i], uniqNums[i], uniqNums[i]})
}
for j := i + 1; j < len(uniqNums); j++ {
if (uniqNums[i]*2+uniqNums[j] == 0) && counter[uniqNums[i]] > 1 {
res = append(res, []int{uniqNums[i], uniqNums[i], uniqNums[j]})
}
if (uniqNums[j]*2+uniqNums[i] == 0) && counter[uniqNums[j]] > 1 {
res = append(res, []int{uniqNums[i], uniqNums[j], uniqNums[j]})
}
c := 0 - uniqNums[i] - uniqNums[j]
if c > uniqNums[j] && counter[c] > 0 {
res = append(res, []int{uniqNums[i], uniqNums[j], c})
}
}
}
return res
}

``````