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

Python dichotomy to find out how to use it

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use python dichotomy search". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian and go deep into it slowly to study and learn "how to use python dichotomy search" together!

For more elements to search, binary search speed is faster than simple search This is the advantage of binary search algorithm, but binary algorithm also has disadvantages, binary algorithm only for ordered lists, so insertion and deletion will be very difficult, therefore, binary search method is only suitable for ordered lists that do not change often

Binary search has a very important feature, that is, it does not find all the elements of the sequence, and the amount of data found actually coincides with the logarithm of the elements. Under normal circumstances, the elements found each time are reduced by half. The time complexity of a binary search is O(log2n). At best, of course, it's a one-time search, but at worst and in general it's a lot better than a sequential search.

Given an ordered (ascending) array of n integers numbers and a target value target, write a function to search for target in numbers, returning subscripts if the target value exists, otherwise returning-1.

class Solution: def search(self,nums:List[int],target:int)->int: left=0 right=len(nums)-1 while(left

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

Development

Wechat

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

12
Report