Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- programmers
- 파이썬
- PYTHON
- Baekjoon
- 금융문자분석경진대회
- 데이콘
- 프로그래머스 파이썬
- SW Expert Academy
- Docker
- AI 경진대회
- Real or Not? NLP with Disaster Tweets
- github
- ChatGPT
- 코로나19
- 더현대서울 맛집
- 우분투
- 자연어처리
- leetcode
- dacon
- 백준
- Kaggle
- 맥북
- gs25
- Git
- 프로그래머스
- 편스토랑
- 편스토랑 우승상품
- ubuntu
- 캐치카페
- hackerrank
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 415. Add Strings (Python) 본문
728x90
반응형
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.
Note:
- The length of both num1 and num2 is < 5100.
- Both num1 and num2 contains only digits 0-9.
- Both num1 and num2 does not contain any leading zero.
- You must not use any built-in BigInteger library or convert the inputs to integer directly.
Solution
class Solution(object):
def addStrings(self, num1, num2):
answer_list = []
carry = 0
index1, index2 = len(num1), len(num2)
while index1 or index2 or carry:
digit = carry
if index1:
index1 -= 1
digit += int(num1[index1])
if index2:
index2 -= 1
digit += int(num2[index2])
carry = digit > 9
answer_list.append(str(digit % 10))
return ''.join(answer_list[::-1])
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 506. Relative Ranks (Python) (0) | 2020.09.07 |
---|---|
[leetCode] 1507. Reformat Date (Python) (0) | 2020.09.06 |
[leetCode] 917. Reverse Only Letters (Python) (0) | 2020.09.04 |
[leetCode] 443. String Compression (Python) (0) | 2020.09.03 |
[leetCode] 1480. Running Sum of 1d Array (Python) (0) | 2020.09.02 |
Comments