본문 바로가기

JAVA

JAVA(2022.09.07) - math함수

다른사람의 풀이) 

class Solution {
    public int solution(int[][] sizes) {
        int length = 0, height = 0;
        for (int[] card : sizes) {
            length = Math.max(length, Math.max(card[0], card[1]));
            height = Math.max(height, Math.min(card[0], card[1]));
        }
        int answer = length * height;
        return answer;
    }
}

어제 푼 <최소직사각형>을 해결한 다른 사람들의 코드를 리뷰하다가 감탄할만한 코드를 찾았다. 

for문이 아닌 Math함수를 통해서 사용할 수 있었을텐데 말이다. 

 

그래서 오늘은 Math함수를 복습하고자 한다. 

 

주요 메소드 

Math.abs() 절대값 반환 
Math.max() 최대값 반환 
Math.min() 최소값 반환 
Math.pow() 제곱
Math.sqrt() 제곱근  
static double ceil(double a) 올림
static double floor(double a) 내림 

나중에는 조금 더 clean하게 풀 수 있도록 해야겠다는 생각이 또 드는 지금이다.