[프로그래머스, 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] 유클리드 호제법이란?
·
CodingTest/Programmers
1️⃣최대공약수 GCD(Greatest Common Divisor)두 자연수가 공통으로 갖는 약수들 중에서 가장 큰 값ex) 24와 18의 최대공약수는 6 2️⃣최소공배수 LCM(Least Common Multiple)두 자연수들의 배수들 중에서 공통된 가장 작은수ex) 24와 18의 최소공배수는 72 3️⃣유클리드 호제법 (Euclidean Algorithm)유클리드 알고리즘은 2개의 자연수의 최대공약수를 구하는 알고리즘호제법이란 두 수가 서로 상대방 수를 나누어서 결과 값을 얻어내는 알고리즘을 나타낸다. 2개의 자연수 a, b에 대해서 a를 b로 나눈 나머지를 r이라 하면( a%b=r 단, a>b) a와 b의 최대공약수는 b와 r의 최대공약수와 같다. 이 성질에 따라, b를 r로 나눈 나머지 r'를..
[프로그래머스, Java] 같은 숫자는 싫어
·
CodingTest/Programmers
💡풀이- Stack 사용import java.util.*;public class Solution { public int[] solution(int []arr) { Stack stack = new Stack(); for(int n : arr){ if(stack.isEmpty() || stack.peek() != n){ stack.push(n); } } return stack.stream().mapToInt(Integer::intValue).toArray(); }}💡풀이 2- Stack을 ArrayDeque로 구현import java.util.*;import jav..
[프로그래머스, Java] 직사각형 별찍기
·
CodingTest/Programmers
💡풀이import java.util.Scanner;class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); for (int i = 0; i 💡풀이 2- StringTokenizer, BufferedReader, BufferedWriter 사용해서 풀어보기 (속도 향상)import java.io.*;import java.util.StringTokenizer;class Solution { public static void main(String[] args)..
[프로그래머스, Java] 행렬의 덧셈
·
CodingTest/Programmers
💡풀이class Solution { public int[][] solution(int[][] arr1, int[][] arr2) { int answer[][] = new int[arr1.length][arr1[0].length]; for (int i = 0; i GitHub - okjunghyeon/Programmers_CodingTest: 프로그래머스 관련 코딩테스트 문제를 풀이한 저장소입니다.프로그래머스 관련 코딩테스트 문제를 풀이한 저장소입니다. Contribute to okjunghyeon/Programmers_CodingTest development by creating an account on GitHub.github.com
[프로그래머스, Java] 문자열 다루기 기본
·
CodingTest/Programmers
💡풀이- toCharArray() 활용class Solution { public boolean solution(String s) { for (char c : s.toCharArray()) { if(!Character.isDigit(c)) return false; } return s.length() == 4 || s.length() == 6; }}💡풀이 2- chars() 스트림 활용class Solution { public boolean solution(String s) { return s.chars().allMatch(Character::isDigit) ..