Skip to content

Commit

Permalink
[level 1] Title: 가운데 글자 가져오기, Time: 11.03 ms, Memory: 78.1 MB -Baekjo…
Browse files Browse the repository at this point in the history
…onHub
  • Loading branch information
shinjaewon99 committed Sep 3, 2023
1 parent 4a5156d commit 2e136f0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# [level 1] 가운데 글자 가져오기 - 12903

[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/12903#)

### 성능 요약

메모리: 78.1 MB, 시간: 11.03 ms

### 구분

코딩테스트 연습 > 연습문제

### 채점결과

Empty

### 문제 설명

<p>단어 s의 가운데 글자를 반환하는 함수, solution을 만들어 보세요. 단어의 길이가 짝수라면 가운데 두글자를 반환하면 됩니다.</p>

<h6>재한사항</h6>

<ul>
<li>s는 길이가 1 이상, 100이하인 스트링입니다.</li>
</ul>

<h5>입출력 예</h5>
<table class="table">
<thead><tr>
<th>s</th>
<th>return</th>
</tr>
</thead>
<tbody><tr>
<td>"abcde"</td>
<td>"c"</td>
</tr>
<tr>
<td>"qwer"</td>
<td>"we"</td>
</tr>
</tbody>
</table>

> 출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public String solution(String s) {
String answer = "";
int sLength = s.length();

// 1. 단어 s 문자열의 길이가 짝수일 경우
if (sLength % 2 == 0) {
int div = sLength / 2;
answer += s.charAt(div - 1);
answer += s.charAt(div);
}
// 2. 단어 s 문자열의 길이가 홀수일 경우
else {
int div = sLength / 2;
answer += s.charAt(div);
}

return answer;
}
}

0 comments on commit 2e136f0

Please sign in to comment.