[프로그래머스, Java] 삼총사
·
CodingTest/Programmers
💡풀이class Solution { public int solution(int[] number) { int answer = 0; for(int i=0; i GitHub - okjunghyeon/Programmers_CodingTest: 프로그래머스 관련 코딩테스트 문제를 풀이한 저장소입니다.프로그래머스 관련 코딩테스트 문제를 풀이한 저장소입니다. Contribute to okjunghyeon/Programmers_CodingTest development by creating an account on GitHub.github.com
[프로그래머스, Java] 3진법 뒤집기
·
CodingTest/Programmers
💡풀이class Solution { public int solution(int n) { String toThird = Integer.toString(n, 3); System.out.println(toThird); StringBuilder sb = new StringBuilder(toThird); return Integer.parseInt(sb.reverse().toString(), 3); }} 📖새로 배운 부분Integer.toString(n, 3);- 10진법 숫자를 3진법의 문자열로 변환 Integer.parseInt(str, 3);- 3진법으로 된 문자열을 10진법 숫자로 변환 GitHub - okjunghyeon/Programmers..
[프로그래머스, Java] 예산
·
CodingTest/Programmers
💡풀이- 오름차순 정렬하고 값을 더해나가면서 최대 부서 개수 측정import java.util.Arrays;class Solution { public int solution(int[] d, int budget) { Arrays.sort(d); for(int sum = 0, count = 0; count budget) return count; } return d.length; }} GitHub - okjunghyeon/Programmers_CodingTest: 프로그래머스 관련 코딩테스트 문제를 풀이한 저장소입니다.프로그래머스 관련 코딩테스트 문제를 풀이한 저장소입니다. Contribute to okjunghyeon/Progra..
[프로그래머스, Java] 크기가 작은 부분문자열
·
CodingTest/Programmers
💡풀이class Solution { public int solution(String t, String p) { long pValue = Long.parseLong(p); int count = 0; for (int i = 0; i 💡풀이 2class Solution { public int solution(String t, String p) { int count = 0; for (int i = 0; i 📖새로 배운 부분주어진 문자를 변환해야 하는 경우 int 의 범위를 벗어나는지 항상 확인하자 GitHub - okjunghyeon/Programmers_CodingTest: 프로그래머스 관련 코딩테스트 문제를 풀이한 저장소입니다.프..
[프로그래머스, Java] 최대공약수와 최소공배수
·
CodingTest/Programmers
💡풀이class Solution { public int[] solution(int n, int m) { // 유클리드 호제법을 사용하여 최대공약수와 최소공배수를 구하는 방법 int a = Math.max(n, m); int b = Math.min(n, m); while (b > 0) { int temp = a % b; a = b; b = temp; } int gcd = a; // 최대공약수 greatest common divisor int lcm = n * m / gcd; // 최소공배수 least common multiple return ..
[Java, Stack] 왜 Stack이 아닌 Deque로 스택을 구현해야 할까?
·
Backend/Java
Stack 클래스가 상속하고 있는 Vector가 문제가 되기 때문에 Deque 사용을 권장한다. ❌ Stack stack = new Stack();⭕ Deque stack = new ArrayDeque(); Vector는 자바 버전 1부터 있었던 굉장히 오래된 클래스이기 때문에 여러모로 취약점이 많다. 1️⃣Stack은 Vector를 상속받아 구현되어 있다.상속으로 부모 메서드를 공유하기 때문에 Stack을 사용자가 잘못되게 사용할 수 있는 문제가 발생한다.Vector에는 원하는 index에 객체를 끼워 넣을 수 있는 insertElementAt이라는 메서드가 존재한다.문제는 Stack이 Vector를 상속받았기 때문에 insertElementAt 사용이 가능하다는 점이다.후입 선출이 보장되어야 하..