Algorithms
-
프로그래머스 - 정수 제곱근 판별 [자바]Algorithms/- 프로그래머스 2022. 2. 1. 17:48
문제 링크 : 정수 제곱근 판별 코딩테스트 연습 - 정수 제곱근 판별 임의의 양의 정수 n에 대해, n이 어떤 양의 정수 x의 제곱인지 아닌지 판단하려 합니다. n이 양의 정수 x의 제곱이라면 x+1의 제곱을 리턴하고, n이 양의 정수 x의 제곱이 아니라면 -1을 리턴하는 함 programmers.co.kr 정답 class Solution { public long solution(long n) { long x = (long) Math.sqrt(n); if(n == (long)Math.pow(x, 2)){ return (long)Math.pow(x+1, 2); }else{ return -1L; } } } 분석 int의 입력값의 범위 대략 21억을 알고 있는지 java Math 라이브러리를 알고 있는지에 따..
-
[자바] 기수 변환 (e.g. 10진수 ↔ 2진수 변환)Algorithms 2022. 2. 1. 12:27
TL;DR 1. 10진수 정수 -> n 진수 문자열 int num = 123; String num2Str = Integer.toString(num, 2); //2진수 String num3Str = Integer.toString(num, 3); //3진수 String num8Str = Integer.toString(num, 8); //8진수 String num16Str = Integer.toString(num, 16); //16진수 System.out.println(num2Str); //1111011 System.out.println(num3Str); //11120 System.out.println(num8Str); //173 System.out.println(num16Str); //7b 16진수 : 0,..
-
프로그래머스 - 다트 게임 [자바]Algorithms/- 프로그래머스 2022. 2. 1. 02:34
문제 링크 : 다트 게임 코딩테스트 연습 - [1차] 다트 게임 programmers.co.kr 정답 import java.util.*; import java.util.stream.*; class Solution { public int solution(String dartResult) { int answer = 0; //숫자 뽑아내기 String d = dartResult; String[] arr = d.split("[^0-9]"); arr = Arrays.stream(arr).filter(x -> !x.equals("")).toArray(String[]::new); String[] arr2 = d.split("[0-9]"); arr2 = Arrays.stream(arr2).filter(x -> !x.e..
-
프로그래머스 - 가운데 글자 가져오기 [자바]Algorithms/- 프로그래머스 2022. 2. 1. 02:29
문제 링크 : 가운데 글자 가져오기 코딩테스트 연습 - 가운데 글자 가져오기 단어 s의 가운데 글자를 반환하는 함수, solution을 만들어 보세요. 단어의 길이가 짝수라면 가운데 두글자를 반환하면 됩니다. 재한사항 s는 길이가 1 이상, 100이하인 스트링입니다. 입출력 예 s ret programmers.co.kr 정답 class Solution { public String solution(String s) { String answer = ""; int length = s.length(); int index = length / 2; if(length % 2 == 0){ answer = s.substring(index-1, index+1); }else{ answer = String.valueOf(s...
-
프로그래머스 - 비밀지도 [자바]Algorithms/- 프로그래머스 2022. 2. 1. 02:20
문제 링크 : 비밀지도 코딩테스트 연습 - [1차] 비밀지도 비밀지도 네오는 평소 프로도가 비상금을 숨겨놓는 장소를 알려줄 비밀지도를 손에 넣었다. 그런데 이 비밀지도는 숫자로 암호화되어 있어 위치를 확인하기 위해서는 암호를 해독해야 한다. 다 programmers.co.kr 정답 class Solution { public String[] solution(int n, int[] arr1, int[] arr2) { String[] answer = new String[n]; /* #는 하나라도 있으면 # - 1 " "는 둘 다 일치해야 " " - 0 */ for(int i=0; i
-
프로그래머스 - 부족한 금액 계산하기 [자바]Algorithms/- 프로그래머스 2022. 2. 1. 02:13
문제 링크 : 부족한 금액 계산하기 코딩테스트 연습 - 부족한 금액 계산하기 새로 생긴 놀이기구는 인기가 매우 많아 줄이 끊이질 않습니다. 이 놀이기구의 원래 이용료는 price원 인데, 놀이기구를 N 번 째 이용한다면 원래 이용료의 N배를 받기로 하였습니다. 즉, 처음 이 programmers.co.kr 정답 class Solution { public long solution(int price, int money, int count) { long answer = 0L; /* price 이용료 n 배 count 번 모자른 금액 return But 부족하지 않으면 0 return */ long total = 0L; for(int i=1; i
-
프로그래머스 - 문자열 압축 [자바]Algorithms/- 프로그래머스 2022. 2. 1. 01:26
문제 링크 : 문자열 압축 코딩테스트 연습 - 문자열 압축 데이터 처리 전문가가 되고 싶은 "어피치"는 문자열을 압축하는 방법에 대해 공부를 하고 있습니다. 최근에 대량의 데이터 처리를 위한 간단한 비손실 압축 방법에 대해 공부를 하고 있는데, 문 programmers.co.kr 정답 class Solution { public int solution(String s) { int len = s.length(); int answer = len; for(int i=1; i 1){ min += (int)Math.log10(cnt) + 1; } str1 = str2; cnt = 1; } } min += str1.length(); if(cnt > 1){ min += (int)Math.log10(cnt) + 1; }..
-
프로그래머스 - 자릿수 더하기 [자바]Algorithms/- 프로그래머스 2022. 1. 31. 22:18
문제 링크 : 자릿수 더하기 코딩테스트 연습 - 자릿수 더하기 자연수 N이 주어지면, N의 각 자릿수의 합을 구해서 return 하는 solution 함수를 만들어 주세요. 예를들어 N = 123이면 1 + 2 + 3 = 6을 return 하면 됩니다. 제한사항 N의 범위 : 100,000,000 이하의 자연수 입출 programmers.co.kr 정답 형변환으로 풀고 싶을 때 public class Solution { public int solution(int n) { int answer = 0; String[] arr =String.valueOf(n).split(""); for(String str : arr){ answer += Integer.valueOf(str); } return answer; }..