나의 풀이
import java.util.*;
class Solution {
public static int gcd(int a, int b) {
for (int i = Math.min(a, b); i > 0; i--) {
if (a % i == 0 && b % i == 0) {
return i;
}
}
return 1;
}
public static int lcm(int a, int b) {
return a * b / gcd(a, b);
}
public static int solution(int[] arr) {
Stack<Integer> stack = new Stack<>();
for (int element : arr) {
if (stack.isEmpty()) {
stack.push(element);
} else {
stack.push(lcm(stack.pop(), element));
}
}
return stack.pop();
}
}
▶ stack.isEmpty() → 매우 자주 쓰인다.
'프로그래머스(자바) > LV.2(자바)' 카테고리의 다른 글
괄호 회전하기★★ →List의 rotate()를 이용, String → 리스트 (1) | 2022.12.27 |
---|---|
1차 캐시→ 익명 클래스★★, this참조변수, super() 생성자 (0) | 2022.12.26 |
끝말잇기→contains()★+words[i].charAt(words[i].length()-1)★ (0) | 2022.12.24 |
짝지어 제거하기→'문자비교' == , "문자열비교" equals, peek()★★+크레인 인형뽑기와 유사 (0) | 2022.12.24 |
카펫→ 리스트 배열넣기, 리스트 출력★★ (0) | 2022.12.24 |