Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- postgresql
- CS지식
- baekjoon
- 데이터베이스
- 리눅스
- 파이썬
- 자바스크립트
- javascript
- Java
- Spring Security
- Flutter
- 플러터
- 데이터
- springboot
- 스프링 부트 쇼핑몰 프로젝트 with JPA
- 자료구조
- 자바
- 시큐리티
- backjoon
- DB
- 네트워크
- 프로그래머스
- spring
- 스프링
- Oracle
- 스프링부트
- JPA
- 백준
- python
Archives
- Today
- Total
Jin's Dev Story
[프로그래머스 Lv0.] 181927번 마지막 두 원소 (JAVA) 본문
1. 문제
문제 설명
정수 리스트 num_list가 주어질 때, 마지막 원소가 그전 원소보다 크면 마지막 원소에서 그전 원소를 뺀 값을 마지막 원소가 그전 원소보다 크지 않다면 마지막 원소를 두 배한 값을 추가하여 return하도록 solution 함수를 완성해주세요.
제한사항
- 2 ≤ num_list의 길이 ≤ 10
- 1 ≤ num_list의 원소 ≤ 9
입출력 예
num_list | result |
[2, 1, 6] | [2, 1, 6, 5] |
[5, 2, 1, 7, 5] | [5, 2, 1, 7, 5, 10] |
2. 풀이과정
class Solution {
public int[] solution(int[] num_list) {
int[] answer = new int[num_list.length + 1];
for(int i=0; i<num_list.length; i++)
answer[i] = num_list[i];
if(num_list[num_list.length-1] > num_list[num_list.length-2])
answer[num_list.length] += num_list[num_list.length-1] - num_list[num_list.length-2];
else
answer[num_list.length] += num_list[num_list.length-1] * 2;
return answer;
}
}
'Coding Test > 프로그래머스[JAVA]' 카테고리의 다른 글
[프로그래머스 Lv0.] 120875번 평행 (JAVA) (0) | 2024.09.25 |
---|---|
[프로그래머스 Lv0.] 120876번 겹치는 선분의 길이 (JAVA) (0) | 2024.09.25 |
[프로그래머스 Lv0.] 120923번 연속된 수의 합 (JAVA) (0) | 2023.07.23 |
[프로그래머스 Lv0.] 120924번 다음에 올 숫자 (JAVA) (0) | 2023.07.23 |
[프로그래머스 Lv0.] 120863번 다항식 더하기 (JAVA) (0) | 2023.07.23 |