python-algorithm1422 Leetcode 268. Missing Number 1 2 3 4 5 6 class Solution: def missingNumber(self, nums: List[int]) -> int: nums_sum=sum(nums) n=len(nums) compared=n*(n+1)//2 return compared-nums_sum cs 2022. 3. 28. leetcode 2129. Capitalize the Title 1 2 3 4 5 6 7 8 9 10 11 12 13 class Solution: def capitalizeTitle(self, title: str) -> str: titleList=list(title.split()) ans='' for i in titleList : i = i.lower() if len(i) 2022. 3. 25. leetcode 217. Contains Duplicate 1 2 3 4 5 6 class Solution: def containsDuplicate(self, nums: List[int]) -> bool: ans=False numsSet=list(set(nums)) if sorted(nums) != sorted(numsSet) : ans=True return ans Colored by Color Scripter cs 2022. 3. 24. 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. 이전 1 ··· 47 48 49 50 51 52 53 ··· 119 다음