몬그로이

코드카타 본문

TIL

코드카타

Mon Groy 2024. 6. 4. 20:00

정수 제곱근 판별

class Solution {
    public long solution(long n) {
 
        for (long x = 1; x <= n ; x++) {
            if (n/== x) {
                return (x + 1)*(x + 1);
            }
        }
        return -1;
    }
}
 
cs

 

정수 내림차순으로 배치하기

 

class Solution {
   public static long solution(long n) {
 
        int length = String.valueOf(n).length();
        int[] numbers = new int[length];
 
        for (int i = 0 ; i < length ; i++ ) {
            int x = (int) n % 10;
            n = n/10;
            numbers[i] = x;
        }
 
        Arrays.sort(numbers);
        int descNumber = 0;
        int j = 1;
        for (int i = 0 ; i < length; i++) {
            descNumber += numbers[i] * j;
            j *= 10;
        }
 
        return descNumber;
    }
}
cs

 

'TIL' 카테고리의 다른 글

TIL_033  (0) 2024.06.05
TIL_032  (0) 2024.06.04
TIL_031  (0) 2024.06.03
TIL_030  (0) 2024.06.01
TIL_028  (0) 2024.05.30