본문 바로가기
반응형

SWE/코테137

[Leetcode][python3] 442. Find All Duplicates in an Array problem: https://leetcode.com/problems/find-all-duplicates-in-an-array/ code: class Solution: def findDuplicates(self, nums: List[int]) -> List[int]: hashmap = dict() result = [] for num in nums: # n times if num in hashmap : #O(1) result.append(num) else: hashmap[num]=True return result # time complexity # O(n) # space complextiy # O(n) code: class Solution: def findDuplicates(self, nums: List[.. 2022. 3. 10.
[Leetcode][python3] 69. Sqrt(x) problem: code: # result^2 = x # x= 2 -> output : 1 # x= 3 -> output : 1 # 4 output # 9 output class Solution: def mySqrt(self, x: int) -> int: # edge case if x==0 : return 0 result =1 while result*result 1: approach = int((left+right)/2) if approach*approach >x: right = approach elif approach*approach < x: left = approach elif approach*approach == x: return approach return left time complexity i.. 2022. 3. 10.
[Leetcode][python3] 7. Reverse Integer Problem: Code 1 : I used String API in Python. First, convert Integer X to String Second, check if last character is 0 or not reverse string class Solution: def reverse(self, x: int) -> int: if x == 0: return 0 s = str(x) negativeFlag = 1 if s[0] == '-': negativeFlag = -1 s = s[1:] index = 0 while index (2**31-1) or (-2**31)>result : return 0 return result Code 2: I'm not sure if it is right way.. 2022. 3. 8.
HackerRank - Tree: Huffman Decoding 문제 : https://www.hackerrank.com/challenges/tree-huffman-decoding/problem?h_r=internal-search 해결 코드 : import sys sys.setrecursionlimit(2000) def decodeHuff(root, s): result = [] def _recursive(cur, root, binary): #print(f'cur.data{cur.data} binary{binary}') if cur.left==None and cur.right==None : result.append(cur.data) return _recursive(root, root, binary) if not binary: return if binary[0] == '.. 2022. 3. 6.
반응형