Algorithm/알고리즘

프로그래머스 가운데 글자 가져오기

한둥둥 2024. 4. 5. 14:30

https://school.programmers.co.kr/learn/courses/30/lessons/12903

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

import java.util.*;

class Solution {
    public String solution(String s) {
        StringBuilder answer = new StringBuilder();
        
        if(s.length()%2==0){
            int index = s.length()/2;
            answer.append(s.substring(index-1, index+1));
        }else {
            int index = s.length()/2;
            answer.append(s.substring(index, index+1));
        }     
        return answer.toString();
    }
}

 

예전에는.. 프로그래머스 일 단계도 어려웠던거 같은데.. 이제는 쉽다 다행이네..! 

/2로 나눈 길이를 가지고 절반을 출력시켜주면되니깐 인덱스를 앞뒤로 움직인다는 컨셉으로 풀게됨..!