๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“– Problem Solution/Programmers

2019 ์นด์นด์˜ค ๊ฐœ๋ฐœ์ž ๊ฒจ์šธ ์ธํ„ด์‰ฝ ํŠœํ”Œ Swift

by Fomagran ๐Ÿ’ป 2020. 9. 7.
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์ฐธ๊ณ 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํŠœํ”Œ

"{{2},{2,1},{2,1,3},{2,1,3,4}}" [2, 1, 3, 4] "{{1,2,3},{2,1},{1,2,4,3},{2}}" [2, 1, 3, 4] "{{4,2,3},{3},{2,3,4,1},{2,3}}" [3, 2, 4, 1]

programmers.co.kr

Foma์˜ ํ’€์ด

์ฃผ์–ด์ง„ s๋Š” {{}} <- ๋‹ค์Œ ํ˜•์‹์œผ๋กœ ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.

 

์—ฌ๊ธฐ์„œ {...} ๊ฐ€์žฅ ๋ฐ”๊นฅ ๋Œ€๊ด„ํ˜ธ ์•ˆ {...} ์ˆซ์ž๋“ค์„ ๊ตฌ๋ถ„ํ•ด๋‚ด์–ด์•ผ ํ•˜๋Š”๋ฐ์š”.

 

๋จผ์ € ๋ฐ”๊นฅ{..}์•ˆ์˜ {...}์˜ ์ˆซ์ž๋Š” {...}์•ˆ "{"์˜ ๊ฐฏ์ˆ˜๋กœ count์ƒ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

=> let count = s.filter{$0 == "{"}.count

 

{...}๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ”๊พธ์–ด ๋‹ด์•„์ค„ ๋ณ€์ˆ˜๋„ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.(count-1๋งŒํผ ํฌ๊ธฐ๋ฅผ ์ •ํ•ด์ค๋‹ˆ๋‹ค.)

=> var numbersArray = Array(repeating: [Int](), count: count-1)

 

๋ฐฉ๋ฒ•์€ ๋จผ์ € s์—์„œ "{"๋ฅผ ์ œ๊ฑฐํ•ด์ค๋‹ˆ๋‹ค. ex) "{{2},{2,1},{2,1,3},{2,1,3,4}}" -> "2},2,1},2,1,3},2,1,3,4}}"

=> s.replacingOccurrences(of: "{"with"")

 

๊ทธ ๋‹ค์Œ์€ "}"๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ฐ๊ฐ ๋‚˜๋ˆ„์–ด์ค๋‹ˆ๋‹ค. ex) "2",2,1","2,1,3","2,1,3,4"

=> .components(separatedBy: "}")

 

๊ทธ ๋‹ค์Œ for๋ฌธ์„ ์ด์šฉํ•ด numbers์˜ ์ˆซ์ž๋งŒํผ ๋ฐ˜๋ณตํ•ด์ค๋‹ˆ๋‹ค.

=>  for i in 0..<numbers.count{...}

๊ทธ๋ฆฌ๊ณ  numbers์•ˆ์˜ ๋ฌธ์žฅ๋“ค์„ ","๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ„์–ด์ค๋‹ˆ๋‹ค.

=>  let number = numbers[i].components(separatedBy: ",")

 

๊ทธ๋ฆฌ๊ณ  numbers์•ˆ์˜ ๋ฐฐ์—ด๋“ค์˜ ๋ฌธ์žฅ์ค‘ ๊ณต๋ฐฑ์ด ์•„๋‹ˆ๋ผ๋ฉด numbersArray์˜ ํ•ด๋‹น i์ž๋ฆฌ์— ๋ฌธ์žฅ์„ Int๋กœ ํ˜•๋ณ€ํ™˜์‹œ์ผœ ์ฐจ๋ก€๋กœ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

=> for n in number {

            if n != "" {

                numbersArray[i].append(Int(n)!)

            }

        }

 

์—ฌ๊ธฐ๊นŒ์ง€ ํ•˜๊ฒŒ๋˜๋ฉด numbersArray์—” [[2], [2, 1], [2, 1, 3], [2, 1, 3, 4]] ์ด ๋‹ด๊ธฐ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ด์ œ ์ด ๊ตฌ๋ถ„ํ•ด๋‚ธ ๋ฐฐ์—ด๋“ค์„ ํŠœํ”Œ๋กœ ๋ฐ”๊ฟ”์ค˜์•ผํ•˜๋Š”๋ฐ์š”.

๊ทœ์น™์€ ๋ฐฐ์—ด์ด 1๊ฐœ๋ถ€ํ„ฐ numbersArray์— ๋‹ด๊ธด ์ˆซ์ž๊นŒ์ง€ ์ถ”๊ฐ€ํ•ด์ค€ ์ˆซ์ž๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ํ•œ๊ฐœ์˜ ์ˆซ์ž๋ฅผ answer์˜ ์ž๋ฆฌ์— ์ถ”๊ฐ€์‹œ์ผœ์ค๋‹ˆ๋‹ค.

ex) ๋ฐฐ์—ด์ด 1๊ฐœ์ธ๊ฒƒ -> [2] answer[0]์— 2์ถ”๊ฐ€

๋ฐฐ์—ด์ด 2๊ฐœ์ธ๊ฒƒ ->[2,1] answer[1]์— ์ถ”๊ฐ€๋œ 2๋ฅผ ์ œ์™ธํ•œ ์ˆซ์ž 1์„ ์ถ”๊ฐ€

...

ํ•˜๊ฒŒ๋˜๋ฉด answer => [2,1,3,4]๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

=> for i in 1...numbersArray.count {

        let a = numbersArray.filter{$0.count == i}.first!.filter{!answer.contains($0)}

        answer[i-1= a.first!

    }

 

 

๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ answer์„ ๋ฐ˜ํ™˜์‹œ์ผœ์ค๋‹ˆ๋‹ค.

=>  return answer

 

์ „์ฒด์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import Foundation
 
func solution(_ s:String-> [Int] {
    let count = s.filter{$0 == "{"}.count
    var numbersArray = Array(repeating: [Int](), count: count-1)
    let numbers = s.replacingOccurrences(of: "{"with"").components(separatedBy: "}")
    var answer = Array(repeating: 0, count: count-1)
    
    
    for i in 0..<numbers.count{
        let number = numbers[i].components(separatedBy: ",")
        for n in number {
            if n != "" {
                numbersArray[i].append(Int(n)!)
            }
        }
    }
    for i in 1...numbersArray.count {
        let a = numbersArray.filter{$0.count == i}.first!.filter{!answer.contains($0)}
        answer[i-1= a.first!
    }
    return answer
}
cs

 

 

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€