[LeetCode] Search Insert Position

동스토리 ㅣ 2021. 8. 26. 20:59

반응형

안녕하세요.

 

LeetCode 35번 Search Insert Position 문제풀이 하겠습니다.

 

https://leetcode.com/problems/search-insert-position/

 

Search Insert Position - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

[문제]

 

 

[TestCase]

 

 

[해설]

 

- target이 nums리스트의 인덱스와 같거나 작다면 해당 인덱스 번호

- 아닐 경우 nums리스트의 길이

 

[코드]

 

class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        
        for i in range(len(nums)):
            if target <=nums[i]:
                return i
        
        return(len(nums))

 

 

감사합니다.

반응형

'Development > Algorithm' 카테고리의 다른 글

[LeetCode] Plus One  (0) 2021.08.31
[LeetCode] Length of Last Word  (0) 2021.08.31
[LeetCode] Implement strStr()  (0) 2021.08.26
[LeetCode] Remove Element  (0) 2021.08.24
[LeetCode] Remove Duplicates from Sorted Array  (0) 2021.08.22