LeetCode034 Search for a Range

    xiaoxiao2021-03-25  81

    详细见:leetcode.com/problems/search-for-a-range

    Java Solution: github

    package leetcode; public class P034_SearchForARange { public static void main(String[] args) { new Solution().searchRange(new int[] {4, 8, 15, 16, 17, 18}, 8); } /* * 1 ms * 7.00% */ static class Solution { public int[] searchRange(int[] nums, int target) { boolean isHas = false; for (int i = 0; !isHas && i != nums.length; i ++) isHas |= nums[i] == target; if (isHas) return new int[] {getIndexFirst(nums, 0, nums.length - 1, target), getIndexLast(nums, 0, nums.length - 1, target)}; else return new int[] {-1, -1}; } /* * 第一个 */ private int getIndexFirst(int[] nums, int sti, int eni, int target) { while (sti < eni) { int mid = (sti + eni) >> 1; if (nums[mid] >= target) eni = mid; else sti = mid + 1; } return sti; } private int getIndexLast(int[] nums, int sti, int eni, int target) { while (sti < eni) { int mid = sti + eni; mid = (mid & 0x1) == 1 ? (mid >> 1) + 1 : mid >> 1; if (nums[mid] <= target) sti = mid; else eni = mid - 1; } return sti; } } }

    C Solution: github

    /* url: leetcode.com/problems/search-for-a-range/ 6ms 26.67% */ #include <stdio.h> #include <stdlib.h> //[i, j) int binary_search_first_equal_or_larger(int* n, int i, int j, int t) { int m = 0; j --; while (i < j) { m = i + (j - i) / 2; if (n[m] >= t) { j = m; } else { i = m + 1; } } return i; } int* searchRange(int* nums, int numsSize, int target, int* returnSize) { int* a = (int*) malloc(sizeof(int) * 2); a[0] = -1; a[1] = -1; * returnSize = 2; if (nums[0] == target) a[0] = 0; else if (nums[0] > target) return a; if (nums[numsSize - 1] == target) a[1] = numsSize - 1; else if (nums[numsSize - 1] < target) return a; if (a[0] == -1) a[0] = binary_search_first_equal_or_larger(nums, 0, numsSize, target); if (a[1] == -1) a[1] = binary_search_first_equal_or_larger(nums, 0, numsSize, target + 1) - 1; //target not exists if (a[0] > a[1]) { a[0] = -1; a[1] = -1; } return a; } int main() { int nums[] = {6, 6, 6, 8, 8, 10}; int numsSize = 6; int target = 7; int returnSize = 0; int * a = searchRange(nums, numsSize, target, &returnSize); printf("answer is %d %d \r\n", a[0], a[1]); free(a); return 0; }

    Python Solution: github

    #coding=utf-8 ''' url: leetcode.com/problems/search-for-a-range/ @author: zxwtry @email: zxwtry@qq.com @date: 2017年4月4日 @details: Solution: 46ms 74.11% ''' class Solution(object): #[i, j) def binarySearchFirstEqualOrLarger(self, n, i, j, t): j -= 1 if n[j] < t: return j + 1 while i < j: m = (i + j) // 2 if n[m] >= t: j = m else: i = m + 1 return i def searchRange(self, n, t): """ :type n: List[int] :type t: int :rtype: List[int] """ nn = 0 if n == None else len(n) if nn == 0: return [-1, -1] v1 = self.binarySearchFirstEqualOrLarger(n, 0, nn, t) v2 = self.binarySearchFirstEqualOrLarger(n, 0, nn, t+1) v2 -= 1 if v1 < 0 or v1 >= nn or n[v1] != t or n[v2] != t: return [-1, -1] return [v1, v2] if __name__ == "__main__": n = [1, 1] t = 1 print(Solution().searchRange(n, t))

    转载请注明原文地址: https://ju.6miu.com/read-21050.html

    最新回复(0)