lc015

package
v0.0.0-...-b071cee Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2023 License: GPL-3.0 Imports: 1 Imported by: 0

README

三数之和

1.问题描述

给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。

注意:答案中不可以包含重复的三元组。

2.示例

提示:

0 <= nums.length <= 3000
-105 <= nums[i] <= 105

示例 1:

输入:nums = [-1,0,1,2,-1,-4]
输出:[[-1,-1,2],[-1,0,1]]

示例 2:

输入:nums = []
输出:[]

示例 3:

输入:nums = [0]
输出:[]
``` 

## 3.解题
采用双指针方法
1.先将数组进行排序
2.使用for循环遍历数组,并在其中增加去重条件。for循环先固定一个数num[i],另外两个数采用动态指针的方法,初始值为i+1,len(nums)-1。
3.若三数之和大于0,则说明三数和太大,需将右指针左移;若小于0,则将左指针右移;若三数之和为0,需将三数追加到切片数组中,并进行去重。

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL