Skip to content

Commit

Permalink
optimize code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangyang.qi committed Nov 8, 2022
1 parent 380baa1 commit 59cc179
Show file tree
Hide file tree
Showing 67 changed files with 120 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""
https://leetcode.com/problems/two-sum/
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Expand Down
92 changes: 92 additions & 0 deletions Leetcode/1-50/00002_add_two_numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""
https://leetcode.com/problems/add-two-numbers/
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 contains a single digit. Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example 1:
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.
Example 2:
Input: l1 = [0], l2 = [0]
Output: [0]
Example 3:
Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]
Constraints:
The number of nodes in each linked list is in the range [1, 100].
0 <= Node.val <= 9
It is guaranteed that the list represents a number that does not have leading zeros.
"""
from typing import Optional

class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next


def addTwoNumbers(l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
l1_str = ""
while True:
l1_str += str(l1.val)
if l1.next is None:
break
l1 = l1.next
print(f"l1_str: {l1_str}")

l2_str = ""
while True:
l2_str += str(l2.val)
if l2.next is None:
break
l2 = l2.next

print(f"l2_str: {l2_str}")

big, small = l1_str, l2_str
if len(l1_str) < len(l2_str):
big, small = l2_str, l1_str

total = int(big) + int(small)
print(total)

node_list = list()
for s in str(total):
node_list.append(ListNode(int(s)))

for i, n in enumerate(node_list):
if i + 1 <= len(node_list) - 1:
n.next = node_list[i+1]

return node_list[0]


if __name__ == '__main__':

# test link table
a1 = ListNode(2)
b1 = ListNode(4)
c1 = ListNode(3)
a1.next = b1
b1.next = c1

a2 = ListNode(5)
b2 = ListNode(6)
c2 = ListNode(4)
d2 = ListNode(6)
a2.next = b2
b2.next = c2
c2.next = d2

addTwoNumbers(a1, a2)

# print(a.next.next.val)
27 changes: 27 additions & 0 deletions Leetcode/00003.py → Leetcode/1-50/00003_longest_substrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,33 @@
输出: 3
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
Given a string s, find the length of the longest
substring without repeating characters.
Example 1:
Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
Constraints:
0 <= s.length <= 5 * 104
s consists of English letters, digits, symbols and spaces.
"""

import time
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
51 changes: 0 additions & 51 deletions Leetcode/_00002.py

This file was deleted.

Empty file added Leetcode/tools.py
Empty file.

0 comments on commit 59cc179

Please sign in to comment.