Zero to Hero
136. Single Number
Algorithm 2021. 4. 22. 14:31

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 1. 집합 사용 class Solution: def singleNumber(self, nums: List[int]) -> int: set1 = set() for num in nums: if num not in set1: set1.add(num) else: set1.remove(num) return set1[0] 2. 조건의 특성 사용, 2*(a+b+c) - (2a+2b+c) = c cla..

article thumbnail
617. Merge Two Binary Trees
Algorithm 2021. 4. 21. 12:36

Merge Two Binary Trees - 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 1. 나의 Trash Garbage 코드 class Solution: def search(self, p1, p2): p1.val += (p2.val if p2 else 0) if p1.left: if p2: self.search(p1.left, p2.left) else: self.search(p1.left, TreeNode(0, None, None)) else: if p2..

14. Longest Common Prefix
Algorithm 2021. 4. 20. 16:42

Longest Common Prefix - 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 1. 완전 탐색 from typing import * class Solution: def sol(self, strs, length): if length == 0: return True for strr in strs: # length 보다 문자열이 짧은 상황은 불가능하니깐 반환 if len(strr) < length: return False if strs[0][:length]..

article thumbnail
424. Longest Repeating Character Replacement
Algorithm 2021. 4. 19. 14:03

Longest Repeating Character Replacement - 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 삽질 1 (실패) from typing import * from collections import defaultdict # 주어진 문자열에서 지정된 횟수만큼 알파벳을 교환했을 때 # 동일한 문자로 구성된 부분 문자열의 길이의 최댓값을 반환하시오 class Solution: def characterReplacement(self, s: str, ..

2174번: 로봇 시뮬레이션
Algorithm 2021. 4. 18. 17:37

1. 소스 코드 import sys from collections import deque r = sys.stdin.readline width, height = map(int, r().split()) robot_num, command_num = map(int, r().split()) board = [[0] * width for _ in range(height)] robots = {} for i in range(robot_num): y, x, direction = r().split() ny = int(y) - 1 nx = height - int(x) robots[i + 1] = [nx, ny, direction] board[nx][ny] = i + 1 commands = [] for i in range(co..

article thumbnail
7. Reverse Integer
Algorithm 2021. 4. 17. 21:44

Reverse Integer - 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 1. 내 풀이 class Solution: def reverse(self, x: int) -> int: x = int(str(x)[::-1]) if str(x)[-1] != "-" else int("-" + str(x)[::-1][:-1]) return x if abs(x) < 2 ** 31 else 0