본문 바로가기

전체 글267

[LeetCode] Reverse String 파이썬 (in-place, list.reverse()) class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ i, j = 0, len(s) - 1 while i < j: s[i], s[j] = s[j], s[i] i += 1 j -= 1 왼쪽 끝부터, 오른쪽 끝부터 탐색하면서 서로 swap string의 길이가 짝, 홀 상관없이 i None: """ Do not return anything, modify s in-place instead. """ s.reverse() 2021. 4. 12.
[LeetCode] Word Pattern 파이썬 (isomorphic, contract mapping) class Solution: def wordPattern(self, pattern: str, s: str) -> bool: s = list(s.split(" ")) if len(pattern) != len(s): return False s2t, t2s = {}, {} for p, w in zip(pattern, s): if w not in s2t and p not in t2s: s2t[w] = p t2s[p] = w elif w not in s2t or s2t[w] != p: # Contradict mapping. return False return True 이 문제를 본 순간 [LeetCode] Isomorphic Strings 문제가 떠올랐다. ygseo.tistory.com/221 len으로 먼저 .. 2021. 4. 12.
[LeetCode] Move Zeros 파이썬 (in-place) in-place modifiy class Solution(object): def moveZeroes(self, nums): """ :type nums: List[int] :rtype: void Do not return anything, modify nums in-place instead. """ pos = 0 for i in range(len(nums)): if nums[i]: nums[i], nums[pos] = nums[pos], nums[i] pos += 1 nums[i]가 0이 아닌경우 0의 위치와 swap한다. # i track non zero location # pos track zero location # print(nums[i], nums[pos] , "i={}, pos={}".format.. 2021. 4. 12.
[LeetCode] Missing Number 파이썬 class Solution: def missingNumber(self, nums: List[int]) -> int: nums = sorted(nums) for idx, value in enumerate(nums): if idx != nums[idx]: return idx else: return len(nums) enumerate 써서 idx로 확인 [0,1] 같은 경우 n은 2이지만 for문 안에서는 None으로 return 되기 때문에 이와 같은 경우는 for-else문을 써서 for문이 오류없이 종료된다면 [0,1] 같은 경우이기 때문에 return len(nums)를 해준다. 2021. 4. 12.
[LeetCode] Ugly Number 파이썬 class Solution: # @param {integer} num # @return {boolean} def isUgly(self, num): if num == 0: return False for i in [2, 3, 5]: while num % i == 0: num /= i return num == 1 2021. 4. 11.
[LeetCode] Add Digits 파이썬 class Solution: def addDigits(self, num: int) -> int: if num = 2: num = list(str(num)) # print(num) num=sum([int(x) for x in num]) # print(num) len_num = len(list(str(num))) return num ['3', '8'] 11 ['1', '1'] 2 아무래도 list로 만들고 다시 sum으로 list comprehension을 수행하는게 비효율적인 과정이라서 속도가 느린편으로 나왔다. 다른 풀이 class Solution: def addDigits(self, num: int) ->.. 2021. 4. 11.
[LeetCode] Trips and Users (date between) # Write your MySQL query statement below select Trips.Request_at as Day, round(sum(if(status != 'completed',1,0)) / count(*), 2) as 'Cancellation Rate' from Trips, Users where Trips.Client_Id = Users.Users_Id and Users.Banned = "No" and Trips.Request_at BETWEEN '2013-10-01' and '2013-10-03' group by Trips.Request_at 출처: tanwirkhan.medium.com/leet-code-262-trips-and-users-5899bf04ec0e 2021. 4. 10.
[LeetCode] Department Top Three Salaries (partition by, rank, sub query) select b.Name as Department , a.Name as Employee, a.Salary from (select *, dense_rank() over (partition by DepartmentId order by salary desc) as sal_rank from Employee ) a join Department b on a.DepartmentId = b.Id where a.sal_rank 2021. 4. 10.
[LeetCode] Department Highest Salary ( sub query, partition by) # Write your MySQL query statement below select a.Department, a.Employee, a.Salary from (select D.Name as Department, E.Name as Employee, E.Salary, rank() over (partition by D.Name order by Salary desc) as 'rank' from Employee E join Department D on E.DepartmentId = D.Id) as a where a.rank = 1; 서브쿼리부터 보자, from employee 에서 join 으로 employee and deparment을 수행 select 수행 select에서 rank를 사용하는데 여기서 part.. 2021. 4. 10.