파이썬알고리즘인터뷰(6)
-
[LeetCode] 20. Valid Parentheses
LeetCode 20. Valid Parentheses Solution class Solution: def isValid(self, s: str) -> bool: stack = [] bracket = { ')': '(', ']': '[', '}': '{' } for char in s: if char not in bracket: stack.append(char) # open bracket elif not stack or bracket[char] != stack.pop(): return False return len(stack) == 0 괄호 짝 딕셔너리로 설정 open bracket일경우 stack에 push open이 없거나, 짝이 안맞을 경우 return False stack에 남아있을 경우(open이..
2021.01.18 -
[LeetCode] 2. Add Two Numbers
LeetCode 2. Add Two Numbers https://leetcode.com/problems/add-two-numbers/ Add Two Numbers - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com Solution # Definition for singly-linked list. class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next class Sol..
2021.01.14 -
[LeetCode] 121. Best Time to Buy and Sell Stock
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ Best Time to Buy and Sell Stock - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 풀이 import collections from typing import Deque from typing import List class Solution: def maxProfit(self, prices: List[int]) -> int: if ..
2021.01.11 -
[LeetCode] 238. Product of Array Except Self
https://leetcode.com/problems/product-of-array-except-self/ Product of Array Except Self - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 풀이 from typing import List class Solution: def productExceptSelf(self, nums: List[int]) -> List[int]: left = [] right = [] result = [] # 왼쪽 곱셈 ..
2021.01.10 -
[LeetCode] 561. Array Partition I
https://leetcode.com/problems/array-partition-i/ Array Partition I - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 내 풀이 1 class Solution: def arrayPairSum(self, nums: List[int]) -> int: nums.sort(reverse=True) sum = 0 for i in range(1, len(nums), 2): sum += nums[i] return sum 내림차..
2021.01.10 -
[LeetCode] 1. Two Sum
https://leetcode.com/problems/two-sum/ Two Sum - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 내 풀이 1 class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: index = [] for i in range(len(nums)): for j in range(i+1, len(nums)): if nums[i] + nums[j] == target:..
2021.01.07