본문 바로가기

python-algorithm1422

백준 24510 시간복잡도를 배운 도도 1 2 3 4 5 6 mymax=0 for i in range(int(input())): s=input() cnt=s.count('for')+s.count('while') mymax=max(cnt,mymax) print(mymax) cs 2022. 4. 21.
백준 25024 시간과 날짜 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 def sol1(a,b): if (a>=0 and a=0 and b=1 and b=1 and b= 1 and b 2022. 4. 21.
leetcode 345. Reverse Vowels of a String 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class Solution: def reverseVowels(self, s: str) -> str: vowels=['a','e','i','o','u','A','E','I','O','U'] my_vowel=[] start=0 ans='' for i in s: if i in vowels : my_vowel.append(i) my_vowel=my_vowel[::-1] for i in s: if i in vowels : ans+=my_vowel[start] start+=1 else: ans+=i return(ans) Colored by Color Scripter cs 2022. 4. 21.
leetcode 504. Base 7 1 2 3 4 import numpy class Solution: def convertToBase7(self, num: int) -> str: return(numpy.base_repr(num, base=7)) cs numpy 라이브러리에 base 변환해주는 함수가 있네요..! 2022. 4. 18.
leetcode 1752. Check if Array Is Sorted and Rotated 1 2 3 4 5 6 7 8 9 10 11 class Solution: def check(self, nums: List[int]) -> bool: ans = False target=sorted(nums) for i in range(len(nums)): nums.append(nums[0]) nums.pop(0) if nums == target : ans=True break return(ans) cs 2022. 4. 18.
leetcode 1796. Second Largest Digit in a String 1 2 3 4 5 6 7 8 9 class Solution: def secondHighest(self, s: str) -> int: num_list=[] for i in s: if i.isdecimal() : num_list.append(int(i)) num_list=sorted(list(set(num_list)), reverse=True) if len(num_list) 2022. 4. 18.
leetcode 1154. Day of the Year 1 2 3 4 5 6 import datetime class Solution: def dayOfYear(self, date: str) -> int: year,month,days=map(int, date.split('-')) ans=datetime.date(year,month,days).timetuple().tm_yday return ans Colored by Color Scripter cs 2022. 4. 18.
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.