LeetCode

LeetCode

  • Problems
  • GitHub

›Problems

Problems

  • Two Sum
  • Add Two Numbers
  • Longest Substring Without Repeating Characters
  • Median of Two Sorted Arrays
  • Longest Palindromic Substring
  • ZigZag Conversion
  • Reverse Integer
  • String to Integer (atoi)
  • Palindrome Number
  • Regular Expression Matching
  • Container With Most Water
  • Integer to Roman
  • Roman to Integer
  • Longest Common Prefix
  • 3Sum
  • 3Sum Closest
  • Letter Combinations of a Phone Number
  • 4Sum
  • Remove Nth Node From End of List
  • Valid Parentheses
  • Merge Two Sorted Lists
  • Generate Parentheses
  • Merge k Sorted Lists
  • Swap Nodes in Pairs
  • Reverse Nodes in k-Group
  • Remove Duplicates from Sorted Array
  • Remove Element
  • Implement strStr()
  • Divide Two Integers
  • Substring with Concatenation of All Words
  • Next Permutation
  • Longest Valid Parentheses
  • Search in Rotated Sorted Array
  • Find First and Last Position of Element in Sorted Array
  • Search Insert Position
  • Valid Sudoku
  • Sudoku Solver
  • Count and Say
  • Combination Sum
  • Combination Sum II
  • First Missing Positive
  • Trapping Rain Water
  • Multiply Strings
  • Wildcard Matching
  • Jump Game II
  • Permutations
  • Permutations II
  • Rotate Image
  • Group Anagrams
  • Pow(x, n)
  • Binary Tree Inorder Traversal
  • Triangle
  • Number of Islands
  • Random Pick Index
  • Coin Change 2
  • Maximum Length of Pair Chain
  • Repeated String Match
  • Minimum ASCII Delete Sum for Two Strings
  • Remove Comments
  • Split Linked List in Parts
  • Design HashSet
  • RLE Iterator
  • Number of Recent Calls
  • Rotting Oranges
  • Minimum Number of K Consecutive Bit Flips
  • Remove All Adjacent Duplicates In String
  • Unique Number of Occurrences
  • Count Servers that Communicate
  • Subtract the Product and Sum of Digits of an Integer
  • Find the Smallest Divisor Given a Threshold
  • Find N Unique Integers Sum up to Zero
  • Minimum Flips to Make a OR b Equal to c
  • Combine Two Tables
  • Second Highest Salary
  • Nth Highest Salary
  • Rank Scores
  • Consecutive Numbers
  • Employees Earning More Than Their Managers
  • Duplicate Emails
  • Customers Who Never Order
  • Department Highest Salary
  • Department Top Three Salaries
  • Delete Duplicate Emails
  • Rising Temperature
  • Trips and Users
  • Big Countries
  • Classes More Than 5 Students
  • Valid Phone Numbers
  • Tenth Line
  • Print in Order
  • Maximum Subarray

Find First and Last Position of Element in Sorted Array

Description

Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.

Your algorithm's runtime complexity must be in the order of O(log n).

If the target is not found in the array, return [-1, -1].

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]

Solution(javascript)

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var searchRange = function(nums, target) {
    let firstleft = 0, lastleft = 0;
    let firstright = nums.length - 1, lastright = nums.length - 1;
    let first = -1, last = -1;
    let mid;
    
    while (firstleft <= firstright) {
        mid = Math.floor((firstleft + firstright) / 2);

        if (nums[mid] === target && ( nums[mid-1] < nums[mid] || mid === 0)) {
            first = mid;
            break;
        } else if (nums[mid] < target) {
            firstleft = mid + 1;
        } else {
            firstright = mid - 1;
        }
    }
    
    while (lastleft <= lastright) {
        mid = Math.floor((lastleft + lastright) / 2);

        if (nums[mid] === target && ( nums[mid+1] > nums[mid] || mid === nums.length-1)) {
            last = mid;
            break;
        } else if (nums[mid] > target) {
            lastright = mid - 1;
        } else {
            lastleft = mid + 1;
        }
    }
    
    return [first, last];
};
← Search in Rotated Sorted ArraySearch Insert Position →
  • Description
  • Solution(javascript)
Powered By LeetCode Site Generator