일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- 코틀린기초
- python xor
- LeetCode
- binary search
- 릿코드 파이썬
- leetcode 풀기
- python Leetcode
- 릿코드풀이
- 파이썬알고리즘
- 릿코드풀기
- python 릿코드
- 잇츠디모
- 파이썬 프로그래머스
- python sorted
- 상가수익률계산기
- leetcode풀기
- 알고리즘풀기
- 파이썬릿코드
- 알고리즘풀이
- 파이썬 알고리즘
- 파이썬알고리즘풀기
- 파이썬 릿코드
- 파이썬릿코드풀기
- python priority queue
- python 알고리즘
- python zip_longest
- 릿코드 풀기
- 파이썬 알고리즘 풀기
- 릿코드
- leetcode풀이
- Today
- Total
목록알고리즘/LeetCode (177)
소프트웨어에 대한 모든 것
https://leetcode.com/problems/single-number/ Single Number - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제) 풀이 전략) XOR 교환법칙 결합법칙 이용 A^A = 0 A^0=A A^A^B^B^C=0^0^A=A 솔루션1) class Solution: def singleNumber(self, nums: List[int]) -> int: # 모든 값을 xor 취하면 하나만 남음 ret = 0 for num in n..
771. Jewels and Stones 문제) You're given strings jewels representing the types of stones that are jewels, and stones representing the stones you have. Each character in stones is a type of stone you have. You want to know how many of the stones you have are also jewels. Letters are case sensitive, so "a" is considered a different type of stone from "A". 솔루션1) class Solution: def numJewelsInStones..
226. Invert Binary Tree 문제) Given the root of a binary tree, invert the tree, and return its root. 솔루션1) # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: self.invert(root) return root def invert(se..
461. Hamming Distance 문제) 솔루션1) xor 연산을 취한 후 비트연산 class Solution: def hammingDistance(self, x: int, y: int) -> int: xor = x ^ y return sum((xor>>i & 1) for i in range(31)) 솔루션2) xor 연산을 취한 후 내장함수 bin()을 사용해서 이진수 문자열로 변환 후 1을 셈 class Solution: def hammingDistance(self, x: int, y: int) -> int: xor = x^y return bin(xor).count('1')
1684. Count the Number of Consistent Strings 문제) 솔루션1) allowed 단어에 words가 있는지 탐색 O(n2) class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: # Brute-Force count = 0 for word in words: count += 1 for i in range(len(word)): if word[i] not in allowed: count -= 1 break return count 솔루션2) class Solution: def countConsistentStrings(self, allowed: str, words: List[str..
1588. Sum of All Odd Length Subarrays 문제) 솔루션1) class Solution: def sumOddLengthSubarrays(self, arr: List[int]) -> int: res = 0 n = len(arr) for j in range(1, len(arr)+1, 2): for i in range(n - j + 1): res += sum(arr[i:i+j]) return res
1512. Number of Good Pairs 문제) 솔루션1) class Solution: def numIdenticalPairs(self, nums: List[int]) -> int: # brute-force # O(n2) count = 0 for i in range(len(nums)): for j in range(1, len(nums)): if nums[i] == nums[j] and i int: count_dict = defaultdict(int) count = 0 for num in nums: count += count..
1773. Count Items Matching a Rule 문제) 솔루션1) class Solution: def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int: type_dict = {} color_dict = {} name_dict ={} for item in items: if item[0] in type_dict: type_dict[item[0]] += 1 else: type_dict[item[0]] = 1 if item[1] in color_dict: color_dict[item[1]] += 1 else: color_dict[item[1]] = 1 if item[2] in name_dict: name_..