일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 카드뉴스
- 웹접근성
- programmers
- 알고리즘
- leetcode
- html&css
- 제로베이스 프론트엔드 스쿨
- JavaScript
- 디자인
- react-query
- 프로그래머스
- TypeScript
- 정규표현식
- react
- 비트연산자
- wai-aria
Archives
- Today
- Total
記錄
[JS] 34. Find First and Last Position of Element in Sorted Array 본문
FRONTEND STUDY/LeetCode
[JS] 34. Find First and Last Position of Element in Sorted Array
prts 2022. 10. 14. 23:58문제 링크: https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
난이도: Medium
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 the array, return [-1, -1].
You must write an algorithm with O(log n) runtime complexity.
오름차순으로 정렬된 정수 수의 배열이 주어지면, 주어진 목표 값의 시작 위치와 끝 위치를 찾는다.
배열에서 대상을 찾을 수 없으면 [-1, -1]을 반환한다.
O(log n) 런타임 복잡도로 알고리즘을 작성해야 한다.
배열에서 대상을 찾을 수 없으면 [-1, -1]을 반환한다.
O(log n) 런타임 복잡도로 알고리즘을 작성해야 한다.
Example 1:
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
Example 2:
Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]
Example 3:
Input: nums = [], target = 0
Output: [-1,-1]
Constraints:
- 0 <= nums.length <= 105
- -109 <= nums[i] <= 109
- nums is a non-decreasing array.
- -109 <= target <= 109
문제 풀이
연관 주제: 배열, 이진 검색
var searchRange = function(nums, target) {
//오름차순 정렬 배열에서 target value값 가진 index 출력하기
//target이 없을 시 [-1,-1]
//O(log n) 방식
let result=[];
for(i=0;i<nums.length;i++){
if(nums[i] === target) {
result.push(i) ;
}
}
if(result.length > 0) return [result[0],result[result.length-1]];
else return [-1,-1];
};
'FRONTEND STUDY > LeetCode' 카테고리의 다른 글
[JS]2418. Sort the People (0) | 2022.10.19 |
---|---|
[JS]189. Rotate Array (0) | 2022.10.19 |
[JS] 136. Single Number (0) | 2022.10.12 |
[JS] 215. Kth Largest Element in an Array (0) | 2022.10.11 |
[JS] 7. Reverse Integer (0) | 2022.10.08 |
Comments