본문 바로가기
IT/LeetCode

[LeetCode] 67. Add Binary

by 강천구 2022. 8. 19.

Given two binary strings a and b, return their sum as a binary string.

 

Example 1:

Input: a = "11", b = "1"
Output: "100"

Example 2:

Input: a = "1010", b = "1011"
Output: "10101"

 

Constraints:

  • 1 <= a.length, b.length <= 104
  • a and b consist only of '0' or '1' characters.
  • Each string does not contain leading zeros except for the zero itself.
반응형

주어진 2개의 2진수를 더하여 결과를 return하는 문제이다.

주어진 2개의 2진수 String 앞에 '0b'를 붙여 2진수 표기형식으로 바꾼 후 int()로 정수로 변경한다

그 후 주어진 정수 2개를 더한 후 format을 통해 'b'로 2진수로 변경하여 리턴한다.

 

class Solution:
   def addBinary(self, a: 'str', b: 'str') -> 'str':
       answer = int("0b" + a, 2) + int("0b" + b, 2)
       return format(answer, 'b')

 

문제 출처 : 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

Github : https://github.com/kkkkang1009/leetcode/blob/master/leetcode_67.py

 

GitHub - kkkkang1009/leetcode: leetcode

leetcode. Contribute to kkkkang1009/leetcode development by creating an account on GitHub.

github.com

 

반응형

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

[LeetCode] 70. Climbing Stairs  (0) 2022.08.19
[LeetCode] 69. Sqrt(x)  (0) 2022.08.19
[LeetCode] 66. Plus One  (0) 2022.08.19
[LeetCode] 64. Minimum Path Sum  (0) 2022.08.17
[LeetCode] 63. Unique Paths II  (0) 2022.08.17

댓글