본문 바로가기

코딩테스트/JAVA

[코딩테스트][JAVA] 더 크게 합치기

문제 :  https://school.programmers.co.kr/learn/courses/30/lessons/181939

 

프로그래머스

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

programmers.co.kr

난이도 : LV0


내 풀이

class Solution {
    public int solution(int a, int b) {
        int answer = 0;
        // 문자열로 변환
        String str1 = Integer.toString(a);
        String str2 = Integer.toString(b);
        
        // 문자열 연산하고 int로 반환
        int num1 = Integer.parseInt(str1 + str2);
        int num2 = Integer.parseInt(str2 + str1);
        
        // 두 수를 대소 비교하여 더 큰 값을 저장
        answer = num1 > num2 ? num1 : num2;
                
        return answer;
    }
}

다른 사람 풀이

class Solution {
    public int solution(int a, int b) {
        int answer = 0;
        int aLong = Integer.parseInt(""+a+b);
        int bLong = Integer.parseInt(""+b+a);
        answer = aLong > bLong ? aLong : bLong;

        return answer;
    }
}