반응형

 

 

출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges

 

 

 

 

** Javascript

 

 

function solution(s) {
    if (s.length % 2 === 1) {
        return s[parseInt(s.length / 2)];
    } else {
        return `${s[s.length / 2 - 1]}${s[s.length / 2]}`;
    }
}

 

 

** Python

 

def solution(s):
    return s[len(s) // 2] if len(s) % 2 else s[len(s) // 2 - 1 : len(s) // 2 + 1]

 

 

** Java

 

import java.util.*;
class Solution {
    public String solution(String s) {
        int median = Math.round(s.length() / 2);
        String[] ss = s.split("");
        return s.length() % 2 == 1 ? ss[median] : String.join("", Arrays.copyOfRange(ss, median - 1, median + 1));
    }
}

 

참고: https://stackoverflow.com/questions/11001720/get-only-part-of-an-array-in-java

 

Get only part of an Array in Java?

I have an array of Integers in Java, I would like use only a part of it. I know in Python you can do something like this array[index:] and it returns the array from the index. Is something like this

stackoverflow.com

 

 

 

 

반응형

+ Recent posts