일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- CS
- Baekjoon
- python
- programmers
- C++
- c
- 파이썬
- SECS-II
- 회원가입
- Computer Science
- Bitget
- Spring JPA
- 스포티파이
- 백준
- 비트겟
- SECS
- Gem
- 프로그래머스
- 자바
- SECS/GEM
- regression
- java
- modern c++
- SW Expert Academy
- SWEA
- spring boot
- 회귀
- spotify
- Spring
- Spotify Api
Archives
- Today
- Total
비버놀로지
[Programmers 프로그래머스] 68644 두 개 뽑아서 더하기 (JAVA) 본문
728x90
programmers.co.kr/learn/courses/30/lessons/68644
코딩테스트 연습 - 두 개 뽑아서 더하기
정수 배열 numbers가 주어집니다. numbers에서 서로 다른 인덱스에 있는 두 개의 수를 뽑아 더해서 만들 수 있는 모든 수를 배열에 오름차순으로 담아 return 하도록 solution 함수를 완성해주세요. 제한
programmers.co.kr
for을 돌면서 모든 수를 더해서 TreeSet에 넣어준다.
TreeSet에 넣어줄 경우 중복을 제거해주고, 오른차순으로 정렬을 해준다.
import java.util.TreeSet;
class Solution {
public int[] solution(int[] numbers) {
int answer[];
TreeSet<Integer> tset=new TreeSet<Integer>(); //오름차순정렬과 중복제거를 위해 사용
for (int i = 0; i < numbers.length; i++) {
for (int j = i+1; j < numbers.length; j++) {
tset.add(numbers[i]+numbers[j]); //첫번째수와 나머지 모든수를 더해준 값을 넣어준다.
}
}
answer=new int[tset.size()];
int cnt=0;
for (Integer i : tset) {
answer[cnt]=i;
cnt++;
}
return answer;
}
}
728x90
'ALGORITM > JAVA' 카테고리의 다른 글
[Programmers 프로그래머스] 42840 모의고사 (0) | 2021.01.07 |
---|---|
[Programmers 프로그래머스] 42576 완주하지 못한 선수 (0) | 2021.01.06 |
[Programmers 프로그래머스] 64061 크레인 인형뽑기 게임 (0) | 2021.01.06 |
[SWEA SW Expert Academy] 5658 보물상자 비밀번호 (0) | 2020.12.02 |
[SWEA SW Expert Academy] 1249 보급로 (0) | 2020.12.01 |