Algorithm(218)
-
[Leetcode]125. Valid Palindrome
1. problem : https://leetcode.com/problems/valid-palindrome/ 2. solution 1 :class Solution: def isPalindrome(self, s: str) -> bool: new = '' for a in s: if a.isalpha() or a.isdigit(): new += a.lower() return (new == new[::-1])new와 new를 reverse 시킨 것을 비교해서 return 한다. 3. solution 2 :class Solution: def isPalindrome(self, s: str) -> bool: ..
2024.07.31 -
[Leetcode]128. Longest Consecutive Sequence
1. problem :https://leetcode.com/problems/longest-consecutive-sequence/description/ 2. solution 1 :class Solution: def longestConsecutive(self, nums: List[int]) -> int: if len(nums) == 0 or len(nums) == 1: return len(nums) nums = sorted(nums) max_count = 1 count = 1 for i in range(1,len(nums)): if nums[i] == nums[i-1]: ..
2024.07.31 -
[Leetcode]36. Valid Sudoku(x)
1. problem :https://leetcode.com/problems/valid-sudoku/description/ 2. solution 1 :class Solution: def isValidSudoku(self, board: List[List[str]]) -> bool: rows = [set() for _ in range(9)] cols = [set() for _ in range(9)] boxes = [set() for _ in range(9)] for r in range(9): for c in range(9): num = board[r][c] if num != '.':..
2024.07.31 -
[Leetcode]1456. Maximum Number of Vowels in a Substring of Given Length
1. problem :https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/ 2. solution 1 :class Solution: def maxVowels(self, s: str, k: int) -> int: vowels = set("aeiou") current_count = 0 max_count = 0 # initialize window for i in range(k): if s[i] in vowels: current_count += 1 max_count ..
2024.07.30 -
[Leetcode]643. Maximum Average Subarray I
1. problem : https://leetcode.com/problems/maximum-average-subarray-i/description/?envType=study-plan-v2&envId=leetcode-75 2. solution 1 :class Solution: def findMaxAverage(self, nums: List[int], k: int) -> float: temp_sum = sum(nums[:k]) max_sum = temp_sum for i in range(1,len(nums)-k+1): temp_sum = temp_sum - nums[i-1] + nums[i+k-1] if temp_sum ..
2024.07.30 -
[Leetcode]792. Number of Matching Subsequences(x)
1. problem : https://leetcode.com/problems/number-of-matching-subsequences/ 2. solution 1 : from collections import defaultdictfrom typing import List, Iteratorclass Solution: def numMatchingSubseq(self, s: str, words: List[str]) -> int: # defaultdict 초기화: 각 문자가 현재 기다리는 단어들을 저장 waiting = defaultdict(list) # 각 단어의 첫 문자를 키로 하고, 나머지 문자의 이터레이터를 값으로 설정 for word ..
2024.07.29