본문 바로가기
Study with me/프로그래머스 L0 마스터하기

프로그래머스 - L0 더크게합치기 // String ↔ int

by 외계나무 2023. 12. 3.

프로그래머스 - level 0 더 크게 합치기


▼ 내 풀이

class Solution {
    public int solution(int a, int b) {
        String aa = Integer.toString(a);
        String bb = Integer.toString(b);
        int ab = Integer.parseInt(aa+bb);
        int ba = Integer.parseInt(bb+aa);
        int answer = ab >= ba ? ab : ba;
        return answer;
    }
}
  • Integer.toString(n) → String

  • Integer.parseInt(str) → int


▼ 타인의 풀이 1

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;
    }
}
  • String+int → String

  • Integer.parseInt(str) → int


▼ 타인의 풀이 2

class Solution {
    public int solution(int a, int b) {
        String strA = String.valueOf(a);
        String strB = String.valueOf(b);
        String strSum1 = strA + strB;
        String strSum2 = strB + strA;
        return Math.max(Integer.valueOf(strSum1), Integer.valueOf(strSum2));
    }
}
  • String.valueOf(n) → String

  • Integer.valueOf(str) → int