[Algorithm] Add Binary

๋ฌธ์ œ ์„ค๋ช…

String์œผ๋กœ ๋“ค์–ด์˜ค๋Š” ์ด์ง„์ˆ˜ a์™€ b๋ฅผ ๋”ํ•œ ๊ฒฐ๊ณผ๋ฅผ String์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ.

image

๋ฌธ์ œ์˜ ์„ค๋ช…๋„, ์˜ˆ์‹œ๋„ ์•„์ฃผ ์‹ฌํ”Œํ•˜๋‹ค.

ํ‘ผ ๋ฐฉ๋ฒ•

์„ค๋ช…๊ณผ ์˜ˆ์‹œ๋Š” ์‹ฌํ”Œํ•˜์ง€๋งŒ ๋‚ด ์ฝ”๋“œ๋Š” ์‹ฌํ”Œํ•˜์ง€๊ฐ€ ์•Š์•˜๋‹คโ€ฆ

func addBinary(_ a: String, _ b: String) -> String {
        let longerBinaryArr: [String.Element]
        let shortBinaryArr: [String.Element]
        
        if a.count <= b.count {
            longerBinaryArr = Array(b)
            shortBinaryArr = Array(a)
        } else {
            longerBinaryArr = Array(a)
            shortBinaryArr = Array(b)
        }
        
        var result: [Character] = []
        var add = 0
        
        for (idx, longerBinaryChar) in longerBinaryArr.reversed().enumerated() {
            if shortBinaryArr.count - idx - 1 < 0 {
                let longerBinaryInt = Int(longerBinaryChar.description) ?? 0
                let addResult = addResult(longerBinaryInt + add)
                
                result.insert(contentsOf: addResult.0, at: 0)
                add = addResult.1
                
            } else {
                let shortBinaryChar = shortBinaryArr[shortBinaryArr.count - idx - 1]
                let addResult = addResult((Int(longerBinaryChar.description) ?? 0) + (Int(shortBinaryChar.description) ?? 0) + add)
                
                result.insert(contentsOf: addResult.0, at: 0)
                add = addResult.1
            }
        }
        
        if add == 1 {
            result.insert("1", at: 0)
        }
        
        return String(result)
    }
    
    func addResult(_ result: Int) -> (String, Int) {
        let string: String
        let int: Int
        switch result {
        case 2:
            string = "0"
            int = 1
        case 3:
            string = "1"
            int = 1
        default:
            string = String(result)
            int = 0
        }
        
        return (string, int)
    }
  • ์šฐ์„  Swift์—์„œ๋Š” subscript๋กœ String์˜ ๊ธ€์ž์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†์–ด์„œ ๋จผ์ € Array๋กœ String์„ ๋ณ€ํ™˜ํ•ด์ฃผ์—ˆ๋‹ค.
  • ๋” ๊ธธ๊ฒŒ ๋“ค์–ด์˜ค๋Š” string์„ ๊ธฐ์ค€์œผ๋กœ for๋ฌธ์„ ๋Œ๊ธฐ ์œ„ํ•ด a์™€ b์˜ ๊ธธ์ด๋ฅผ ๋น„๊ตํ–ˆ๋‹ค.
  • ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉด์„œ ๋‘ ์ด์ง„์ˆ˜๋ฅผ ๋”ํ•ด์ฃผ์—ˆ๋‹ค.
    • addResult๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๋”ฐ๋กœ ๋นผ์„œ 2์ธ๊ฒฝ์šฐ, 3์ธ ๊ฒฝ์šฐ, ๋‘˜ ๋‹ค ์•„๋‹Œ ๊ฒฝ์šฐ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์„œ ๋ฐ›์•„์˜ฌ๋ฆผ์ด ์žˆ๋Š”์ง€์™€ ํ•ด๋‹น ์ž๋ฆฌ์— ์–ด๋–ค ์ˆซ์ž๊ฐ€ ๋“ค์–ด๊ฐ€์•ผ ํ•˜๋Š”์ง€ ํŒ๋‹จํ•˜๋„๋ก ํ–ˆ๋‹ค.
  • ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๊นŒ์ง€ ์ฒดํฌํ•œ ๋’ค for๋ฌธ์„ ๋น ์ ธ๋‚˜์™”์„ ๋•Œ add๊ฐ€ 1๋กœ ์„ค์ •๋˜์–ด ์žˆ๋‹ค๋ฉด ๋ฐ›์•„ ์˜ฌ๋ฆผ์ด ๋˜์–ด ์ƒˆ๋กœ์šด ์ž๋ฆฌ์ˆ˜๊ฐ€ ์ƒ๊ฒจ์•ผ ํ•˜๋Š” ๊ฒƒ์„ ์ฒดํฌํ•˜๋„๋ก ํ–ˆ๋‹ค.
  • ์ตœํ›„์— Character ๋ฐฐ์—ด์„ String์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฆฌํ„ดํ–ˆ๋‹ค.

์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ’€๊ณ  ์‹ถ์ง€ ์•Š์„ ๋•Œ๋Š” ์ด์ƒํ•˜๊ฒŒ ์ฝ”๋“œ๊ฐ€ ๋”๋Ÿฌ์›Œ์ง€๋Š” ๊ฒƒ ๊ฐ™๋‹ค. (ํ•ญ์ƒ ํ’€๊ธฐ ๊ท€์ฐฎ์€ ๊ฒƒ ๊ฐ™๊ธฐ๋„ ํ•จ..^^)

๊ฒฐ๊ณผ

image

๋ ˆํฌ ์ปค๋ฐ‹

Categories:

Updated:

Leave a comment