Index2 [LeetCode] Search Insert Position (index) 48ms class Solution: def searchInsert(self, nums: List[int], target: int) -> int: ans = nums+[target] ans.sort() return ans.index(target) target 의 index를 for loop으로 찾기 보다는 ans에 추가하고 sort한 뒤 index로 찾기 40ms: append로 넣었을때 class Solution: def searchInsert(self, nums: List[int], target: int) -> int: nums.append(target) nums.sort() return nums.index(target) 2021. 3. 30. [LeetCode] Implement strStr() 파이썬 (exhaustive search, find, index string) 48ms 걸리는 내 솔루션 class Solution: def strStr(self, haystack: str, needle: str) -> int: m = len(needle) if len(needle) == 0: return 0 for i in range(len(haystack)): if haystack[i] == needle[0] and haystack[i:i+m] == needle: return i else: return -1 20ms 걸리는 솔루션 class Solution: def strStr(self, haystack: str, needle: str) -> int: m, n = len(haystack), len(needle) for i in range(m - n + 1): if haystac.. 2021. 3. 29. 이전 1 다음