From aa3217cbb5b8a19a9efcd00a7e60bfa7f5a529cb Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Tue, 7 May 2024 19:46:53 +0900 Subject: [PATCH 1/6] 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 e9a963c39f5fd3fb49e64586d3a5b28241ee8492 Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Tue, 7 May 2024 22:44:03 +0900 Subject: [PATCH 2/6] 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 29b6ef15a4d840d2e173feed709b93e06858dde0 Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Fri, 24 May 2024 01:02:44 +0900 Subject: [PATCH 3/6] phase1 --- arai60/first_unique_character_in_a_string/phase1.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 arai60/first_unique_character_in_a_string/phase1.py diff --git a/arai60/first_unique_character_in_a_string/phase1.py b/arai60/first_unique_character_in_a_string/phase1.py new file mode 100644 index 0000000..9d95090 --- /dev/null +++ b/arai60/first_unique_character_in_a_string/phase1.py @@ -0,0 +1,11 @@ +class Solution: + def firstUniqChar(self, s: str) -> int: + # 登場回数を数える + count_appearance = [0 for _ in range(128)] + for char in s: + count_appearance[ord(char)] += 1 + + for index, char in enumerate(s): + if count_appearance[ord(char)] == 1: + return index + return -1 From ce57f7b0fb4fdec8eccce808b72358289c109c6c Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Fri, 24 May 2024 01:02:52 +0900 Subject: [PATCH 4/6] phase2 --- .../phase2.py | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 arai60/first_unique_character_in_a_string/phase2.py diff --git a/arai60/first_unique_character_in_a_string/phase2.py b/arai60/first_unique_character_in_a_string/phase2.py new file mode 100644 index 0000000..c0f8759 --- /dev/null +++ b/arai60/first_unique_character_in_a_string/phase2.py @@ -0,0 +1,35 @@ +""" +reference +Exzrgsさん: https://github.com/Exzrgs/LeetCode/pull/9/files 命名を参考にした +hayashi-ayさん: https://github.com/hayashi-ay/leetcode/pull/28/files +2回ループをどうにか1回ループで回せないか考えてみたが, 1回ループでまわしたあと, 辞書が順序を保存することを使わないとうまく書けないことと, 少し複雑な条件分岐が入ることを学んだ + +一応, 辞書を使っている人が多いのでdictを使うようにした。入力の文字列の名前がsなのでcharもcにした +""" +class Solution: + def firstUniqChar(self, s: str) -> int: + char_to_appear_count = defaultdict(int) + for c in s: + char_to_appear_count[c] += 1 + + for index, c in enumerate(s): + if char_to_appear_count[c] == 1: + return index + return -1 + +# ループを一回に済ませる +class Solution: + def firstUniqChar(self, s: str) -> int: + char_to_first_appear_index = defaultdict(int) + duplicated = set() + for index, c in enumerate(s): + if c in duplicated: + continue + if c in char_to_first_appear_index: + del char_to_first_appear_index[c] + duplicated.add(c) + continue + char_to_first_appear_index[c] = index + if not char_to_first_appear_index: + return -1 + return next(iter(char_to_first_appear_index.values())) \ No newline at end of file From 8400ff3d3db8e2c7e289dc7562b89bfc4dc6cea6 Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Fri, 24 May 2024 01:03:01 +0900 Subject: [PATCH 5/6] phase3 --- arai60/first_unique_character_in_a_string/phase3.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 arai60/first_unique_character_in_a_string/phase3.py diff --git a/arai60/first_unique_character_in_a_string/phase3.py b/arai60/first_unique_character_in_a_string/phase3.py new file mode 100644 index 0000000..0c7c3a6 --- /dev/null +++ b/arai60/first_unique_character_in_a_string/phase3.py @@ -0,0 +1,10 @@ +class Solution: + def firstUniqChar(self, s: str) -> int: + char_to_appear_count = defaultdict(int) + for c in s: + char_to_appear_count[c] += 1 + + for index, c in enumerate(s): + if char_to_appear_count[c] == 1: + return index + return -1 \ No newline at end of file From 43eab2dce34383707cf62861e5a3dd4d4b65f68f Mon Sep 17 00:00:00 2001 From: SuperHotDogCat Date: Fri, 24 May 2024 01:25:34 +0900 Subject: [PATCH 6/6] Delete unnecessary files --- .../remove_duplicates_from_sorted_list_2/phase1.py | 14 -------------- arai60/subsets/phase1.py | 13 ------------- 2 files changed, 27 deletions(-) delete mode 100644 arai60/remove_duplicates_from_sorted_list_2/phase1.py delete mode 100644 arai60/subsets/phase1.py diff --git a/arai60/remove_duplicates_from_sorted_list_2/phase1.py b/arai60/remove_duplicates_from_sorted_list_2/phase1.py deleted file mode 100644 index ae13d99..0000000 --- a/arai60/remove_duplicates_from_sorted_list_2/phase1.py +++ /dev/null @@ -1,14 +0,0 @@ -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 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