[프로그래머스/Java] Lv.2 최댓값과 최솟값

이은정·2024년 9월 15일

프로그래머스/Java

목록 보기
33/74

문제

코드

  1. stream 이용
import java.util.*;
import java.util.stream.Collectors; 

class Solution {
    public String solution(String s) {
        String answer = "";
        List<Integer> nums = Arrays.stream(s.split(" "))
            .map(Integer::parseInt)
            .collect(Collectors.toList());
        
        
        Integer max = Collections.max(nums);
        Integer min = Collections.min(nums);
        
        return min + " " + max;
    }
}
  1. for문 이용
import java.util.*;

class Solution {
    public String solution(String s) {
        String[] str = s.split(" ");
        
        List<Integer> nums = new ArrayList<>();
        
        for (int i = 0; i < str.length; i ++) {
            nums.add(Integer.parseInt(str[i]));
        }
        
        
        Integer max = Collections.max(nums);
        Integer min = Collections.min(nums);
        
        return min + " " + max;
    }
}

결과

  1. stream 이용

  2. for문 이용

profile
돈 많은 백수가 꿈인 백엔드 개발자 지망생

0개의 댓글