연산 ⊕는 두 정수에 대한 연산으로 두 정수를 붙여서 쓴 값을 반환합니다. 예를 들면 다음과 같습니다.
12 ⊕ 3 = 123
3 ⊕ 12 = 312
양의 정수 a와 b가 주어졌을 때, a ⊕ b와 b ⊕ a 중 더 큰 값을 return 하는 solution 함수를 완성해 주세요.
단, a ⊕ b와 b ⊕ a가 같다면 a ⊕ b를 return 합니다.
int를 String으로 바꾼다음 각 문자를 concat으로 합치고 비교하면 되지 않을까?
class Solution {
public int solution(int a, int b) {
int answer = 0;
String strA = Integer.toString(a);
String strB = Integer.toString(b);
String ab = strA.concat(strB);
String ba = strB.concat(strA);
int intAB = Integer.parseInt(ab);
int intBA = Integer.parseInt(ba);
if(intAB>intBA){
answer = intAB;
} else if(intAB < intBA){
answer = intBA;
} else if(intBA == intAB){
answer = intAB;
}
return answer;
}
}
class Solution {
public int solution(int a, int b) {
String strAB = Integer.toString(a) + Integer.toString(b);
String strBA = Integer.toString(b) + Integer.toString(a);
return Integer.parseInt(strAB) > Integer.parseInt(strBA) ? Integer.parseInt(strAB) : Integer.parseInt(strBA);
}
}
삼항연산자를 사용해서 한줄로도 깔끔하게 표기할수 있다!
intBA가 큰 경우를 제외하고는 모두 intAB가 나오니까 더 깔끔하게 작성가능하다.