ice rabbit programming

[LeetCode] Add Binary 본문

PS/LeetCode

[LeetCode] Add Binary

판교토끼 2020. 4. 23. 21:50

https://leetcode.com/problems/add-binary/

 

Add Binary - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

이 문제도 제목 그대로 이진수로 주어지는 string을 더해서 string으로 반환하는 문제이다.

논리회로 단계에서 Adder를 만들듯이 구현하였다.

public class Solution {
    public string AddBinary(string a, string b) {
        int shortLength = (a.Length < b.Length) ? a.Length : b.Length;
        int longLength = (a.Length > b.Length) ? a.Length : b.Length;
        string result = "";
        string carry = "0";
        for(int i=0;i<shortLength;i++) {
            if(a[a.Length-1-i]=='1' && b[b.Length-1-i]=='1') {
                if(carry=="1")
                    result = "1"+result;
                else
                    result = "0"+result;
                carry = "1";
            }
            else if((a[a.Length-1-i]=='1' && b[b.Length-1-i]=='0') 
                    || (a[a.Length-1-i]=='0' && b[b.Length-1-i]=='1')) {
                if(carry=="1") {
                    result = "0"+result;
                    carry = "1";
                }
                else {
                    result = "1"+result;
                    carry = "0";
                }
            }
            else {
                if(carry=="1")
                    result = "1"+result;
                else
                    result = "0"+result;
                carry="0";
            }
        }
        if(a.Length==b.Length) {
            if(carry=="1")
                result = "1"+result;
            return result;
        }
        for(int i=shortLength;i<longLength;i++) {
            if(a.Length>shortLength) {
                if(a[a.Length-1-i]=='1') {
                    if(carry=="1") {
                        result = "0"+result;
                        carry = "1";
                    }
                    else {
                        result = "1"+result;
                        carry = "0";
                    }
                }
                else {
                     if(carry=="1") {
                        result = "1"+result;
                        carry = "0";
                    }
                    else {
                        result = "0"+result;
                        carry = "0";
                    }
                }
            }
            else if(b.Length>shortLength) {
                if(b[b.Length-1-i]=='1') {
                    if(carry=="1") {
                        result = "0"+result;
                        carry = "1";
                    }
                    else {
                        result = "1"+result;
                        carry = "0";
                    }
                }
                else {
                     if(carry=="1") {
                        result = "1"+result;
                        carry = "0";
                    }
                    else {
                        result = "0"+result;
                        carry = "0";
                    }
                }
            }
        }
        if(result[0]=='0')
            result = "1"+result;
        return result;
    }
}

'PS > LeetCode' 카테고리의 다른 글

[LeetCode] Combine Two Tables  (0) 2020.04.27
[LeetCode] Customers Who Never Order  (0) 2020.04.26
[LeetCode] Same Tree  (0) 2020.04.22
[LeetCode] Majority Element  (0) 2020.04.21
[LeetCode] Pascal's Triangle  (0) 2020.04.20