Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to find numbers in a sorted array in LeetCode

Shulou Source: shulou.com Published: 2022-06-01 13:06:33 10月03日 Update

Editor to share with you how to find numbers in the sorted array in LeetCode. I hope you will get something after reading this article. Let's discuss it together.

Topic description

Count the number of times a number appears in the sorted array.

0 1

If nums [m] = = target:

If isleft:

# currently looking for the left boundary, updating the result to a smaller subscript equal to target, while continuing to search to the left

Res = m if res is None else min (res, m)

E = m-1

Else:

# currently looking for the right boundary, update the result to a larger subscript equal to target, while continuing to search to the right

Res = m if res is None else max (res, m)

S = m + 1

Elif nums [m] < target:

S = m + 1

Else:

E = m-1

Return res

Left = binarySearch (True)

If left is None:

# if the left boundary does not exist, the whole array does not have target, and 0 is returned directly

Return 0

Right = binarySearch (False)

# the end result is the right boundary-left boundary + 1

Return right-left + 1 after reading this article, I believe you have some understanding of "how to find numbers in a sorted array in LeetCode". If you want to know more about it, please follow the industry information channel. Thank you for reading!

Tags: Arrays numbers right complexity complexity times sorting results code topics subscript ideas time space statistics and at the same time conditions tags articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info MySQL Microsoft Redmi Docker