15.3sums leetcode java

1392 단어
1. 제목
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note: The solution set must not contain duplicate triplets.
For example, given array S = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]

n 의 수 를 정 한 배열 이 존재 하 는 지, 그 중 세 개의 수 를 0 으로 더 하면 이 조건 을 만족 시 키 는 모든 3 원 그룹 을 찾 을 수 있 습 니 다.
2. 사고방식
먼저 배열 을 정렬 한 다음 에 각 배열 을 옮 겨 다 닌 다. 예 를 들 어 nums [i] 에 옮 겨 다 닐 때 nums [i [결과 로 집 중 된 하나의 숫자 로 해당 되 는 것 은 2sum 문제 로 바 뀌 었 다. 바로 그 다음 숫자 에서 - nums [i] 와 같은 숫자 두 개 를 찾 는 것 이다. 주의 하 는 점 은 같은 숫자 를 뛰 어 넘 는 것 이다.
3. 알고리즘
오류:
class Solution {
    List> res=new  ArrayList>();
    public List> threeSum(int[] nums) {
		Arrays.sort(nums);
		for(int i=0;i0)
				end--;
			else {
				List ans=new ArrayList();
				ans.add(nums[i]);
				ans.add(nums[start]);
				ans.add(nums[end]);
				res.add(ans);
				while(start

① 처 로 도 쓸 수 있다
while(i!=0&&i

좋은 웹페이지 즐겨찾기