분류 전체보기 (174) 썸네일형 리스트형 [Leetcode] 2485. Find the Pivot Integer https://leetcode.com/problems/find-the-pivot-integer class Solution: def pivotInteger(self, n: int) -> int: for x in range(n + 1): sum1 = 0 sum2 = 0 for i in range(1, x + 1): sum1 += i for i in range(x, n + 1): sum2 += i if sum1 == sum2: return x return -1 [Leetcode] 791. Custom Sort String https://leetcode.com/problems/custom-sort-string class Solution: def customSortString(self, order: str, s: str) -> str: result = '' for i in order: for j in range(s.count(i)): result += i s = s.replace(i, '') result += s return result [Leetcode] 3074. Apple Redistribution into Boxes https://leetcode.com/problems/apple-redistribution-into-boxes class Solution: def minimumBoxes(self, apple: List[int], capacity: List[int]) -> int: sum = 0 for n in apple: sum += n print('sum: ' + str(sum)) capacity.sort(reverse=True) c_sum = 0 for i in range(len(capacity)): c_sum += capacity[i] if c_sum >= sum: break return i + 1 [Leetcode] 349. Intersection of Two Arrays https://leetcode.com/problems/intersection-of-two-arrays class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: return list(set(nums1).intersection(nums2)) [Leetcode] 2540. Minimum Common Value https://leetcode.com/problems/minimum-common-value class Solution: def getCommon(self, nums1: List[int], nums2: List[int]) -> int: common = list(set(nums1).intersection(nums2)) common.sort() if len(common) == 0: return -1 return common[0] [Leetcode] 3005. Count Elements With Maximum Frequency https://leetcode.com/problems/count-elements-with-maximum-frequency class Solution: def maxFrequencyElements(self, nums: List[int]) -> int: frequency = {} unique = list(set(nums)) for n in unique: count = nums.count(n) if frequency.get(count, None) is None: frequency[count] = 1 else: frequency[count] += 1 max_f = max(frequency.keys()) return max_f * frequency[max_f] [Leetcode] 2864. Maximum Odd Binary Number https://leetcode.com/problems/maximum-odd-binary-number class Solution: def maximumOddBinaryNumber(self, s: str) -> str: count = [0, 0] count[0] = s.count('0') count[1] = s.count('1') s = '' for i in range(count[1] - 1): s += '1' for i in range(count[0]): s += '0' s += '1' return s [Leetcode] 876. Middle of the Linked List https://leetcode.com/problems/middle-of-the-linked-list # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]: count = 0 node = head while node.next != None: count += 1 node = node.next middle = ceil(count / 2) node = head for i in rang.. 이전 1 ··· 5 6 7 8 9 10 11 ··· 22 다음