728x90
파이썬 내장 int, bin을 사용하면 10 to 2, 2 to 10이 가능
단 bin으로 했을 경우 0b100 이런식으로 str으로 return이 되기 때문에 slicing 필요
36ms
class Solution:
def addBinary(self, a: str, b: str) -> str:
ans = int(a,base=2)+int(b,base=2)
ans = bin(ans)[2:]
return ans
한줄로는 그냥 이렇게 32ms
4ms 단축
return bin(int(a,2)+int(b,2))[2:]
728x90
'Leetcode' 카테고리의 다른 글
[LeetCode] Remove Duplicates from Sorted List 파이썬 (Linked List, shallow copy, deepcopy) (0) | 2021.03.31 |
---|---|
[LeetCode] Climbing Stairs 파이썬 (DP) (0) | 2021.03.30 |
[LeetCode] Plus One 파이썬 (join, list comprehension) (0) | 2021.03.30 |
[LeetCode] Length of Last Word 파이썬 (rstrip, split) (0) | 2021.03.30 |
[LeetCode] Maximum Subarray 파이썬 (dp, local max, global max) (0) | 2021.03.30 |
댓글