문제 : https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/description/ 난이도 : Easy 풀이 class Solution: def numberOfSteps(self, num: int) -> int: cnt = 0 while num > 0: if num % 2 == 0: num /= 2 else: num -= 1 cnt += 1 return cnt 이건 뭐... 너무 쉬워서 할 말이 없움ㅎ 다른 풀이 class Solution: def numberOfSteps(self, num: int) -> int: return bin(num).count("1") * 2 + bin(num).count("0") - 2 한 줄로 끝..
문제 : https://leetcode.com/problems/find-the-town-judge/description/ 난이도 : Lv.1 풀이 class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: if not trust: if n == 1: return 1 else: return -1 relationship = [[] for _ in range(n+1)] for a, b in trust: #a는 b를 신뢰 relationship[b].append(a) for i in range(1, n+1): if len(relationship[i]) == n-1: for t in range(1, n+1): if i in relatio..
문제 : https://leetcode.com/problems/longest-common-prefix/description/ 난이도 : Easy 풀이 class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: if len(strs) == 1: return strs[0] strs = sorted(strs) minLen = min(len(strs[0]), len(strs[-1])) i = 0 while i < minLen and strs[0][i] == strs[-1][i]: i += 1 prefix = strs[0][0:i] return prefix 배열을 다 훑어서 하나하나 비교해야 될 것 같지만, 사실은 오름차순 정렬 후 처음과 끝의 ..