Skip to content

A pythonic solution for 0002 #201

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions solution/0002.Add Two Numbers/Solution2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
"""
This solution is from: https://leetcode.com/problems/add-two-numbers/discuss/1102/Python-for-the-win
A very Pythonic solution, coooooool!
Runtime: 76 ms, faster than 84.66% of Python3 online submissions for Add Two Numbers.
Memory Usage: 13.3 MB, less than 38.91% of Python3 online submissions for Add Two Numbers.
"""
def addTwoNumbers(self, l1, l2):
def toint(node):
return node.val + 10 * toint(node.next) if node else 0
def tolist(n):
node = ListNode(n % 10)
if n > 9:
node.next = tolist(n // 10)
return node
return tolist(toint(l1) + toint(l2))
3 changes: 2 additions & 1 deletion solution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
│   ├── Solution.js
│   ├── Solution.py
│   ├── Solution.rb
│   ├── Solution2.py
│   └── Solution2.js
├── 0003.Longest Substring Without Repeating Characters
│   ├── README.md
Expand Down Expand Up @@ -1274,4 +1275,4 @@
├── 5087.Letter Tile Possibilities
│   └── Solution.py
└── README.md
```
```