ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ–ฅ ๋ฌธ์ œ

์ง์‚ฌ๊ฐํ˜•์„ ๋งŒ๋“œ๋Š” ๋ฐ ํ•„์š”ํ•œ 4๊ฐœ์˜ ์  ์ค‘ 3๊ฐœ์˜ ์ขŒํ‘œ๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ๋‚˜๋จธ์ง€ ํ•œ ์ ์˜ ์ขŒํ‘œ๋ฅผ ๊ตฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์  3๊ฐœ์˜ ์ขŒํ‘œ๊ฐ€ ๋“ค์–ด์žˆ๋Š” ๋ฐฐ์—ด v๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์ง์‚ฌ๊ฐํ˜•์„ ๋งŒ๋“œ๋Š” ๋ฐ ํ•„์š”ํ•œ ๋‚˜๋จธ์ง€ ํ•œ ์ ์˜ ์ขŒํ‘œ๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”. ๋‹จ, ์ง์‚ฌ๊ฐํ˜•์˜ ๊ฐ ๋ณ€์€ x์ถ•, y์ถ•์— ํ‰ํ–‰ํ•˜๋ฉฐ, ๋ฐ˜๋“œ์‹œ ์ง์‚ฌ๊ฐํ˜•์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๋งŒ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

๐Ÿค” ํ’€์ด ๋ฐฉ๋ฒ•

[[1, 4], [3, 4], [3, 10], [1, 10]] -> 1, y : 2๊ฐœ, 3, y : 2๊ฐœ...
4๊ฐœ์˜ ์ขŒํ‘œ๋กœ ์ง์‚ฌ๊ฐํ˜•์„ ๋งŒ๋“ค๋ฉด ๊ฐ ์ ์„ ์ด๋ฃจ๋Š” ์ขŒํ‘œ๋Š” 2๊ฐœ์”ฉ ์ค‘๋ณต๋œ๋‹ค. ์ด๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ฌธ์ œ๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋‹ค.

if๋กœ ๊ตฌํ˜„

if v[0][0] == v[1][0] { ans[0] = v[2][0] }

  • ์ฒซ ๋ฒˆ์งธ ์ ๊ณผ ๋‘ ๋ฒˆ์งธ ์ ์˜ x์ขŒํ‘œ๊ฐ€ ๊ฐ™์œผ๋ฉด ์„ธ ๋ฒˆ์งธ ์ ์˜ x์ขŒํ‘œ๊ฐ€ ๋‹ต์ด ๋œ๋‹ค.

ternary๋กœ ๊ตฌํ˜„

if๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•œ ๊ฒƒ์„ ternary๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • a ? b : c : a๊ฐ€ ์ฐธ์ด๋ฉด b๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ๊ฑฐ์ง“์ด๋ฉด c๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

XOR ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„

์ถœ์ฒ˜ : https://swift.org

ans[0] = ans[0]^arrayV.first!

  • ans[0]์— v์˜ x์ขŒํ‘œ๋ฅผ ๋„ฃ๊ณ  ์ด๋ฅผ XOR์—ฐ์‚ฐ์„ ํ•ด์ค€๋‹ค.

map์œผ๋กœ ๊ตฌํ˜„

v.map{$0[0]}.reduce(0, ^)

  • x์ขŒํ‘œ์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์„ ๋ฝ‘์•„ ์ด๋ฅผ ^(XOR) ์—ฐ์‚ฐํ•ด ์ค€๋‹ค.

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป Swift Code

if๋กœ ๊ตฌํ˜„

import Foundation

func solution(_ v: [[Int]]) -> [Int]
{
    var ans = [Int](repeating: 0, count: 2)

    if v[0][0] == v[1][0] {
        ans[0] = v[2][0]
    } else if v[0][0] == v[2][0] {
        ans[0] = v[1][0]
    } else if v[1][0] == v[2][0] {
        ans[0] = v[0][0]
    }

    if v[0][1] == v[1][1] {
        ans[1] = v[2][1]
    } else if v[0][1] == v[2][1] {
        ans[1] = v[1][1]
    } else if v[1][1] == v[2][1] {
        ans[1] = v[0][1]
    }

    return ans
}

ternary๋กœ ๊ตฌํ˜„

import Foundation

func solution(_ v: [[Int]]) -> [Int]
{
    var ans = [Int](repeating: 0, count: 2)

    ans[0] = v[0][0] == v[1][0] ? v[2][0] : (v[0][0] == v[2][0] ? v[1][0] : v[0][0])
    ans[1] = v[0][1] == v[1][1] ? v[2][1] : (v[0][1] == v[2][1] ? v[1][1] : v[0][1])

    return ans
}

xor ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„

import Foundation

func solution(_ v: [[Int]]) -> [Int]
{
    var ans = [Int](repeating: 0, count: 2)

    for arrayV in v {
        ans[0] = ans[0]^arrayV.first!
        ans[1] = ans[1]^arrayV.last!
    }

    return ans
}

map์œผ๋กœ ๊ตฌํ˜„

import Foundation

func solution(_ v: [[Int]]) -> [Int]
{
    var ans = [Int](repeating: 0, count: 2)

    ans[0] = v.map{$0[0]}.reduce(0, ^)
    ans[1] = v.map{$0[1]}.reduce(0, ^)

    return ans
}

 

 

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ•ด์„ค - ๋‚˜๋จธ์ง€ ํ•œ ์ 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๋ชจ์˜ํ…Œ์ŠคํŠธ๋Š” ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ์‹œ์Šคํ…œ์— ์ต์ˆ™ํ•ด์ง€๊ธฐ ์œ„ํ•œ ํ…Œ์ŠคํŠธ์ด๋ฉฐ, ๋ฌธ์ œ ์ž์ฒด๋Š” 2018 1ST KAKAO BLIND RECRUITMENT์™€ ์ „ํ˜€ ๊ด€๊ณ„์—†์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ ๋ชจ์˜ํ…Œ์ŠคํŠธ์˜ ํ’€์ด์— ๋Œ€ํ•œ ์š”์ฒญ์ด ์žˆ์–ด

programmers.co.kr

 

๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
ยซ   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
๊ธ€ ๋ณด๊ด€ํ•จ