Problem Solving with Algorithms

반응형

이 문제들은 내가 목표로 하는 회사에서 내는 스타일은 아닌것 같다.

 

 

811. Subdomain Visit Count

leetcode.com/problems/subdomain-visit-count/

 

class Solution:
    def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
        ans = collections.Counter()
        for domain in cpdomains:
            count, domain = domain.split()
            count = int(count)
            frags = domain.split('.')
            for i in range(len(frags)):
                ans[".".join(frags[i:])] += count
        
        return ["{} {}".format(ct, dom) for dom, ct in ans.items()]

파이썬...이 문제 다른 사람 코드 한번 다시 보기.

 

 


 

 

1480. Running Sum of 1d Array

leetcode.com/problems/running-sum-of-1d-array/

 

class Solution:
    def runningSum(self, nums: List[int]) -> List[int]:
        for i in range(1, len(nums)):
            nums[i] += nums[i-1]
            
        return nums

 

    def runningSum(self, A):
        return list(itertools.accumulate(A))

 

class Solution:
    def runningSum(self, nums: List[int]) -> List[int]:
        return accumulate(nums)   

 

def runningSum(self, nums: List[int]) -> List[int]:
	result = []
	result.append(nums[0])
	for i in range(1, len(nums)):
		result.append(result[-1]+nums[i])
	return result

 


572. Subtree of Another Tree

leetcode.com/problems/subtree-of-another-tree/

class Solution:
    def isSubtree(self, s: TreeNode, t: TreeNode) -> bool:
        
        if not s: 
            return False
        if self.isSameTree(s, t): 
            return True
        return self.isSubtree(s.left, t) or self.isSubtree(s.right, t)

    def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
        if p and q:
            return p.val == q.val and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
        return p is q

 


 

 

leetcode.com/problems/missing-number/

 

 

비트연산자

class Solution:
    def missingNumber(self, nums: List[int]) -> int:
        missing = len(nums)
        for i, num in enumerate(nums):
            missing ^= i ^ num
        return missing

 

 

 

 

 

반응형
반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band