알고리즘 풀이 방법입니다.
문제(Problem) -> 생각(Think) -> 해결책(Solution) -> 리뷰(Review) 를 통해서 정리해서 작성합니다.
Problem📄

 

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

 

프로그래머스

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

programmers.co.kr


Think🤔

 

stream 사용법에 대해서 공부해야 될 필요가 있다.


Solution✍

 

class Solution {
    public int[] solution(String[] strlist) {
        int[] answer = new int[strlist.length];
        for(int i=0; i<strlist.length; i++){
            answer[i] = strlist[i].length();
        }
        return answer;
    }
}

 

Review🤩

 

import java.util.Arrays;

class Solution {
    public int[] solution(String[] strList) {
        return Arrays.stream(strList).mapToInt(String::length).toArray();
    }
}

 

mapToInt는 int로 형변환 

Arrays.stream을 사용하여 문자열 배열을 스트림으로 변환

mapToInt를 사용하여 각 문자열을 그 길이로 매핑 , toArray를 호출하여 정수 배열로 변환


 

+ Recent posts