Algorithm
-
[leetcode] 819. Most Common WordAlgorithm/1일 1코테 2021. 4. 28. 14:21
819. Most Common Word leetcode.com/problems/most-common-word/ Most Common Word - 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. 금지어는 제거할 것 2. 특수문자 제거할 것 3. 소문자 혹은 대문자로 통일할 것 나의 답 def mostCommonWord(self, paragraph: str, banned: List[str]) -> str: import re # 전처리 paragraph = ..
-
[leetcode] 937. Reorder Log FilesAlgorithm/1일 1코테 2021. 4. 28. 09:12
leetcode.com/problems/reorder-data-in-log-files/submissions/ Reorder Data in Log Files - 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. 문자가 숫자의 앞 2. 제일 앞 문자가 동일할 경우 식별자 순으로 나열 3. 숫자로그는 입력 순서대로 처음에 생각한 건 sort로 정렬시킨 다음에 문자/숫자 나누려고 했는데 그러면 숫자로그가 입력순이 아니게 됨 결국 하나하..
-
[leetcode] 344. Reverse StringAlgorithm/1일 1코테 2021. 4. 14. 19:56
문제 leetcode.com/problems/reverse-string/ Reverse String - 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 return할 필요없이 s 안의 값만 변경하는 문제. 단, 새로운 list 생성 등을 하면 안되고 s 안에서만 변경해야 한다. 모범답안 class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s ..
-
[leetcode] 125.Valid PalindromeAlgorithm/1일 1코테 2021. 4. 14. 19:35
문제 입력 문자열이 팰린드롬인지 확인하는 문제 leetcode.com/problems/valid-palindrome/ # 내 답 class Solution: def isPalindrome(self, s: str) -> bool: p = '' # 전처리한 str 담아줄 변수 for i in s: if i.isalpha() or i.isdigit(): # 알파벳이거나 숫자일때만 i = i.lower() # 소문자로 바꿔준 뒤 더해줌 p += i p2 = p[::-1] # 팰린드롬인지 비교하기 위해 뒤집음 for i in range(len(p)): if p[i] != p2[i]: # 0번째부터 하나씩 출력해서 다르면 False return False return True isalpha() + isdigit()..
-
[🐉 백준 13단계] 백트래킹Algorithm/1일 1코테 2021. 1. 4. 15:45
백트래킹: 조건에 따른 모든 조합의 수를 찾는 것. 15649 import itertools a, b = input().split() l = [i for i in range(1, int(a)+1)] s = list(itertools.permutations(l, int(b))) for s in s: a = '' for i in s: a += str(i) a += ' ' print(a) 15650 import itertools a, b = input().split() l = [i for i in range(1, int(a)+1)] s = list(itertools.combinations(l, int(b))) for s in s: a = '' for i in s: a += str(i) a += ' ' prin..
-
[🦩 프로그래머스] 하샤드수, 124 숫자의 나라Algorithm/1일 1코테 2020. 12. 28. 11:26
하샤드 수 def solution(x): a = list(map(int, str(x))) # x를 자릿수별로 짤라서 list if x%sum(a) == 0: # x를 a의 합으로 나눈 나머지가 0이면 True return True return False 124 숫자의 나라 def solution(n): answer = '' while n > 0: n -= 1 answer = '124'[n%3] + answer n //= 3 return answer
-
[🦩 프로그래머스] 최솟값 만들기 / 3진법 뒤집기Algorithm/1일 1코테 2020. 12. 21. 14:57
최솟값 만들기 def solution(A,B): A.sort() B.sort(reverse=True) s = 0 for a, b in zip(A,B): s += a*b return s 3진법 뒤집기 def solution(n): answer = [] while True: answer.append(n%3) n = n//3 if n < 1: break i = 0 s = 0 while answer: a = answer.pop(-1) s += a * 3**i i += 1 return s
-
[🐉 백준 12단계] 정렬Algorithm/1일 1코테 2020. 12. 21. 14:43
백준 12단계 [정렬] 2750 n = int(input()) a = [] for _ in range(n): a.append(int(input())) a.sort() for i in range(len(a)): print(a[i]) 2751 import sys n = int(input()) l = [] for _ in range(n): l.append(int(sys.stdin.readline())) l.sort() for i in l: print(i) 10989 import sys c = [0] * 10001 n = int(input()) for _ in range(n): a = int(sys.stdin.readline()) c[a] += 1 for i in range(len(c)): if c[i] != ..