diff --git a/2-python/ch06/1-1.py b/2-python/ch06/1-1.py index 257d2d6..e3b25b3 100644 --- a/2-python/ch06/1-1.py +++ b/2-python/ch06/1-1.py @@ -1,13 +1,10 @@ class Solution: - def isPalindrome(self, s: str) -> bool: - strs = [] - for char in s: - if char.isalnum(): - strs.append(char.lower()) + def isPalindrome(self, string: str) -> bool: + chars = [char.lower() for char in string if char.isalnum()] # 팰린드롬 여부 판별 - while len(strs) > 1: - if strs.pop(0) != strs.pop(): + while len(chars) > 1: + if chars.pop(0) != chars.pop(): return False return True diff --git a/2-python/ch06/1-2.py b/2-python/ch06/1-2.py index 1d9caa7..3f7d032 100644 --- a/2-python/ch06/1-2.py +++ b/2-python/ch06/1-2.py @@ -1,18 +1,12 @@ -import collections -from typing import Deque +from collections import deque class Solution: - def isPalindrome(self, s: str) -> bool: - # 자료형 데크로 선언 - strs: Deque = collections.deque() + def isPalindrome(self, string: str) -> bool: + chars = deque(char.lower() for char in string if char.isalnum()) - for char in s: - if char.isalnum(): - strs.append(char.lower()) - - while len(strs) > 1: - if strs.popleft() != strs.pop(): + while len(chars) > 1: + if chars.popleft() != chars.pop(): return False return True diff --git a/3-linear-data-structures/ch07/10-2.py b/3-linear-data-structures/ch07/10-2.py index 7e8492f..15d9cd7 100644 --- a/3-linear-data-structures/ch07/10-2.py +++ b/3-linear-data-structures/ch07/10-2.py @@ -3,12 +3,10 @@ class Solution: def arrayPairSum(self, nums: List[int]) -> int: - sum = 0 nums.sort() - for i, n in enumerate(nums): - # 짝수 번째 값의 합 계산 - if i % 2 == 0: - sum += n - - return sum + return sum( + number + for i, number in enumerate(nums) + if i % 2 == 0 + ) diff --git a/3-linear-data-structures/ch07/11-1.py b/3-linear-data-structures/ch07/11-1.py index 7a756ce..973e35b 100644 --- a/3-linear-data-structures/ch07/11-1.py +++ b/3-linear-data-structures/ch07/11-1.py @@ -3,15 +3,16 @@ class Solution: def productExceptSelf(self, nums: List[int]) -> List[int]: - out = [] - p = 1 - # 왼쪽 곱셈 - for i in range(0, len(nums)): - out.append(p) - p = p * nums[i] - p = 1 - # 왼쪽 곱셈 결과에 오른쪽 값을 차례대로 곱셈 - for i in range(len(nums) - 1, 0 - 1, -1): - out[i] = out[i] * p - p = p * nums[i] - return out + result = [] + + left_accumulated = 1 + for number in nums: + result.append(left_accumulated) + left_accumulated *= number + + right_accumulated = 1 + for i in range(0, len(nums))[::-1]: + result[i] *= right_accumulated + right_accumulated *= nums[i] + + return result diff --git a/3-linear-data-structures/ch07/12-2.py b/3-linear-data-structures/ch07/12-2.py index ebd2109..355f01b 100644 --- a/3-linear-data-structures/ch07/12-2.py +++ b/3-linear-data-structures/ch07/12-2.py @@ -5,7 +5,7 @@ class Solution: def maxProfit(self, prices: List[int]) -> int: profit = 0 - min_price = sys.maxsize + min_price = sys.maxsize # or prices[0] # 최소값과 최대값 계속 갱신 for price in prices: diff --git a/3-linear-data-structures/ch08/13-4.py b/3-linear-data-structures/ch08/13-4.py index 18f9617..7173926 100644 --- a/3-linear-data-structures/ch08/13-4.py +++ b/3-linear-data-structures/ch08/13-4.py @@ -12,7 +12,13 @@ def isPalindrome(self, head: ListNode) -> bool: # 런너를 이용해 역순 연결 리스트 구성 while fast and fast.next: fast = fast.next.next - rev, rev.next, slow = slow, rev, slow.next + + next_node = slow.next + previous_node = rev + + rev = slow + rev.next = previous_node + slow = next_node if fast: slow = slow.next