Day50

강태훈·2026년 3월 13일

nbcamp TIL

목록 보기
50/58

코드카타 - 알고리즘

SQL 코드카타 완료 -> 다시 알고리즘 문제로~

문자열을 정수로 바꾸기

class Solution {
    public int solution(String s) {
        int answer = 0;
        
        answer = Integer.valueOf(s);
        return answer;
    }
}

정수 제곱근 판별

class Solution {
    public long solution(long n) {
        if(Math.sqrt(n) % 1 == 0){
            return (long) Math.pow(Math.sqrt(n) + 1, 2);
        }
        return -1L;
    }
}

하사드 수

class Solution {
    public boolean solution(int x) {
        int size = String.valueOf(x).length();
        int y = x;
        int[] list = new int[size];

        for(int i = 0; i < size; i++){
            list[i] = y%10;
            y /= 10;
        }
        int sum = 0;
        for(int z:list){
            sum += z;
        }
        if(x%sum != 0){
            return false;
        }
        
        return true;
    }
}

두 정수 사이의 합

class Solution {
    public long solution(int a, int b) {
        long answer = 0;
        if (a <= b) {
            for (int i = a; i <= b; i++) {
                answer += i;
            }
        } else{
            for (int i = b; i <= a; i++) {
                answer += i;
            }
        }
        return answer;
    }
}

콜라츠 추측

class Solution {
    public int solution(int num) {
        int answer = 0;
        long sol = (long) num;

        while (sol != 1){
            if(sol % 2 == 0){
                sol /= 2;
                answer++;
            }else{
                sol = (sol * 3) + 1;
                answer++;
            }
            if(answer >= 500){
                return -1;
            }
        }
        
        return answer;
    }
}

서울에서 김서방 찾기

class Solution {
    public String solution(String[] seoul) {
        int index=0;
        for (String kim:seoul){
            if("Kim".equals(kim)){
               return "김서방은 " + index + "에 있다"; 
            }
            index++;
        }
        return "못찾겠다 꾀꼬리";
    }
}

나누어 떨어지는 숫자배열

import java.util.Arrays;

class Solution {
    public int[] solution(int[] arr, int divisor) {
        int[] answer = Arrays.stream(arr)
                     .filter(i -> i % divisor == 0)
                     .sorted()
                     .toArray();
        if(answer.length == 0) {
            return new int[]{-1};
        }
        return answer;
    }
}

음양 더하기

import java.util.ArrayList;
import java.util.List;

class Solution {
    public int solution(int[] absolutes, boolean[] signs) {
        int answer = 0;
        int i = 0;
        List<Integer> solution = new ArrayList<>(); 
        for(int a:absolutes){
            if(signs[i]){
                solution.add(a);
            }else {
                solution.add(-a);
            }
            i++;
        }
        for(int b:solution){
            answer+=b;
        }
        return answer;
    }
}

핸드폰 번호 가리기

class Solution {
    public String solution(String phone_number) {
        String answer = "";
        int leng = phone_number.length() - 4;
        String last = phone_number.substring(leng);
        for (int i = 0; i < leng; i++) {
            answer += "*";
        }
        answer += last;
        return answer;
    }
}
  • 문자열.substring(시작위치, 끝위치);: 문자열(String)을 자를수있는 함수 시작위치부터 끝위치까지를 잘라낸다. 끝위치 생략가능
  • 문자열.replace([기존 문자],[바꿀 문자]): 자신이 바꾸고싶은 문자로 문자열을 치환시켜주는 기능
  • 문자열.replaceAll([정규식],[바꿀 문자]): 자신이 바꾸고싶은 문자로 문자열을 전부 치환시켜주는 기능, 특수문자로는 치환이 어려울수있다.
  • 문자열.replaceFirst([기존문자],[바꿀문자]): 문자열에 가장 처음 나오는 바꿀 문자를 자신이 바꾸고 싶은 문자로 치환
  • 문자열배열 = 대상문자열.split("기준문자");: 기준 문자를 기준으로 잘라서 배열에 넣어줌

없는 숫자 더하기

public int solution(int[] numbers) {
        int answer = 9*(9+1)/2;
        for(int i: numbers){
            answer-=i;
        }
        return answer;
    }
// 방법 2: 0~9 중 numbers에 포함되지 않은 숫자만 필터링하여 합산
         return IntStream.rangeClosed(0, 9)
             .filter(n -> Arrays.stream(numbers).noneMatch(number -> number == n))
             .sum();

제일 작은 수 제거하기

import java.util.Arrays;

public class Solution {
    public int[] solution(int[] arr) {
        if (arr == null || arr.length == 1){
            return new int[]{-1};
        }

        int min = Arrays.stream(arr).min().getAsInt();

        return Arrays.stream(arr).filter(x -> x != min).toArray();
    }
}

0개의 댓글