본문 바로가기

Counter3

[자료구조] 나머지 한 점 파이썬 Counter를 사용해서 풀었는데 더 간편하게 풀 수 있는 방법이 있을 것 같다. from collections import Counter def solution(v): x = [x for x,y in v] y = [y for x,y in v] # print(x,y) xd = Counter(x) yd = Counter(y) answer = [] for k,v in xd.items(): if v == 1: answer.append(k) for k,v in yd.items(): if v == 1: answer.append(k) return answer # return answer 2021. 4. 20.
[LeetCode] Longest Palindrome 파이썬 (bitwise &, Counter) from collections import Counter class Solution(object): def longestPalindrome(self, s): """ :type s: str :rtype: int """ odds = 0 for k, v in Counter(s).items(): odds += v & 1 return len(s) - odds + int(odds > 0) lambda, map을 사용해서 홀 수 인 s 구하기 def longestPalindrome2(self, s): """ :type s: str :rtype: int """ odd = sum(map(lambda x: x & 1, Counter(s).values())) return len(s) - odd + int(odd > 0) 홀.. 2021. 4. 16.
[LeetCode] Find the Difference 파이썬 (Counter, A.subtract(B), bit operation, xor, reduce) from collections import Counter class Solution: def findTheDifference(self, s: str, t: str) -> str: if len(s) == 0: return t # s = "" S = Counter(s) T = Counter(t) T.subtract(S) # inplace res = [k for k, v in T.items() if v > 0] return res[0] Counter 클래스에는 subtract라는 메서드가 있다. inplace이기 때문에 이 점을 유의할 것. 다른 풀이들 class Solution(object): def findTheDifference(self, s, t): """ :type s: str :type t: str.. 2021. 4. 14.