[leetcode-1] Two Sum
ยท
๐Ÿข One step
LeetCode-1 Two Sum : target๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ฆฌ์ŠคํŠธ ๋‘๊ฐœ์˜ ํ•ฉ note : OMyAnswer (O(n * n))class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: len_nums = len(nums) if len_nums==2: return [0,1] for i in range(len(nums)): for j in range(i+1,len(nums)): if target==(nums[i]+nums[j]): return [i, j] Result : 3649ms Memory: 14.9mbAnswer O(n * n) (in์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„ n)class Solution: def twoSum(self, nums:..
[leetcode-5] Longest Palindrome Substring
ยท
๐Ÿข One step
LeetCode-5 Longest Palindrome Substring : ๊ฐ€์žฅ ๊ธด Palindrome note :Myanswerclass Solution: def longestPalindrome(self, s: str) -> str: if len(s)==1: return s[0] for i in range(0,len(s)-1): for j in range(0,i+1): org = s[j:len(s)-(-j+i)] if org[::-1]==org: return org return org[0] Result : 6515ms Memory: 13.9mbSolutionclass Solution: def longestPalindrome(self, s: str) -> str: def expend(left: int, ..
[leetcode-49] Group Anagrams
ยท
๐Ÿข One step
LeetCode-49 Group Anagrams : ์–ธ์–ด์œ ํฌ ex) cat -> act cat note : hint: collections.defaultdict(list)class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: anagrams = defaultdict(list) for word in strs: anagrams[''.join(sorted(word))].append(word) return list(anagrams.values()) Result : 98ms Memory: 17.2mbdefault_factory: index[x] ์—์„œ x๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์„ ๋•Œ ValueError๋•Œ ๋ฐœ์ƒํ•œ๋‹ค. ๋ชจ๋“  ๊ตฌํ˜„์ด ..
[leetcode-819] Most Common Word
ยท
๐Ÿข One step
LeetCode-819 Most Common Word : ๊ธˆ์ง€๋œ ๋‹จ์–ด๋ฅผ ์ œ์™ธํ•œ ๊ฐ€์žฅ ๋งŽ์€ ๋นˆ๋„์˜ ๋‹จ์–ด ์ถ”์ถœ Info : ๋Œ€์†Œ ๋ฌธ์ž, ๊ตฌ๋‘์  ๋ฌด์‹œclass Solution: def mostCommonWord(self, paragraph: str, banned: List[str]) -> str: return Counter([word for word in re.sub("[^a-z]", ' ',paragraph.lower()).split() if word not in banned]).most_common(1)[0][0] Result : 28 Memory: 13.9mb
[leetcode-937] Reorder_Data_in_LogFile
ยท
๐Ÿข One step
LeetCode-937 Reorder_Data_in_LogFile : reorder log files ์ฒซ๋ฒˆ์งธ ๋ฌธ์ž ์‹๋ณ„์ž ex) "dig2 a","dig1 b"-> "a","b"Myanswerclass Solution: def reorderLogFiles(self, logs: List[str]) -> List[str]: let = [] digit = [] for i in logs: if i.split()[1].isdigit()==False: let.append(i) else: digit.append(i) let.sort(key = lambda x: (x.split()[1:], x.split()[0])) #digit.sort(key = lambda x: x.split()[1:],reverse=True) r..
leetCode python code markdown ๋ณ€ํ™˜๊ธฐ
ยท
๐Ÿ Python
vscode editor๋กœ ์ฝ”๋”ฉ์„ ๋งŽ์ด ํ•˜๋‹ค๋ณด๋‹ˆ ํฌ์ŠคํŒ…ํ•  ๋•Œ ์ฝ”๋“œ ๋ธ”๋Ÿญ์„ ๋งŒ๋“ค์–ด์ฃผ๋Š” ์ผ์ด ๋„ˆ๋ฌด ๊ท€์ฐฎ์•„์„œ ๋งˆํฌ๋‹ค์šด ์ž๋™๋ณ€ํ™˜๊ณผ Tstory์— ๋ฐ”๋กœ ํฌ์ŠคํŒ…์ด ๋˜๋„๋ก ๊ฐœ๋ฐœํ•˜๊ณ  ์žˆ๋‹ค. using python-markdown for posting Tstory python2Markdown.py [span code block & markdown] LeetCode-0 subject : info sub {code} Result : 0 Memory: 0mb
[leetcode-125] Palindrome + Slicing
ยท
๐Ÿข One step
using Slicing leetcode-125 + ์ •๊ทœ์‹ ์‚ฌ์šฉimport re import collections class Solution: def isPalindrome(self, s: str) -> bool: s = s.lower() s = re.sub('[^a-z0-9]','',s) return s == s[::-1] # reverse Result : 46ms Memory: 15.7mb
[leetcode-125] Palindrome
ยท
๐Ÿข One step
LeetCode 125 Valid Palindrome Palindrome : ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ ์—†์ด ์ขŒ์šฐ๊ฐ€ ๋Œ€์นญ์ธ ๋ฌธ์ž์—ด 1. isalnum : ๋ฌธ์ž ํŒ๋ณ„ ํ›„ ์†Œ๋ฌธ์ž ๋ณ€ํ™˜ test = 'is a car ; rac a si' c_string = [] for s in test: if s.isalnum(): # ๋ฌธ์ž์ธ์ง€ ํŒ๋ณ„ c_string.append(s.lower()) # ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ # c_string # ['i', 's', 'a', 'c', 'a', 'r', 'r', 'a', 'c', 'a', 's', 'i'] 2. loop๋กœ ์ฒซ๋ฒˆ์งธ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž์—ด ๋น„๊ต ํ›„ pop while len(c_string) > 1: if c_string.pop(0) != c_string.pop(): break #c_string ..
๋‹คํ–ˆ๋‹ค
'Leetcode' ํƒœ๊ทธ์˜ ๊ธ€ ๋ชฉ๋ก (4 Page)