본문 바로가기

Backend/Algorithm

(33)
29. 에라토스테네스의 체 - 소수찾기 코드 class Solution { public int solution(int n) { int [] arr = new int [n+1]; for(int a=2; a
28. 다차원 배열 - 행렬의 덧셈 코드 class Solution { public int[][] solution(int[][] arr1, int[][] arr2) { int [][] answer = new int [arr1.length][arr1[0].length]; for(int i=0; i
27. double - 평균 구하기 코드 class Solution { public double solution(int[] arr) { double answer = 0; for(int i=0; i
26. ArrayList - 약수의 합 코드 import java.util.ArrayList; class Solution { public int solution(int n) { int answer = 0; ArrayList list = new ArrayList(); for(int i=1; i
25. ArrayList - 같은 숫자는 싫어 코드 import java.util.*; public class Solution { public int[] solution(int []arr) { ArrayList array = new ArrayList(); int cur = 10; for (int i=0; i
24. 문자열 반복 - 수박수박수박수박수박수? 코드 class Solution { public String solution(int n) { String answer = ""; for(int i=1; i
23. Array - 배열의 회전 [문제] * 배열의 회전이란 모든 원소를 오른쪽으로 한 칸씩 이동시키고, 마지막 원소는 배열의 맨 앞에 넣는 것을 말합니다. * 두 배열 arrA와 arrB가 매개변수로 주어질 때, arrA를 회전해 arrB로 만들 수 있으면 true를, 그렇지 않으면 false를 return 하는 solution 함수를 작성해주세요. [제한 조건] arrA는 길이가 1 이상 1,500 이하인 배열입니다. arrA의 원소는 0 이상 1,500 이하인 정수입니다. arrB는 길이가 1 이상 1,500 이하인 배열입니다 arrB의 원소는 0 이상 1,500 이하인 정수입니다. 코드 public class Solution { public boolean solution(int[] arrA, int[] arrB) { boolea..
22. Stack - 짝지어 제거하기 코드 import java.util.Stack; class Solution{ public int solution(String s){ Stack stack = new Stack(); for(char c : s.toCharArray()) { if(!stack.empty() && stack.peek() == c) { stack.pop(); }else{ stack.push(c); } } int solution = stack.empty() ? 1 : 0; return solution; } } 접근방법