From aa3217cbb5b8a19a9efcd00a7e60bfa7f5a529cb Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Tue, 7 May 2024 19:46:53 +0900 Subject: [PATCH 1/5] phase1 --- arai60/subsets/phase1.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 arai60/subsets/phase1.py diff --git a/arai60/subsets/phase1.py b/arai60/subsets/phase1.py new file mode 100644 index 0000000..d8887f5 --- /dev/null +++ b/arai60/subsets/phase1.py @@ -0,0 +1,13 @@ +class Solution: + def subsets(self, nums: List[int]) -> List[List[int]]: + all_subsets = [] + def recursive_find_all_subsets(subsets_so_far, index): + if subsets_so_far not in all_subsets: + all_subsets.append(subsets_so_far.copy()) + if nums[index] not in subsets_so_far: + recursive_find_all_subsets(subsets_so_far + [nums[index]], index) + index += 1 + if index < len(nums): + recursive_find_all_subsets(subsets_so_far, index) + recursive_find_all_subsets([], 0) + return all_subsets \ No newline at end of file From 9f08859cd37e1daa071fc2c4c4170034d9a1aee4 Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Thu, 9 May 2024 02:41:16 +0900 Subject: [PATCH 2/5] phase1 --- .../remove_duplicates_from_sorted_list_2/phase1.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 arai60/remove_duplicates_from_sorted_list_2/phase1.py diff --git a/arai60/remove_duplicates_from_sorted_list_2/phase1.py b/arai60/remove_duplicates_from_sorted_list_2/phase1.py new file mode 100644 index 0000000..ae13d99 --- /dev/null +++ b/arai60/remove_duplicates_from_sorted_list_2/phase1.py @@ -0,0 +1,14 @@ +class Solution: + def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: + dummy_head = ListNode(0) + current_head = dummy_head + skip_number = None + while head: + if head.next and head.val != head.next.val and head.val != skip_number: + current_head.next = ListNode(head.val) + current_head = current_head.next + elif head.next == None and head.val != skip_number: + current_head.next = ListNode(head.val) + skip_number = head.val + head = head.next + return dummy_head.next \ No newline at end of file From 2446b4002080a83609216f2e681037b9a6b4e86b Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Thu, 9 May 2024 02:41:25 +0900 Subject: [PATCH 3/5] phase2 --- .../phase2.py | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 arai60/remove_duplicates_from_sorted_list_2/phase2.py diff --git a/arai60/remove_duplicates_from_sorted_list_2/phase2.py b/arai60/remove_duplicates_from_sorted_list_2/phase2.py new file mode 100644 index 0000000..eee82b8 --- /dev/null +++ b/arai60/remove_duplicates_from_sorted_list_2/phase2.py @@ -0,0 +1,25 @@ +""" +Reference: +fhiyoさん: https://github.com/fhiyo/leetcode/pull/4/files +nittocoさん: https://github.com/nittoco/leetcode/pull/9/files +TORUさん: https://github.com/TORUS0818/leetcode/pull/6/files +phase1の回答は, skip_numberという状態変数を持って条件を制御していくやりかただったが, 状態変数の扱いが難しいものになる恐れがあると考えた。 +あまり状態変数は持ちたくないものではあるので, 重複が見つかったらスキップというやり方を採用する。 +""" + +class Solution: + def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: + def skip_duplicate_nodes(node): + while node.next and node.val == node.next.val: + node = node.next + return node + dummy_head = ListNode(-1) + current_node = dummy_head + while head: + if head.next and head.val == head.next.val: + head = skip_duplicate_nodes(head) + else: + current_node.next = ListNode(head.val) + current_node = current_node.next + head = head.next + return dummy_head.next \ No newline at end of file From 7b63b0b5e80a3be2b4b19df63432a06ca424e5fa Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Thu, 9 May 2024 02:41:32 +0900 Subject: [PATCH 4/5] phase3 --- .../phase3.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 arai60/remove_duplicates_from_sorted_list_2/phase3.py diff --git a/arai60/remove_duplicates_from_sorted_list_2/phase3.py b/arai60/remove_duplicates_from_sorted_list_2/phase3.py new file mode 100644 index 0000000..a561987 --- /dev/null +++ b/arai60/remove_duplicates_from_sorted_list_2/phase3.py @@ -0,0 +1,16 @@ +class Solution: + def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: + def skip_duplicated_nodes(head: Optional[ListNode]) -> Optional[ListNode]: + while head.next and head.val == head.next.val: + head = head.next + return head + dummy_head = ListNode(-1) + current_node = dummy_head + while head: + if head.next and head.val == head.next.val: + head = skip_duplicated_nodes(head) + else: + current_node.next = ListNode(head.val) + current_node = current_node.next + head = head.next + return dummy_head.next \ No newline at end of file From 193a8ddc87c4000a5099950f245dd090bba38a61 Mon Sep 17 00:00:00 2001 From: SuperHotDog <115141367+SuperHotDogCat@users.noreply.github.com> Date: Thu, 9 May 2024 02:43:31 +0900 Subject: [PATCH 5/5] Delete arai60/subsets/phase1.py --- arai60/subsets/phase1.py | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 arai60/subsets/phase1.py diff --git a/arai60/subsets/phase1.py b/arai60/subsets/phase1.py deleted file mode 100644 index d8887f5..0000000 --- a/arai60/subsets/phase1.py +++ /dev/null @@ -1,13 +0,0 @@ -class Solution: - def subsets(self, nums: List[int]) -> List[List[int]]: - all_subsets = [] - def recursive_find_all_subsets(subsets_so_far, index): - if subsets_so_far not in all_subsets: - all_subsets.append(subsets_so_far.copy()) - if nums[index] not in subsets_so_far: - recursive_find_all_subsets(subsets_so_far + [nums[index]], index) - index += 1 - if index < len(nums): - recursive_find_all_subsets(subsets_so_far, index) - recursive_find_all_subsets([], 0) - return all_subsets \ No newline at end of file