본문 바로가기
CodingTest

코딩테스트 입문 - Day 16

by Jiwon_Loopy 2025. 4. 27.
반응형

편지


class Solution {
    public int solution(String message) {
        return message.length() * 2;
    }
}

가장 큰 수 찾기


class Solution {
    public int[] solution(int[] array) {
        int max = array[0];
        int idx = 0;
        for(int i =1; i<array.length;i++){
            if(array[i] > max){
                max = array[i];
                idx = i;
            }
        }
        return new int[] {max, idx};
    }
}

문자열 계산하기


class Solution {
    public int solution(String my_string) {
        String[] strArr = my_string.split(" ");
        int sum = Integer.parseInt(strArr[0]);
        for(int i = 1; i< strArr.length-1; i+=2){
            String symbol = strArr[i];
            int n = Integer.parseInt(strArr[i+1]);
            if(symbol.equals("+")){
                sum+=n;
            }else if(symbol.equals("-")){
                sum-=n;
            }
        }
        return sum;
    }
}

배열의 유사도


import java.util.*;

class Solution {
    public int solution(String[] s1, String[] s2) {
        ArrayList<String> answer = new ArrayList<>();
        for(String str1 : s1){
            for(String str2 : s2){
                if(str1.equals(str2) && !answer.contains(str1)){
                    answer.add(str1);
                }
            }
        }
        return answer.size();
    }
}
728x90
반응형

'CodingTest' 카테고리의 다른 글

코딩테스트 입문 - Day 18  (1) 2025.04.27
코딩테스트 입문 - Day 17  (0) 2025.04.27
코딩테스트 입문 - Day 15  (0) 2025.04.27
코딩테스트 입문 - Day 14  (1) 2025.04.27
프로그래머스 2레벨 - 무인도  (2) 2025.04.20