View

๋ฐ˜์‘ํ˜•

์ž์—ฐ์ˆ˜ ๋’ค์ง‘์–ด ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค๊ธฐ

https://school.programmers.co.kr/learn/courses/30/lessons/12932?language=java


๋ฌธ์ œ ์„ค๋ช…

์ž์—ฐ์ˆ˜ n์„ ๋’ค์ง‘์–ด ๊ฐ ์ž๋ฆฌ ์ˆซ์ž๋ฅผ ์›์†Œ๋กœ ๊ฐ€์ง€๋Š” ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ๋ฆฌํ„ดํ•ด์ฃผ์„ธ์š”. ์˜ˆ๋ฅผ๋“ค์–ด n์ด 12345์ด๋ฉด [5,4,3,2,1]์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

์ œํ•œ ์กฐ๊ฑด

  • n์€ 10,000,000,000์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

n return

12345 [5,4,3,2,1]

import java.util.*;

class Solution {
    public int[] solution(long n) {

        /*
            1. ์ž…๋ ฅ๋ฐ›์€ ์ˆซ์ž๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜
            2. ๋ฌธ์ž์—ด ๋’ค์ง‘์Œ reversed ์‚ฌ์šฉ
            3. ๋ฌธ์ž์—ด์„ ๋ฌธ์ž์˜ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜(char)
            4. ๋ฐฐ์—ด์˜ ๊ฐ ๋ฌธ์ž๋ฅผ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜
        */

        String inputNum = Long.toString(n);
        String reversed = new StringBuilder(inputNum).reverse().toString();
        char arr [] = reversed.toCharArray();
        
        int[] answer = new int[arr.length];
        
        for (int i=0; i<arr.length; i++){
            answer[i] = arr[i] - '0';
            
        }
        
        return answer;
    }
}
๋ฐ˜์‘ํ˜•
Share Link
reply
๋ฐ˜์‘ํ˜•
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30