LeetCode342 leetcode 1464. Maximum Product of Two Elements in an Array 1 2 3 4 5 6 7 class Solution: def maxProduct(self, nums: List[int]) -> int: first=max(nums) nums.pop(nums.index(first)) second=max(nums) ans=(first-1)*(second-1) return ans cs 2022. 3. 16. leetcode 125. Valid Palindrome 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 def isPalindromic(s): ans=False convertedS='' for i in s: i=i.lower() #i=i.replace(" ",'') if i.isalnum(): convertedS+=i sLength=len(convertedS) if sLength%2==0: if convertedS[:sLength//2] ==convertedS[sLength//2:][::-1] :ans=True else: if convertedS[:sLength//2] ==convertedS[sLength//2+1:][::-1] :ans=True return ans class Solution: def isPalindrome(self, s.. 2022. 3. 15. leetcode 2108. Find First Palindromic String in the Array 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 def isPalindromic(s): ans='' sLength=len(s) if sLength%2==0: if s[:sLength//2] ==s[sLength//2:][::-1] :ans=s else: if s[:sLength//2] ==s[sLength//2+1:][::-1] :ans=s return ans class Solution: def firstPalindrome(self, words: List[str]) -> str: ans='' for i in words: if isPalindromic(i)!='' : ans=isPalindromic(i) break return ans Colored by Color.. 2022. 3. 15. leetcode 2176. Count Equal and Divisible Pairs in an Array 1 2 3 4 5 6 7 class Solution: def countPairs(self, nums: List[int], k: int) -> int: ans=0 for i in range(len(nums)): for j in range(i+1,len(nums)): if nums[i]==nums[j] and i*j%k==0 :ans+=1 return ans Colored by Color Scripter cs 2022. 3. 15. leetcode 1844. Replace All Digits with Characters 1 2 3 4 5 6 7 class Solution: def replaceDigits(self, s: str) -> str: ans='' for i in range(len(s)): if s[i].isalpha(): ans+=s[i] if s[i].isdigit(): ans+=chr(ord(s[i-1])+int(s[i])) return ans cs 2022. 3. 15. leetcode 2089. Find Target Indices After Sorting Array 1 2 3 4 5 6 7 8 class Solution: def targetIndices(self, nums: List[int], target: int) -> List[int]: ans=[] nums.sort() for i in range(len(nums)): if target==nums[i]: ans.append(i) #if target 2022. 3. 15. leetcode 1816. Truncate Sentence 1 2 3 4 5 6 7 class Solution: def truncateSentence(self, s: str, k: int) -> str: sList=s.split() ans=sList[0] for i in range(1, min(k,len(sList))): ans=ans+' '+sList[i] return ans Colored by Color Scripter cs 2022. 3. 7. leetcode 1688. Count of Matches in Tournament 1 2 3 4 5 6 7 class Solution: def numberOfMatches(self, n: int) -> int: ans=0 while n>1: ans+=n//2+n%2 n//=2 return ans cs 2022. 3. 7. Leetcode 2006. Count Number of Pairs With Absolute Difference K 1 2 3 4 5 6 7 class Solution: def countKDifference(self, nums: List[int], k: int) -> int: cnt=0 for i in range(len(nums)): for j in range(i+1,len(nums)): if abs (nums[i]-nums[j])==k: cnt+=1 return cnt Colored by Color Scripter cs 2022. 3. 7. leetcode 2194. Cells in a Range on an Excel Sheet 1 2 3 4 5 6 7 8 9 10 11 12 class Solution: def cellsInRange(self, s: str) -> List[str]: ans=[] s1,s2=s.split(':') start=ord(s1[0]) end=ord(s2[0]) sheetStart=int(s1[1]) sheetEnd=int(s2[1]) for i in range(start,end+1): for j in range(sheetStart,sheetEnd+1): ans.append(chr(i)+str(j)) return ans Colored by Color Scripter cs 2022. 3. 7. leetcode 2160. Minimum Sum of Four Digit Number After Splitting Digits 1 2 3 4 5 6 7 8 9 10 class Solution: def minimumSum(self, num: int) -> int: tmp1='' tmp2='' numStr=list(str(num)) numStr.sort() for i in range(len(numStr)): if i%2==0: tmp1+=numStr[i] else :tmp2+=numStr[i] return int(tmp1)+int(tmp2) cs 2022. 3. 7. leetcode 2114. Maximum Number of Words Found in Sentences 1 2 3 4 5 6 class Solution: def mostWordsFound(self, sentences: List[str]) -> int: start=0 for i in sentences: start=max(start,i.count(' ')) return start+1 Colored by Color Scripter cs 2022. 3. 7. 이전 1 ··· 21 22 23 24 25 26 27 ··· 29 다음