python-algorithm1422 Leetcode 1791. Find Center of Star Graph 1 2 3 4 5 6 7 8 class Solution: def findCenter(self, edges: List[List[int]]) -> int: ans=0 if edges[0][0]==edges[1][0]: ans=edges[0][0] if edges[0][0]==edges[1][1]: ans=edges[0][0] if edges[0][1]==edges[1][0]: ans=edges[0][1] if edges[0][1]==edges[1][1]: ans=edges[0][1] return ans Colored by Color Scripter cs 모든 노드들에 대해서 확인 해볼 필요 없이 처음 두 노드에만 확인하면 됩니다. 2021. 7. 16. Leetcode 1221. Split a String in Balanced Strings 1 2 3 4 5 6 7 8 9 class Solution: def balancedStringSplit(self, s: str) -> int: cnt=0 ans=0 for i in s: if i=='R' : cnt+=1 else: cnt-=1 if cnt==0: ans+=1 return ans Colored by Color Scripter cs 2021. 7. 16. Leetcode 1773. Count Items Matching a Rule 1 2 3 4 5 6 7 8 9 10 11 class Solution: def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int: cnt=0 for i in items: if ruleKey=='type': if i[0]==ruleValue:cnt+=1 if ruleKey=='color': if i[1]==ruleValue:cnt+=1 if ruleKey=='name': if i[2]==ruleValue:cnt+=1 return cnt Colored by Color Scripter cs 2021. 7. 16. Leetcode 1678. Goal Parser Interpretation 1 2 3 4 5 6 class Solution: def interpret(self, command: str) -> str: command=command.replace("()","o") command=command.replace("(","") command=command.replace(")","") return command cs 2021. 7. 16. Leetcode 1389. Create Target Array in the Given Order 1 2 3 4 5 6 class Solution: def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]: target=[] for i in range(len(nums)): target.insert(index[i],nums[i]) return target Colored by Color Scripter cs 2021. 7. 16. Leetcode 1342. Number of Steps to Reduce a Number to Zero 1 2 3 4 5 6 7 8 class Solution: def numberOfSteps(self, num: int) -> int: cnt=0 while num!=0: if num%2==0: num//=2 else: num-=1 cnt+=1 return cnt cs 2021. 7. 15. Leetcode 1281. Subtract the Product and Sum of Digits of an Integer 1 2 3 4 5 6 7 8 class Solution: def subtractProductAndSum(self, n: int) -> int: a,b=1,0 n=str(n) for i in n: a*=int(i) b+=int(i) return a-b Colored by Color Scripter cs 2021. 7. 15. Leetcode 1528. Shuffle String 1 2 3 4 5 6 7 8 9 class Solution: def restoreString(self, s: str, indices: List[int]) -> str: tmp=['0']*len(indices) for i in range(len(indices)): tmp[indices[i]]=s[i] ans='' for i in tmp: ans+=i return ans Colored by Color Scripter cs 2021. 7. 15. Leetcode 1365. How Many Numbers Are Smaller Than the Current Number 1 2 3 4 5 6 7 8 9 class Solution: def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: ans=[] for i in range(len(nums)): tmp=0 for j in range(len(nums)): if nums[i]>nums[j] :tmp+=1 ans.append(tmp) return ans Colored by Color Scripter cs 2021. 7. 15. Leetcode 88. Merge Sorted Array 1 2 3 4 5 6 7 8 9 class Solution: def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: """ Do not return anything, modify nums1 in-place instead. """ for i in range(len(nums1)-m): nums1.pop() for i in range(n): nums1.append(nums2[i]) nums1.sort() Colored by Color Scripter cs 2021. 7. 15. Leetcode 771. Jewels and Stones 1 2 3 4 5 6 7 8 9 10 11 class Solution: def numJewelsInStones(self, jewels: str, stones: str) -> int: tmp=[] for i in jewels: tmp.append(i) tmp=set(tmp) ans=0 for i in tmp: for j in stones: if i==j: ans+=1 return ans Colored by Color Scripter cs 처음에 jewels에 해당하는 문자가 unique가 아닐경우도 생각해서 Line 6 처럼 중복값 지웠는데 제약사항에 있네요 ㅎㅎ 2021. 7. 15. Leetcode 171. Excel Sheet Column Number 1 2 3 4 5 6 class Solution: def titleToNumber(self, columnTitle: str) -> int: ans=0 for i in range(len(columnTitle)): ans+= pow(26,len(columnTitle)-i-1)*int (ord( columnTitle[i])-64) return ans Colored by Color Scripter cs 2021. 7. 15. 이전 1 ··· 69 70 71 72 73 74 75 ··· 119 다음