본문 바로가기

LeetCode342

leetcode 796. Rotate String 1 2 3 4 5 6 7 8 class Solution: def rotateString(self, s: str, goal: str) -> bool: ans=False for i in range(len(s)): s=s[1:]+s[0] if s == goal : ans = True; break return ans Colored by Color Scripter cs 2022. 4. 18.
leetcode 202. Happy Number 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 pyuthclass Solution: def isHappy(self, n: int) -> bool: ans=False number_list=[n] while 1: tmp_n=0 for i in str(n): tmp_n+=int(i)**2 if tmp_n== 1 : ans=True break if tmp_n in number_list : break n=tmp_n number_list.append(n) return ans cs 크게 어렵지 않은 문제였는데 Line 8에서 += 로 해야할것을 =+로 써놓고 왜 틀리지 하고 계속 봤네요 ^^; 2022. 4. 18.
leetcode 1903. Largest Odd Number in String 1 2 3 4 5 class Solution: def largestOddNumber(self, num: str) -> str: for i in range(len(num)): if int(num[-1])%2 == 0 : num=num[:-1] return(num) cs 2022. 4. 14.
leetcode 387. First Unique Character in a String 1 2 3 4 5 6 class Solution: def firstUniqChar(self, s: str) -> int: answer=-1 for i in s: if s.count(i)==1 : answer=s.index(i); break return answer Colored by Color Scripter cs 1 2 3 4 5 6 7 8 9 class Solution: def firstUniqChar(self, s: str) -> int: unique_s=[] for i in s : if i not in unique_s: unique_s.append(i) answer=-1 for i in unique_s: if s.count(i)==1 : answer=s.index(i); break return a.. 2022. 4. 14.
leetcode 1317. Convert Integer to the Sum of Two No-Zero Integers 1 2 3 4 5 6 class Solution: def getNoZeroIntegers(self, n: int) -> List[int]: for i in range(1, n): if not '0' in str(i) and not '0' in str(n-i) : answer=[i,n-i]; break return(answer) Colored by Color Scripter cs 2022. 4. 14.
leetcode 2235. Add Two Integers 1 2 3 class Solution: def sum(self, num1: int, num2: int) -> int: return num1+num2 cs 2022. 4. 12.
leetcode 682. Baseball Game 1 2 3 4 5 6 7 8 9 10 class Solution: def calPoints(self, ops: List[str]) -> int: record=[] for i in ops : if i.isnumeric() : record.append(int(i)) if i[0]=='-' : record.append(int(i[1:])*-1) if i == 'C': record.pop() if i == 'D': record.append(record[-1]*2) if i == '+': record.append(record[-1]+record[-2]) return (sum(record)) Colored by Color Scripter cs 2022. 4. 11.
leetcode 1507. Reformat Date 1 2 3 4 5 6 class Solution: def reformatDate(self, date: str) -> str: month=["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] date=date.split() ans=date[2]+'-'+'%.2d'%(month.index(date[1])+1)+'-'+'%.2d'%(int(date[0][:-2])) return(ans) Colored by Color Scripter cs 2022. 4. 8.
leetcode 169. Majority Element 1 2 3 4 5 class Solution: def majorityElement(self, nums: List[int]) -> int: nums_set=list(set(nums)) for i in nums_set: if nums.count(i)>=len(nums)/2 : return(i); break cs 2022. 4. 8.
leetcode 258. Add Digits 1 2 3 4 5 6 7 8 class Solution: def addDigits(self, num: int) -> int: while num>=10: tmp = 0 for i in (str(num)): tmp+=int(i) num=tmp return (num) cs 2022. 4. 8.
leetcode 1550. Three Consecutive Odds 1 2 3 4 5 6 7 8 class Solution: def threeConsecutiveOdds(self, arr: List[int]) -> bool: ans=False for i in range(2,len(arr)): if arr[i]%2 == 1 and arr[i-1]%2 == 1 and arr[i-2]%2 == 1 : ans = True break return ans Colored by Color Scripter cs 2022. 4. 4.
leetcode 1800. Maximum Ascending Subarray Sum 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Solution: def maxAscendingSum(self, nums: List[int]) -> int: ans=[] tmp=0 if len(nums)== 1: ans.append(nums[0]) else : for i in range(len(nums)-1): if nums[i] 2022. 4. 4.