๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด(Java)

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค_๋‘ ๊ฐœ ๋ฝ‘์•„์„œ ๋”ํ•˜๊ธฐ

by carrot0911 2024. 12. 5.

๋ฌธ์ œ ์„ค๋ช…

https://school.programmers.co.kr/learn/courses/30/lessons/68644

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr

์ •์ˆ˜ ๋ฐฐ์—ด numbers๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. numbers์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ์ธ๋ฑ์Šค์— ์žˆ๋Š” ๋‘ ๊ฐœ์˜ ์ˆ˜๋ฅผ ๋ฝ‘์•„ ๋”ํ•ด์„œ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์ˆ˜๋ฅผ ๋ฐฐ์—ด์— ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • numbers์˜ ๊ธธ์ด๋Š” 2 ์ด์ƒ 100 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
    • numbers์˜ ๋ชจ๋“  ์ˆ˜๋Š” 0 ์ด์ƒ 100 ์ดํ•˜์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

numbers result
[2, 1, 3, 4, 1] [2, 3, 4, 5, 6, 7]
[5, 0, 2, 7] [2, 5, 7, 9 12]

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • 2 = 1 + 1 ์ž…๋‹ˆ๋‹ค. (1์ด numbers์— ๋‘ ๊ฐœ ์žˆ์Šต๋‹ˆ๋‹ค.)
  • 3 = 2 + 1 ์ž…๋‹ˆ๋‹ค.
  • 4 = 1 + 3 ์ž…๋‹ˆ๋‹ค.
  • 5 = 1 + 4 = 2 + 3 ์ž…๋‹ˆ๋‹ค.
  • 6 = 2 + 4 ์ž…๋‹ˆ๋‹ค.
  • 7 = 3 + 4 ์ž…๋‹ˆ๋‹ค.
  • ๋”ฐ๋ผ์„œ [2, 3, 4, 5, 6, 7]์„ returnํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • 2 = 0 + 2 ์ž…๋‹ˆ๋‹ค.
  • 5 = 5 + 0 ์ž…๋‹ˆ๋‹ค.
  • 7 = 7 + 0 =5 = 2 ์ž…๋‹ˆ๋‹ค.
  • 9 = 2 + 7 ์ž…๋‹ˆ๋‹ค.
  • 12 = 5 + 7 ์ž…๋‹ˆ๋‹ค.
  • ๋”ฐ๋ผ์„œ [2, 5, 7, 9, 12]๋ฅผ returnํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

import java.util.*;

class Solution {
    public ArrayList<Integer> solution(int[] numbers) {
        ArrayList<Integer> answer = new ArrayList<>();
        
        for (int i = 0; i < numbers.length-1; i++) {
            for (int j = i+1; j < numbers.length; j++) {
                int sum = numbers[i] + numbers[j];
                if (!answer.contains(sum)){
                    answer.add(sum);
                }
            }
        }
        
        Collections.sort(answer);
        
        return answer;
    }
}

์ฝ”๋“œ ์„ค๋ช…

  • ArrayList<Integer> answer = new ArrayList<>( ) : ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ answer๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ArrayList๋Š” ๋™์ ์œผ๋กœ ํฌ๊ธฐ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š” ๋ฆฌ์ŠคํŠธ๋กœ, ์ค‘๋ณต๋˜์ง€ ์•Š์€ ๋‘ ์ˆ˜์˜ ํ•ฉ์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค.
  • for (int i = 0; i < numbers.length - 1; i++) { } : ์ฒซ ๋ฒˆ์งธ for๋ฌธ์œผ๋กœ numbers ๋ฐฐ์—ด์˜ ์š”์†Œ๋ฅผ ์ˆœํšŒํ•œ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด numbers.length - 1๊นŒ์ง€๋งŒ ๋ฐ˜๋ณตํ•œ๋‹ค.
    • for (int j = i+1; j < numbers.length; j++) { } : ๋‘ ๋ฒˆ์งธ for๋ฌธ์œผ๋กœ i ์ดํ›„์˜ ์ˆซ์ž๋“ค์„ ์ˆœํšŒํ•œ๋‹ค. j๋Š” ํ•ญ์ƒ i+1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ฐ ๋ฐ˜๋ณต์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ์ˆซ์ž์˜ ์กฐํ•ฉ์„ ๋งŒ๋“ ๋‹ค.
      • int sum = numbers[i] + numbers[j] : numbers ๋ฐฐ์—ด์˜ i๋ฒˆ์งธ ์š”์†Œ์™€ j๋ฒˆ์งธ ์š”์†Œ์˜ ํ•ฉ์„ ๊ตฌํ•˜์—ฌ sum ๋ณ€์ˆ˜์— ์ €์žฅํ•œ๋‹ค. ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๋‘ ์ˆซ์ž์˜ ํ•ฉ์„ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค.
      • if (!answer.contains(sum)) { answer.add(sum) } : answer ๋ฆฌ์ŠคํŠธ์— ์ด๋ฏธ sum ๊ฐ’์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. ๋งŒ์•ฝ answer ๋ฆฌ์ŠคํŠธ์— sum ๊ฐ’์ด ์—†๋‹ค๋ฉด ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•œ๋‹ค.
  • Collenctions.sort(answer) : ๋ชจ๋“  ๋‘ ์ˆ˜์˜ ํ•ฉ์ด answer ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ์ด ๋œ ํ›„, ๋ฆฌ์ŠคํŠธ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.