current position:Home>[leetcode question brushing Python] 35. Search the insertion position
[leetcode question brushing Python] 35. Search the insertion position
2022-07-24 22:06:02【Better Bench】
1 subject
Given a sort array and a target value , Find the target value in the array , And return its index . If the target value does not exist in the array , Return to where it will be inserted in sequence .
Please use a time complexity of O(log n) The algorithm of .
Example 1:
Input : nums = [1,3,5,6], target = 5
Output : 2
Example 2:
Input : nums = [1,3,5,6], target = 2
Output : 1
Example 3:
Input : nums = [1,3,5,6], target = 7
Output : 4
2 analysis
Insert with binary search , Pay attention to boundary judgment , Insertion position , If you insert target Than a Big , Then insert in a Behind . At this time, take an example to draw , You can understand .
such as nums = [1,3,5,6], target=4 and target=2
3 Python Realization
def searchInsert(self, nums: List[int], target: int) -> int:
l, r = 0, len(nums)-1
result = len(nums)
while l <= r:
mid = int((l+r)/2)
if target<=nums[mid]:
r = mid-1
result = mid
else:
l = mid+1
return result
copyright notice
author[Better Bench],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/205/202207242124210485.html
The sidebar is recommended
- What, three lines of Python code can get massive data?
- Debug Python code and stop printing!
- 25 great lines of Python code, recommended collection!
- Recommended collection, 22 Python Mini projects (with source code)
- Cyberpunk is so popular. How cool can it be combined with Python?
- 2021 is coming. Exchange Python for a avatar until the New Year!
- "Python practical secret 09" better function operation cache
- About the indentation error caused by sublime text writing Python: unindent does not match any outer indentation level error
- Functions of Python
- Advanced order and closure of Python function
guess what you like
What are the formulas and characteristics of Jaccard similarity calculation in Python data mining?
How to use Jaccard similarity to calculate in Python data mining?
In Python data mining, what does ontology mean in text similarity calculation?
In Python data mining, what are the other methods of text similarity calculation?
Python multi label classification reference
What is the idea of simhash algorithm in Python data mining?
What are the steps of simhash algorithm in Python data mining?
How to use simhash algorithm in Python data mining?
Python crawler other
Import class of Python
Random recommended
- When Python translates, the return value is empty, but the result can be printed
- Basic operations of Python files
- Python absolute path and relative path explanation
- Python comments (multi line comments and single line comments) usage details
- Four methods of python3 list merging
- Seven methods of string splicing in python3
- In Python__ new__ Method explanation and use
- Common methods of Python requests Library
- Install common third-party packages such as numpy, CV2, and Matplotlib when Python is offline
- Python brute force crack zip file decompression password
- PythonStudy2
- Design and implementation of hotel housing management system based on Python
- Design and implementation of job duplication checking system based on Python
- Design and implementation of Python based third class hospital website
- Python returns an error in the dictionary. How to solve it?
- How does Python monitor the keyboard?
- How to install pandas into visual studios code?
- WxPython static text, drop-down box, button conflict with controller
- Problems of dictionary classes in Python
- Python modulenotfounderror: no module named popular explanation and method
- Python web page parsing library: crawl web pages with requests HTML
- Python function default parameter pit avoidance Guide
- Django Foundation
- Install Python
- Python crawler engineer here, do you dare to climb the law firm site?
- Advanced tutorial exploring Python code object
- Basic use of selenium in python (1)
- How to set the trial period for Python scripts
- Python data analysis (4)
- Using python+mongodb to realize stock grid Trading Code
- [Python interesting exercises]
- [find, sort ~python]
- [Python exercises]
- Python quantitative investment - Evaluation and visualization of portfolio (Part 1): calculate indicators such as yield, volatility, maximum pullback, alpha alpha, beta beta, sharp ratio [source code + detailed explanation]
- Pickledb a lightweight Python database
- [python] post processing after capturing ctrl+c events
- In Python data mining, what is the algorithm idea of short text similarity based on word vector?
- In Python data mining, what are the algorithm steps of short text similarity based on word vector?
- In Python data mining, how to calculate the similarity based on word vector?
- Advanced application of structured data analysis tool pandas