Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to find numbers in a sorted array in LeetCode

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report