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 |
Tags
- Git
- 금융문자분석경진대회
- ChatGPT
- programmers
- 맥북
- dacon
- 코로나19
- 더현대서울 맛집
- 백준
- PYTHON
- 프로그래머스 파이썬
- 편스토랑
- 자연어처리
- 파이썬
- github
- SW Expert Academy
- hackerrank
- 데이콘
- 캐치카페
- ubuntu
- Kaggle
- gs25
- Baekjoon
- Real or Not? NLP with Disaster Tweets
- Docker
- leetcode
- 편스토랑 우승상품
- 우분투
- AI 경진대회
- 프로그래머스
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])



SOMJANG/CODINGTEST_PRACTICE
1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.
github.com
'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 |