-
[LeetCode] AddTwoNumbers알고리즘 2019. 8. 31. 02:15
문제
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807.
Approach
그냥 자리수 더해서 carry 생기면 넘겨주고 최종 답 내면 끝
마지막자리에서 carry 생길때 자리수 하나 더만들어주는거 test case 만들어서 찾아냄
Optimization
코드가 더러워서 디버깅하기 힘들고 구현을 좀 깔끔하게 할 필요가 있음구현을 손봐서 redundant했던 부분을 없애고 변수 선언등을 최소화 했더니 확 빨라짐
코드
https://github.com/chi3236/algorithm/blob/master/LeetCode_AddTwoNumbers.cpp
'알고리즘' 카테고리의 다른 글
[LeetCode] Reverse Integer (0) 2019.09.07 [LeetCode] Longest Substring Without Repeating Characters (0) 2019.09.06 [LeetCode] Next Permutation (0) 2019.09.04 [LeetCode] Median of Two Sorted Array (0) 2019.09.02 [LeetCode] TwoSum (0) 2019.08.30