diff --git a/Leetcode/00001_two_sum.py b/Leetcode/1-50/00001_two_sum.py similarity index 97% rename from Leetcode/00001_two_sum.py rename to Leetcode/1-50/00001_two_sum.py index 6339379..81d2ee7 100644 --- a/Leetcode/00001_two_sum.py +++ b/Leetcode/1-50/00001_two_sum.py @@ -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. diff --git a/Leetcode/1-50/00002_add_two_numbers.py b/Leetcode/1-50/00002_add_two_numbers.py new file mode 100644 index 0000000..d68e59a --- /dev/null +++ b/Leetcode/1-50/00002_add_two_numbers.py @@ -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) \ No newline at end of file diff --git a/Leetcode/00003.py b/Leetcode/1-50/00003_longest_substrings.py similarity index 98% rename from Leetcode/00003.py rename to Leetcode/1-50/00003_longest_substrings.py index 9362633..c11e4d1 100644 --- a/Leetcode/00003.py +++ b/Leetcode/1-50/00003_longest_substrings.py @@ -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 diff --git a/Leetcode/00004_median_of_two_sorted_arrays.py b/Leetcode/1-50/00004_median_of_two_sorted_arrays.py similarity index 100% rename from Leetcode/00004_median_of_two_sorted_arrays.py rename to Leetcode/1-50/00004_median_of_two_sorted_arrays.py diff --git a/Leetcode/00005_longestPalindrome.py b/Leetcode/1-50/00005_longestPalindrome.py similarity index 100% rename from Leetcode/00005_longestPalindrome.py rename to Leetcode/1-50/00005_longestPalindrome.py diff --git a/Leetcode/00007_reverse_integer.py b/Leetcode/1-50/00007_reverse_integer.py similarity index 100% rename from Leetcode/00007_reverse_integer.py rename to Leetcode/1-50/00007_reverse_integer.py diff --git a/Leetcode/00009_Palindrome.py b/Leetcode/1-50/00009_Palindrome.py similarity index 100% rename from Leetcode/00009_Palindrome.py rename to Leetcode/1-50/00009_Palindrome.py diff --git a/Leetcode/00013_romanToInt.py b/Leetcode/1-50/00013_romanToInt.py similarity index 100% rename from Leetcode/00013_romanToInt.py rename to Leetcode/1-50/00013_romanToInt.py diff --git a/Leetcode/00014_longestCommonPrefix.py b/Leetcode/1-50/00014_longestCommonPrefix.py similarity index 100% rename from Leetcode/00014_longestCommonPrefix.py rename to Leetcode/1-50/00014_longestCommonPrefix.py diff --git a/Leetcode/00020_isValid.py b/Leetcode/1-50/00020_isValid.py similarity index 100% rename from Leetcode/00020_isValid.py rename to Leetcode/1-50/00020_isValid.py diff --git a/Leetcode/00022_generateParenthesis.py b/Leetcode/1-50/00022_generateParenthesis.py similarity index 100% rename from Leetcode/00022_generateParenthesis.py rename to Leetcode/1-50/00022_generateParenthesis.py diff --git a/Leetcode/00023_removeElement.py b/Leetcode/1-50/00023_removeElement.py similarity index 100% rename from Leetcode/00023_removeElement.py rename to Leetcode/1-50/00023_removeElement.py diff --git a/Leetcode/00026_removeDuplicates.py b/Leetcode/1-50/00026_removeDuplicates.py similarity index 100% rename from Leetcode/00026_removeDuplicates.py rename to Leetcode/1-50/00026_removeDuplicates.py diff --git a/Leetcode/00028_strStr.py b/Leetcode/1-50/00028_strStr.py similarity index 100% rename from Leetcode/00028_strStr.py rename to Leetcode/1-50/00028_strStr.py diff --git a/Leetcode/00034_find_first_and_last_position_of_element_in_sorted_array.py b/Leetcode/1-50/00034_find_first_and_last_position_of_element_in_sorted_array.py similarity index 100% rename from Leetcode/00034_find_first_and_last_position_of_element_in_sorted_array.py rename to Leetcode/1-50/00034_find_first_and_last_position_of_element_in_sorted_array.py diff --git a/Leetcode/00035_search_insert_position.py b/Leetcode/1-50/00035_search_insert_position.py similarity index 100% rename from Leetcode/00035_search_insert_position.py rename to Leetcode/1-50/00035_search_insert_position.py diff --git a/Leetcode/_00038_count_and_say.py b/Leetcode/1-50/00038_count_and_say.py similarity index 100% rename from Leetcode/_00038_count_and_say.py rename to Leetcode/1-50/00038_count_and_say.py diff --git a/Leetcode/00050_powx_n.py b/Leetcode/1-50/00050_powx_n.py similarity index 100% rename from Leetcode/00050_powx_n.py rename to Leetcode/1-50/00050_powx_n.py diff --git a/Leetcode/003322_coin_change.py b/Leetcode/1001-10000/003322_coin_change.py similarity index 100% rename from Leetcode/003322_coin_change.py rename to Leetcode/1001-10000/003322_coin_change.py diff --git a/Leetcode/01029_two_city_scheduling.py b/Leetcode/1001-10000/01029_two_city_scheduling.py similarity index 100% rename from Leetcode/01029_two_city_scheduling.py rename to Leetcode/1001-10000/01029_two_city_scheduling.py diff --git a/Leetcode/000121_best_time_to_buy_and_sell_stock.py b/Leetcode/101-150/000121_best_time_to_buy_and_sell_stock.py similarity index 100% rename from Leetcode/000121_best_time_to_buy_and_sell_stock.py rename to Leetcode/101-150/000121_best_time_to_buy_and_sell_stock.py diff --git a/Leetcode/00118_pascals_triangle.py b/Leetcode/101-150/00118_pascals_triangle.py similarity index 100% rename from Leetcode/00118_pascals_triangle.py rename to Leetcode/101-150/00118_pascals_triangle.py diff --git a/Leetcode/00125_valid_palindrome.py b/Leetcode/101-150/00125_valid_palindrome.py similarity index 100% rename from Leetcode/00125_valid_palindrome.py rename to Leetcode/101-150/00125_valid_palindrome.py diff --git a/Leetcode/00131_palindrome_partitioning.py b/Leetcode/101-150/00131_palindrome_partitioning.py similarity index 100% rename from Leetcode/00131_palindrome_partitioning.py rename to Leetcode/101-150/00131_palindrome_partitioning.py diff --git a/Leetcode/00136_single_number.py b/Leetcode/101-150/00136_single_number.py similarity index 100% rename from Leetcode/00136_single_number.py rename to Leetcode/101-150/00136_single_number.py diff --git a/Leetcode/00137_single_number_ii.py b/Leetcode/101-150/00137_single_number_ii.py similarity index 100% rename from Leetcode/00137_single_number_ii.py rename to Leetcode/101-150/00137_single_number_ii.py diff --git a/Leetcode/00146_lru_cache.py b/Leetcode/101-150/00146_lru_cache.py similarity index 100% rename from Leetcode/00146_lru_cache.py rename to Leetcode/101-150/00146_lru_cache.py diff --git a/Leetcode/_00122_best_time_to_buy_and_sell_stock_ii.py b/Leetcode/101-150/_00122_best_time_to_buy_and_sell_stock_ii.py similarity index 100% rename from Leetcode/_00122_best_time_to_buy_and_sell_stock_ii.py rename to Leetcode/101-150/_00122_best_time_to_buy_and_sell_stock_ii.py diff --git a/Leetcode/_00139_word_break.py b/Leetcode/101-150/_00139_word_break.py similarity index 100% rename from Leetcode/_00139_word_break.py rename to Leetcode/101-150/_00139_word_break.py diff --git a/Leetcode/00155_min_stack.py b/Leetcode/151-200/00155_min_stack.py similarity index 100% rename from Leetcode/00155_min_stack.py rename to Leetcode/151-200/00155_min_stack.py diff --git a/Leetcode/00169_majority_element.py b/Leetcode/151-200/00169_majority_element.py similarity index 100% rename from Leetcode/00169_majority_element.py rename to Leetcode/151-200/00169_majority_element.py diff --git a/Leetcode/00191_number_of_1_bits.py b/Leetcode/151-200/00191_number_of_1_bits.py similarity index 100% rename from Leetcode/00191_number_of_1_bits.py rename to Leetcode/151-200/00191_number_of_1_bits.py diff --git a/Leetcode/00198_house_robber.py b/Leetcode/151-200/00198_house_robber.py similarity index 100% rename from Leetcode/00198_house_robber.py rename to Leetcode/151-200/00198_house_robber.py diff --git a/Leetcode/00202_happy_number.py b/Leetcode/201-250/00202_happy_number.py similarity index 100% rename from Leetcode/00202_happy_number.py rename to Leetcode/201-250/00202_happy_number.py diff --git a/Leetcode/00204_count-primes.py b/Leetcode/201-250/00204_count-primes.py similarity index 100% rename from Leetcode/00204_count-primes.py rename to Leetcode/201-250/00204_count-primes.py diff --git a/Leetcode/00217_contains_duplicate.py b/Leetcode/201-250/00217_contains_duplicate.py similarity index 100% rename from Leetcode/00217_contains_duplicate.py rename to Leetcode/201-250/00217_contains_duplicate.py diff --git a/Leetcode/00219_contains_duplicate_ii.py b/Leetcode/201-250/00219_contains_duplicate_ii.py similarity index 100% rename from Leetcode/00219_contains_duplicate_ii.py rename to Leetcode/201-250/00219_contains_duplicate_ii.py diff --git a/Leetcode/00225_implement_stack_using_queues.py b/Leetcode/201-250/00225_implement_stack_using_queues.py similarity index 100% rename from Leetcode/00225_implement_stack_using_queues.py rename to Leetcode/201-250/00225_implement_stack_using_queues.py diff --git a/Leetcode/00232_implement_queue_using_stacks.py b/Leetcode/201-250/00232_implement_queue_using_stacks.py similarity index 100% rename from Leetcode/00232_implement_queue_using_stacks.py rename to Leetcode/201-250/00232_implement_queue_using_stacks.py diff --git a/Leetcode/00240_search-a-2d-matrix-ii.py b/Leetcode/201-250/00240_search-a-2d-matrix-ii.py similarity index 100% rename from Leetcode/00240_search-a-2d-matrix-ii.py rename to Leetcode/201-250/00240_search-a-2d-matrix-ii.py diff --git a/Leetcode/00242_valid_anagram.py b/Leetcode/201-250/00242_valid_anagram.py similarity index 100% rename from Leetcode/00242_valid_anagram.py rename to Leetcode/201-250/00242_valid_anagram.py diff --git a/Leetcode/_00204_count_primes.py b/Leetcode/201-250/_00204_count_primes.py similarity index 100% rename from Leetcode/_00204_count_primes.py rename to Leetcode/201-250/_00204_count_primes.py diff --git a/Leetcode/_00205_isomorphic_strings.py b/Leetcode/201-250/_00205_isomorphic_strings.py similarity index 100% rename from Leetcode/_00205_isomorphic_strings.py rename to Leetcode/201-250/_00205_isomorphic_strings.py diff --git a/Leetcode/_00220_contains_duplicate_iii.py b/Leetcode/201-250/_00220_contains_duplicate_iii.py similarity index 100% rename from Leetcode/_00220_contains_duplicate_iii.py rename to Leetcode/201-250/_00220_contains_duplicate_iii.py diff --git a/Leetcode/00258_add_digits.py b/Leetcode/251-300/00258_add_digits.py similarity index 100% rename from Leetcode/00258_add_digits.py rename to Leetcode/251-300/00258_add_digits.py diff --git a/Leetcode/00260_single_number_iii.py b/Leetcode/251-300/00260_single_number_iii.py similarity index 100% rename from Leetcode/00260_single_number_iii.py rename to Leetcode/251-300/00260_single_number_iii.py diff --git a/Leetcode/00263_ugly_number.py b/Leetcode/251-300/00263_ugly_number.py similarity index 100% rename from Leetcode/00263_ugly_number.py rename to Leetcode/251-300/00263_ugly_number.py diff --git a/Leetcode/000726_number_of_atoms.py b/Leetcode/301-1000/000726_number_of_atoms.py similarity index 100% rename from Leetcode/000726_number_of_atoms.py rename to Leetcode/301-1000/000726_number_of_atoms.py diff --git a/Leetcode/00392_is_subsequence.py b/Leetcode/301-1000/00392_is_subsequence.py similarity index 100% rename from Leetcode/00392_is_subsequence.py rename to Leetcode/301-1000/00392_is_subsequence.py diff --git a/Leetcode/00455_assign_cookies.py b/Leetcode/301-1000/00455_assign_cookies.py similarity index 100% rename from Leetcode/00455_assign_cookies.py rename to Leetcode/301-1000/00455_assign_cookies.py diff --git a/Leetcode/00469_next_greater_element_i.py b/Leetcode/301-1000/00469_next_greater_element_i.py similarity index 100% rename from Leetcode/00469_next_greater_element_i.py rename to Leetcode/301-1000/00469_next_greater_element_i.py diff --git a/Leetcode/00509_fibonacci_number.py b/Leetcode/301-1000/00509_fibonacci_number.py similarity index 100% rename from Leetcode/00509_fibonacci_number.py rename to Leetcode/301-1000/00509_fibonacci_number.py diff --git a/Leetcode/00860_lemonade_change.py b/Leetcode/301-1000/00860_lemonade_change.py similarity index 100% rename from Leetcode/00860_lemonade_change.py rename to Leetcode/301-1000/00860_lemonade_change.py diff --git a/Leetcode/00976_largest_perimeter_triangle.py b/Leetcode/301-1000/00976_largest_perimeter_triangle.py similarity index 100% rename from Leetcode/00976_largest_perimeter_triangle.py rename to Leetcode/301-1000/00976_largest_perimeter_triangle.py diff --git a/Leetcode/_00746_min_cost_climbing_stairs.py b/Leetcode/301-1000/_00746_min_cost_climbing_stairs.py similarity index 100% rename from Leetcode/_00746_min_cost_climbing_stairs.py rename to Leetcode/301-1000/_00746_min_cost_climbing_stairs.py diff --git a/Leetcode/_00882_baseball_game.py b/Leetcode/301-1000/_00882_baseball_game.py similarity index 100% rename from Leetcode/_00882_baseball_game.py rename to Leetcode/301-1000/_00882_baseball_game.py diff --git a/Leetcode/_00887_super_egg_drop.py b/Leetcode/301-1000/_00887_super_egg_drop.py similarity index 100% rename from Leetcode/_00887_super_egg_drop.py rename to Leetcode/301-1000/_00887_super_egg_drop.py diff --git a/Leetcode/00058_length_of_last_word.py b/Leetcode/51-100/00058_length_of_last_word.py similarity index 100% rename from Leetcode/00058_length_of_last_word.py rename to Leetcode/51-100/00058_length_of_last_word.py diff --git a/Leetcode/00066_plus_one.py b/Leetcode/51-100/00066_plus_one.py similarity index 100% rename from Leetcode/00066_plus_one.py rename to Leetcode/51-100/00066_plus_one.py diff --git a/Leetcode/00069_sqrtx.py b/Leetcode/51-100/00069_sqrtx.py similarity index 100% rename from Leetcode/00069_sqrtx.py rename to Leetcode/51-100/00069_sqrtx.py diff --git a/Leetcode/00070_climbing_stairs.py b/Leetcode/51-100/00070_climbing_stairs.py similarity index 100% rename from Leetcode/00070_climbing_stairs.py rename to Leetcode/51-100/00070_climbing_stairs.py diff --git a/Leetcode/00088_merge_sorted_array.py b/Leetcode/51-100/00088_merge_sorted_array.py similarity index 100% rename from Leetcode/00088_merge_sorted_array.py rename to Leetcode/51-100/00088_merge_sorted_array.py diff --git a/Leetcode/_00053_maximum_subarray.py b/Leetcode/51-100/_00053_maximum_subarray.py similarity index 100% rename from Leetcode/_00053_maximum_subarray.py rename to Leetcode/51-100/_00053_maximum_subarray.py diff --git a/Leetcode/_00067_add_binary.py b/Leetcode/51-100/_00067_add_binary.py similarity index 100% rename from Leetcode/_00067_add_binary.py rename to Leetcode/51-100/_00067_add_binary.py diff --git a/Leetcode/_00070_climbing_stairs.py b/Leetcode/51-100/_00070_climbing_stairs.py similarity index 100% rename from Leetcode/_00070_climbing_stairs.py rename to Leetcode/51-100/_00070_climbing_stairs.py diff --git a/Leetcode/_00002.py b/Leetcode/_00002.py deleted file mode 100644 index 3e7d5b6..0000000 --- a/Leetcode/_00002.py +++ /dev/null @@ -1,51 +0,0 @@ - -"""这个题没做出来,需要补充链表知识""" - -""" -给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。 - -如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。 - -您可以假设除了数字 0 之外,这两个数都不会以 0 开头。 - -示例: - -输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) -输出:7 -> 0 -> 8 -原因:342 + 465 = 807 -""" - -class Solution: - def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: - - n = l1.val + l2.val - l3 = ListNode(n % 10) - l3.next = ListNode(n // 10) - p1 = l1.next - p2 = l2.next - p3 = l3 - while True: - if p1 and p2: - sum = p1.val + p2.val + p3.next.val - p3.next.val = sum % 10 - p3.next.next = ListNode(sum // 10) - p1 = p1.next - p2 = p2.next - p3 = p3.next - elif p1 and not p2: - sum = p1.val + p3.next.val - p3.next.val = sum % 10 - p3.next.next = ListNode(sum // 10) - p1 = p1.next - p3 = p3.next - elif not p1 and p2: - sum = p2.val + p3.next.val - p3.next.val = sum % 10 - p3.next.next = ListNode(sum // 10) - p2 = p2.next - p3 = p3.next - else: - if p3.next.val == 0: - p3.next = None - break - return l3 \ No newline at end of file diff --git a/Leetcode/tools.py b/Leetcode/tools.py new file mode 100644 index 0000000..e69de29