코딩테스트 준비/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