| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 315. Count of Smaller Numbers After Self
- Decorator
- shiba
- 715. Range Module
- 30. Substring with Concatenation of All Words
- 밴픽
- Convert Sorted List to Binary Search Tree
- Python
- DWG
- Protocol
- Python Implementation
- 파이썬
- Python Code
- 프로그래머스
- Generator
- attribute
- 109. Convert Sorted List to Binary Search Tree
- 컴퓨터의 구조
- concurrency
- Regular Expression
- 43. Multiply Strings
- iterator
- data science
- Substring with Concatenation of All Words
- 운영체제
- kaggle
- t1
- Class
- LeetCode
- 시바견
Archives
- Today
- Total
Scribbling
[C++] lower_bound, upper_bound 본문
LeetCode 34. Find First and Last Position of Element in Sorted Array
https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
Find First and Last Position of Element in Sorted Array - LeetCode
Can you solve this real interview question? Find First and Last Position of Element in Sorted Array - Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value. If target is not found in t
leetcode.com
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
if (nums.size() == 0) {
return {-1, -1};
}
int i = lower_bound(nums.begin(), nums.end(), target) - nums.begin();
int j = upper_bound(nums.begin(), nums.end(), target) - 1 - nums.begin();
if (i < nums.size() && nums[i] == target) {
return {i, j};
}
return {-1, -1};
}
};
'Computer Science > C++' 카테고리의 다른 글
| [C++] Chapter 2: Types (0) | 2023.08.21 |
|---|---|
| [C++] Load Balancing (0) | 2023.08.19 |
| [C++] isalnum, tolower (0) | 2023.08.18 |
| [C++] LeetCode 128. Longest Consecutive Sequence (0) | 2023.08.18 |
| [C++] LeetCode 238. Product of Array Except Self (0) | 2023.08.18 |