| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- DWG
- Python Implementation
- 109. Convert Sorted List to Binary Search Tree
- Python
- 43. Multiply Strings
- 715. Range Module
- attribute
- Regular Expression
- LeetCode
- iterator
- 315. Count of Smaller Numbers After Self
- 컴퓨터의 구조
- Decorator
- Convert Sorted List to Binary Search Tree
- concurrency
- Substring with Concatenation of All Words
- 파이썬
- 30. Substring with Concatenation of All Words
- data science
- Protocol
- t1
- Class
- Python Code
- 시바견
- shiba
- 운영체제
- kaggle
- 밴픽
- Generator
- 프로그래머스
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 |