본문 바로가기

LeetCode342

leetcode 47. Permutations II 1 2 3 4 5 6 7 8 from itertools import permutations class Solution: def permuteUnique(self, nums: List[int]) -> List[List[int]]: ans=[] for i in permutations(nums) : if list(i) not in ans: ans.append(list(i)) return(ans) Colored by Color Scripter cs 2022. 5. 12.
leetcode 1679. Max Number of K-Sum Pairs 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Solution: def maxOperations(self, nums: List[int], k: int) -> int: ans=0 nums.sort() start=0 end=len(nums)-1 while end>start : if nums[end] + nums[start] > k : end-=1 elif nums[end] + nums[start] 2022. 5. 4.
leetcode 844. Backspace String Compare 1 2 3 4 5 6 7 8 9 10 11 12 13 def sol(s): for i in range(len(s)): if '#' in s : if s[0]=='#' : s=s[1:] else : s = s[:s.find('#') - 1] + s[s.find('#') + 1:] else : break return s class Solution: def backspaceCompare(self, s: str, t: str) -> bool: if sol(s)== sol(t) : return True else: return False Colored by Color Scripter cs 2022. 5. 2.
leetcode 905. Sort Array By Parity 1 2 3 4 5 6 7 8 9 10 11 def sol(nums): even_list=[] odd_list=[] for i in nums : if i%2 == 0 : even_list.append(i) else : odd_list.append(i) even_list.extend(odd_list) return even_list class Solution: def sortArrayByParity(self, nums: List[int]) -> List[int]: return sol(nums) Colored by Color Scripter cs 2022. 5. 2.
leetcode 1910. Remove All Occurrences of a Substring 1 2 3 4 5 6 7 class Solution: def removeOccurrences(self, s: str, part: str) -> str: for i in range(len(s)): if part in s: s = s[:s.find(part)] + s[s.find(part) + len(part):] else : break return(s) Colored by Color Scripter cs 처음에는 단순하게 replace로 했다가 문자열 s에서 part 가 시작되는 부분 찾아서 쪼개는 방식으로 코드 작성함 Line 6은 예외처리고 part 가 s에 없는 경우에는 문자열을 쪼개면 안됨! 2022. 4. 26.
leetcode 46. Permutations 1 2 3 4 5 6 7 from itertools import permutations class Solution: def permute(self, nums: List[int]) -> List[List[int]]: ans=[] for i in permutations(nums) : ans.append(list(i)) return(ans) Colored by Color Scripter cs 1 2 3 4 5 from itertools import permutations class Solution: def permute(self, nums: List[int]) -> List[List[int]]: ans=list(map(list, (permutations(nums)))) return(ans) Colored by.. 2022. 4. 26.
leetcode 2221. Find Triangular Sum of an Array 1 2 3 4 5 6 7 8 class Solution: def triangularSum(self, nums: List[int]) -> int: for j in range(len(nums)-1): tmp=[] for i in range(1,len(nums)): tmp.append( (nums[i]+nums[i-1])%10 ) nums=tmp return(nums[0]) cs 2022. 4. 25.
leetcode 345. Reverse Vowels of a String 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class Solution: def reverseVowels(self, s: str) -> str: vowels=['a','e','i','o','u','A','E','I','O','U'] my_vowel=[] start=0 ans='' for i in s: if i in vowels : my_vowel.append(i) my_vowel=my_vowel[::-1] for i in s: if i in vowels : ans+=my_vowel[start] start+=1 else: ans+=i return(ans) Colored by Color Scripter cs 2022. 4. 21.
leetcode 504. Base 7 1 2 3 4 import numpy class Solution: def convertToBase7(self, num: int) -> str: return(numpy.base_repr(num, base=7)) cs numpy 라이브러리에 base 변환해주는 함수가 있네요..! 2022. 4. 18.
leetcode 1752. Check if Array Is Sorted and Rotated 1 2 3 4 5 6 7 8 9 10 11 class Solution: def check(self, nums: List[int]) -> bool: ans = False target=sorted(nums) for i in range(len(nums)): nums.append(nums[0]) nums.pop(0) if nums == target : ans=True break return(ans) cs 2022. 4. 18.
leetcode 1796. Second Largest Digit in a String 1 2 3 4 5 6 7 8 9 class Solution: def secondHighest(self, s: str) -> int: num_list=[] for i in s: if i.isdecimal() : num_list.append(int(i)) num_list=sorted(list(set(num_list)), reverse=True) if len(num_list) 2022. 4. 18.
leetcode 1154. Day of the Year 1 2 3 4 5 6 import datetime class Solution: def dayOfYear(self, date: str) -> int: year,month,days=map(int, date.split('-')) ans=datetime.date(year,month,days).timetuple().tm_yday return ans Colored by Color Scripter cs 2022. 4. 18.