코딩테스트 준비/leetcode

35. Search Insert Position

MAKGA 2021. 8. 6. 22:08
320x100

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

 

금요일이 고단해 easy 두 문제로 간다.

 

정렬된 목록과 목표 숫자가 있을 때, 목록에 있다면 index를, 없다면 순서대로 삽입될 index를 리턴한다.

 

class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        for (int i = 0; i < nums.size(); ++i)
        {
            if (nums[i] > target) {
                return i;
            }
            
            if (nums[i] == target) {
                return i;
            }
        }
        
        return nums.size();
    }
};

320x100

'코딩테스트 준비 > leetcode' 카테고리의 다른 글

49. Group Anagrams  (0) 2021.08.07
53. Maximum Subarray  (0) 2021.08.06
16. 3Sum Closest  (0) 2021.08.05
15. 3Sum  (0) 2021.08.04
70. Climbing Stairs  (0) 2021.08.03