코테풀이
[프로그래머스] 최댓값과 최솟값 자바 풀이 (level 2)
헝D
2023. 1. 17. 21:35
https://school.programmers.co.kr/learn/courses/30/lessons/12939?language=java
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
나의풀이
import java.lang.Math;
class Solution {
public String solution(String s) {
String answer = "";
String[] cstr=s.split(" ");
int maxNum=Integer.parseInt(cstr[0]),minNum=Integer.parseInt(cstr[0]);
for(String i:cstr){
maxNum=Math.max(maxNum, Integer.parseInt(i));
minNum=Math.min(minNum, Integer.parseInt(i));
}
answer+=Integer.toString(minNum)+" "+Integer.toString(maxNum);
return answer;
}
}
Tip.
문자열을 잘라서 배열로 만들고 싶을때 split 함수를 사용하기
최댓값 최솟값을 구하고 싶을때 java.lang.Math 의 max, min 함수를 이용하기