LeetCode342 leetcode 389. Find the Difference 1 2 3 4 5 6 7 8 9 10 11 12 13 class Solution: def findTheDifference(self, s: str, t: str) -> str: ans='' sList=[] tList=[] for i in s: sList.append(i) for i in t: tList.append(i) for i in sList: tList.pop(tList.index(i)) for i in tList: ans+=i return ans Colored by Color Scripter cs 2022. 3. 24. leetcode 1394. Find Lucky Integer in an Array 1 2 3 4 5 6 class Solution: def findLucky(self, arr: List[int]) -> int: ans=-1 for i in arr: if i>ans and arr.count(i)==i :ans=i return ans cs 2022. 3. 24. leetcode 1491. Average Salary Excluding the Minimum and Maximum Salary 1 2 3 4 5 6 import statistics class Solution: def average(self, salary: List[int]) -> float: salary.sort() salary=salary[1:-1] return statistics.mean(salary) cs 2022. 3. 24. leetcode 1991. Find the Middle Index in Array 1 2 3 4 5 6 class Solution: def findMiddleIndex(self, nums: List[int]) -> int: ans=-1 for i in range( len(nums)): if sum(nums[:i])==sum(nums[i+1:]) : ans=i; break return ans Colored by Color Scripter cs 2022. 3. 24. leetcode 1619. Mean of Array After Removing Some Elements 1 2 3 4 5 6 7 import statistics class Solution: def trimMean(self, arr: List[int]) -> float: arr.sort() lenArr=len(arr) arr=arr[int(0.05*lenArr):int(0.95*lenArr)] return statistics.mean(arr)import statistics cs 2022. 3. 23. leetcode 349. Intersection of Two Arrays 1 2 3 4 5 6 7 class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: ans=[] for i in nums1: if i in nums2: ans.append(i) ans=list(set(ans)) return ans Colored by Color Scripter cs 2022. 3. 23. leetcode 2085. Count Common Words With One Occurrence 1 2 3 4 5 6 class Solution: def countWords(self, words1: List[str], words2: List[str]) -> int: ans=0 for i in words1: if i in words2 and words1.count(i)==1 and words2.count(i)==1:ans+=1 return ans Colored by Color Scripter cs 2022. 3. 22. leetcode 852. Peak Index in a Mountain Array 1 2 3 4 class Solution: def peakIndexInMountainArray(self, arr: List[int]) -> int: maxIndex=arr.index(max(arr)) return maxIndex Colored by Color Scripter cs 2022. 3. 22. leetcode 1380. Lucky Numbers in a Matrix 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class Solution: def luckyNumbers (self, matrix: List[List[int]]) -> List[int]: rowMin = [] colMax=[] ans=[] for i in matrix: rowMin.append(min(i)) for j in range(len(matrix[0])): tmpCol=[] for i in range(len(matrix)): tmpCol.append(matrix[i][j]) colMax.append(max(tmpCol)) for i in rowMin: if i in colMax: ans.append(i) return ans Colored by Color Scripter cs 2022. 3. 22. leetcode 1356. Sort Integers by The Number of 1 Bits 1 2 3 4 class Solution: def sortByBits(self, arr: List[int]) -> List[int]: arr.sort( key= lambda x: (bin(x)[2:].count('1'),x ) ) return arr Colored by Color Scripter cs 이 문제는 정렬문제인데, Python 에서는 정렬의 key를 lambda 함수룰 통해 쉽게 지정이 가능하고 문제에서 요구한 기준 첫번째 이진수로 바꿨을때 1의 갯수, 두번째 1의갯수가 같으면 원래 숫자 순서 를 각각 key에 지정 2022. 3. 22. leetcode 977. Squares of a Sorted Array 1 2 3 4 5 6 class Solution: def sortedSquares(self, nums: List[int]) -> List[int]: ans=[] for i in nums: ans.append(i**2) ans.sort() return ans Colored by Color Scripter cs 2022. 3. 22. leetcode 1207. Unique Number of Occurrences 1 2 3 4 5 6 7 class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: setArr=list(set(arr)) ans=[] for i in setArr: ans.append(arr.count(i)) if sorted(ans)== sorted(list(set(ans))): return True else : return False Colored by Color Scripter cs 2022. 3. 22. 이전 1 ··· 18 19 20 21 22 23 24 ··· 29 다음