[LeetCode/Python] 67. Add Binary

2024. 9. 16. 22:22· Algorithm/LeetCode

문제 링크: https://leetcode.com/problems/add-binary/description/?envType=study-plan-v2&envId=top-interview-150

 

숫자로 쉽게 치환해서 패키지를 써서 이진법 덧셈을 할 생각이었는데, string 자체로 풀어보고 싶다고 생각했다.

 

 

[내 풀이]

class Solution:
    def addBinary(self, a: str, b: str) -> str:
        # without converting string to int
        carry = 0
        i, j = len(a) - 1, len(b) - 1

        result = []
        while i >= 0 or j >= 0 or carry:
            if i >= 0:
                carry += int(a[i])
                i -= 1

            if j >= 0:
                carry += int(b[j])
                j -= 1
            
            result.append(str(carry % 2))
            carry //= 2
        
        return ''.join(reversed(result))

 

처음에 length를 각각 둘 생각을 하지 못해서 좀 애를 먹었으나, 각각 두고 while문을 쓰면 될 것 같다는 생각을 하자마자 빠르게 풀렸다.

그리고 오랜만에 써보는 별거없지만 python 스러운 문법들!

 

 

[다른 풀이]

class Solution:
    def addBinary(self, a: str, b: str) -> str:
        x=int(a,2)
        y=int(b,2)
        return bin(x+y)[2:]

 

bin 메서드로 쉽게 풀어버리기~

 

 

시간 복잡도

각 문자열의 길이를 m, n이라고 했을 때

O(max(m, n))

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

[LeetCode/Python] 125. Valid Palindrome  (0) 2025.04.01
[LeetCode/Python] 58. Length of Last Word  (0) 2025.03.28
[LeetCode/Python] 9. Palindrome Number  (1) 2024.09.17
[LeetCode/Python] 13. Roman to Integer  (0) 2024.08.12
[LeetCode/Python] 1492. The kth Factor of n  (1) 2024.07.23
'Algorithm/LeetCode' 카테고리의 다른 글
  • [LeetCode/Python] 58. Length of Last Word
  • [LeetCode/Python] 9. Palindrome Number
  • [LeetCode/Python] 13. Roman to Integer
  • [LeetCode/Python] 1492. The kth Factor of n
빵빵0
빵빵0
(아직은) 공부하고 정리하는 블로그입니다.
빵빵0
Hack Your World
빵빵0
전체
오늘
어제
  • 분류 전체보기 (69)
    • Error Handling (3)
    • Project (1)
    • Computer Science (4)
      • Data Structure (2)
      • Database (1)
      • Cloud (0)
      • OS (0)
      • Infra, Network (1)
      • AI (0)
    • Language (3)
      • Go (3)
      • Rust (0)
      • Python (0)
      • Java (0)
    • Algorithm (39)
      • BaekJoon (18)
      • Programmers (7)
      • LeetCode (6)
      • NeetCode (8)
    • SW Books (9)
      • gRPC Up & Running (1)
      • System Design Interview (2)
      • 스프링 입문을 위한 자바 객체지향의 원리와 이해 (6)
      • 블록체인 해설서 (0)
      • 후니의 쉽게 쓴 CISCO 네트워킹 (0)
    • Own (2)
      • Personal (0)
      • Novel (0)
      • Comics & Animation (0)
      • Memo (2)
    • BlockChain (4)
      • Issues (0)
      • Research (4)
      • Tech (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • chart
  • 큐
  • Python
  • decimal
  • 해시
  • Event
  • BaekJoon
  • 프로그래머스
  • Palindrome
  • 2024
  • DP
  • ohlcv
  • BEAKJOON
  • Hash Table
  • Greedy
  • KBW
  • go
  • NeetCode
  • blockchain
  • LeetCode
  • 백준
  • two pointer
  • 블록체인
  • golang
  • Programmers
  • candlechart
  • 스택
  • 그리디
  • BFS
  • MongoDB

최근 댓글

최근 글

hELLO · Designed By 정상우.v4.2.0
빵빵0
[LeetCode/Python] 67. Add Binary
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.