๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

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

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํŠœํ”Œ (Python)

1. ๋ฌธ์ œ ์„ค๋ช…

๋ฌธ์ œ ๋งํฌ

  • ์ž…๋ ฅ
    • ๋ถ€๋ถ„ ์ง‘ํ•ฉ
  • ์ถœ๋ ฅ : ํŠœํ”Œ

2. ์ฝ”๋“œ

solution1.py

def solution(s):
    answer = []
    s = s[2:-2] #2},{2,1},{2,1,3},{2,1,3,4
    s = s.split("},{")#['2', '2,1', '2,1,3', '2,1,3,4']
    s.sort(key = len) #['2', '2,1', '2,1,3', '2,1,3,4']
    for ss in s:
        sss = ss.split(',')
        for j in sss:
            if int(j) not in answer:
                answer.append(int(j))
                print('answer',answer)

    return answer

3. ํšŒ๊ณ 

  • ๋ฌธ์ œ ์ดํ•ด๊ฐ€ ์–ด๋ ค์› ๋‹ค.
  • ์˜ˆ์‹œ๋ฅผ ๋ณด๊ณ  ๊ทœ์น™์„ฑ์„ ์ฐพ๊ณ  ํ’€์—ˆ๋‹ค.
  • ์˜ˆ์‹œ๋ฅผ ๋ณด๋ฉด s์—์„œ ์—ฌ๋Ÿฌ๋ฒˆ ๋‚˜์˜จ ์ˆซ์ž๊ฐ€ ์ถœ๋ ฅ์—์„œ ์•ž์ž๋ฆฌ๋ฅผ ์ฐจ์ง€ํ•œ๋‹ค.
  • ๊ฐ€์žฅ ๋งŽ์ด ๋“ฑ์žฅํ•˜๋ ค๋ฉด ๊ธธ์ด๊ฐ€ ์ž‘์€ ์ง‘ํ•ฉ์—๋„ ํฌํ•จ๋˜์–ด ์žˆ์–ด์•ผํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ๊ธธ์ด๊ฐ€ ์งง์€ ๊ฒƒ๋ถ€ํ„ฐ ๊ธด ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ํ›„, ๋จผ์ €๋‚˜์˜จ ์ˆœ์œผ๋กœ answer์— ๋‹ด๊ณ 
  • ์ด ํ›„์—๋Š” answer์— ์—†๋Š” ๊ฒƒ๋งŒ ๋‹ด์œผ๋ฉด ๋œ๋‹ค.
  • sss = ss.split(',') ํ–ˆ์„ ๋•Œ ๊ฐ ์š”์†Œ๋“ค์€ ๋ฐ˜๋ณต๋ฌธ(for j in sss:)์œผ๋กœ ํ•˜๋‚˜์”ฉ ์กฐํšŒ๊ฐ€๋Šฅ