본문 바로가기

python-algorithm1422

leetcode 1356. Sort Integers by The Number of 1 Bits 1 2 3 4 class Solution: def sortByBits(self, arr: List[int]) -> List[int]: arr.sort( key= lambda x: (bin(x)[2:].count('1'),x ) ) return arr Colored by Color Scripter cs 이 문제는 정렬문제인데, Python 에서는 정렬의 key를 lambda 함수룰 통해 쉽게 지정이 가능하고 문제에서 요구한 기준 첫번째 이진수로 바꿨을때 1의 갯수, 두번째 1의갯수가 같으면 원래 숫자 순서 를 각각 key에 지정 2022. 3. 22.
leetcode 977. Squares of a Sorted Array 1 2 3 4 5 6 class Solution: def sortedSquares(self, nums: List[int]) -> List[int]: ans=[] for i in nums: ans.append(i**2) ans.sort() return ans Colored by Color Scripter cs 2022. 3. 22.
leetcode 1207. Unique Number of Occurrences 1 2 3 4 5 6 7 class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: setArr=list(set(arr)) ans=[] for i in setArr: ans.append(arr.count(i)) if sorted(ans)== sorted(list(set(ans))): return True else : return False Colored by Color Scripter cs 2022. 3. 22.
leetcode 2124. Check if All A's Appears Before All B's 1 2 3 4 5 6 class Solution: def checkString(self, s: str) -> bool: compare=[] for i in s: compare.append(i) if compare == sorted(compare) :return True else : return False cs 2022. 3. 21.
leetcode 1460. Make Two Arrays Equal by Reversing Sub-arrays 1 2 3 4 5 6 7 class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: target.sort() arr.sort() if target==arr : return True else : return False Colored by Color Scripter cs 2022. 3. 21.
leetcode 2032. Two Out of Three 1 2 3 4 5 6 7 8 9 10 11 class Solution: def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]: ans=[] for i in nums1: if i in nums2 or i in nums3: ans.append(i) for i in nums2: if i in nums1 or i in nums3: ans.append(i) for i in nums3: if i in nums2 or i in nums1: ans.append(i) ans= list(set(ans)) return ans Colored by Color Scripter cs 2022. 3. 21.
leetcode 1880. Check if Word Equals Summation of Two Words 1 2 3 4 5 6 7 8 9 10 11 class Solution: def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool: wordToint1='' wordToint2='' wordToint3='' for i in firstWord: wordToint1+=str(ord(i)-97) for i in secondWord: wordToint2+=str(ord(i)-97) for i in targetWord: wordToint3+=str(ord(i)-97) if int(wordToint1)+int(wordToint2)==int(wordToint3) : return True else: return False Colored .. 2022. 3. 21.
leetcode 2053. Kth Distinct String in an Array 1 2 3 4 5 6 7 8 9 class Solution: def kthDistinct(self, arr: List[str], k: int) -> str: kCheck=0 ans='' for i in range(len(arr)): if arr.count(arr[i])==1: kCheck+=1 if kCheck==k: ans=arr[i]; break return ans cs 2022. 3. 21.
leetcode 2057. Smallest Index With Equal Value 1 2 3 4 5 6 7 class Solution: def smallestEqual(self, nums: List[int]) -> int: flag=0 for i in range(len(nums)): if i%10==nums[i] : ans=i; flag=1; break if flag==0 : ans=-1 return ans Colored by Color Scripter cs 2022. 3. 21.
leetcode 2154. Keep Multiplying Found Values by Two 1 2 3 4 5 class Solution: def findFinalValue(self, nums: List[int], original: int) -> int: for i in range(len(nums)): if original in nums: original*=2 return original Colored by Color Scripter cs 2022. 3. 21.
leetcode 1051. Height Checker 1 2 3 4 5 6 7 class Solution: def heightChecker(self, heights: List[int]) -> int: compare=sorted(heights) ans=0 for i in range(len(heights)): if heights[i]!= compare[i]: ans+=1 return ans Colored by Color Scripter cs 2022. 3. 21.
leetcode 1351. Count Negative Numbers in a Sorted Matrix 1 2 3 4 5 6 7 class Solution: def countNegatives(self, grid: List[List[int]]) -> int: cnt=0 for i in grid: for j in i: if j 2022. 3. 20.