- 숫자를 문자열로 형변환 해서 더하기 --> 형변환을 꼭 하지 않아도 "" 와 같이 더하면 됨!
- 크기 비교할 때 if 만 떠올렸는데, Math.max() 함수를 쓸 수 있었음. 수학적 사고 필요함~~
- 간단한 비교문은 삼항연산자를 떠올리자~
[Java]
String strA = String.valueOf(a);
String strB = String.valueOf(b);
int ab = Integer.parseInt(strA + strB);
int ba = Integer.parseInt(strB + strA);
if (ba > ab) return ba;
else return ab;
문자열 변환, 결합, 정수 변환이 단계별로 분리되어 있어 디버깅이 용이합니다.
중간 변수 선언과 여러 메서드 호출로 인해 약간의 성능 저하가 있을 수 있습니다.
int aLong = Integer.parseInt(""+a+b);
int bLong = Integer.parseInt(""+b+a);
answer = aLong > bLong ? aLong : bLong;
불필요한 변수 없이 바로 비교하여 반환하여 효율적입니다.
"" + a + b와 같은 문자열 결합 방식은 String.valueOf()보다 명확성이 떨어질 수 있습니다.
return Math.max(Integer.parseInt(a + "" + b), Integer.parseInt(b + "" + a));
Math.max()를 사용하여 두 값 중 더 큰 값을 찾는 방식이 직관적입니다.
https://olrlobt.tistory.com/61
https://hianna.tistory.com/524
[Python]
return int(max(f"{a}{b}", f"{b}{a}"))
return max(int(str(a)+str(b)), int(str(b)+str(a)))