[LeetCode]题解(python):035-Search Insert Position



题目来源

https://leetcode.com/problems/search-insert-position/

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You may assume no duplicates in the array.



题意分析



Input: a sorted list of int

Output: the index if the target is found. If not, return the index where it would be if it were inserted in order.

Conditions:找到target所在的位置或者应该插入的位置

Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0



题目思路



使用二分法找元素,如果找到直接返回index,如果不能找到,此时又first和last两个值,注意到target总是不小于nums[first],所以比较target和first即可,

  1. 如果target大于nums[first],返回first + 1
  2. 反之返回first

注意到在二分法当中first可能会加1溢出数组,所以在上面步骤当中要在比较前加入判断first是否溢出数组



AC代码(Python)


 1 _author_ = "YE"
 2 # -*- coding:utf-8 -*-
 3
 4 class Solution(object):
 5     def searchInsert(self, nums, target):
 6         """
 7         :type nums: List[int]
 8         :type target: int
 9         :rtype: int
10         """
11         len1 = len(nums)
12         first = 0
13         last = len1
14         while first < last:
15             mid = (first + last) // 2
16             if nums[mid] == target:
17                 return mid
18             elif nums[mid] < target:
19                 first = mid + 1
20             else:
21                 last = mid - 1
22         # print(first,last)
23         if first < len1 and nums[first] < target:
24             return first + 1
25         return first
26
27 s = Solution()
28 nums = [1,3]
29 target = 2
30 print(s.searchInsert(nums, target))
时间: 2024-08-12 22:47:16

[LeetCode]题解(python):035-Search Insert Position的相关文章

[LeetCode] 035. Search Insert Position (Medium) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Search Insert Position (Medium) 链接: 题目:https://leetcode.com/problems/search-insert-position/ 代码(github):https://github.com/illuz/leetcode 题意: 要把一个数有序插入到一

LeetCode 035 Search Insert Position

题目要求:Search Insert Position Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1

Leetcode 题目整理 Sqrt &amp;&amp; Search Insert Position

Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt(7)这种情况,思路一就是应用二分法进行查找.每次给出中间值,然后比对cur的平方与目标值的大小.需要先设定两个变量用来存放左右游标. 这里要考虑一下整型溢出的问题,另外,即使不能开出整数的也要近似给出整数部分,不能忽略. 代码如下: int Solution::mySqrt(int x) { //

LeetCode练题——35. Search Insert Position

1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in th

[Leetcode][Python]35: Search Insert Position

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 35: Search Insert Positionhttps://oj.leetcode.com/problems/search-insert-position/ Given a sorted array and a target value, return the index if the target is found.If not, return the index whe

Java for LeetCode 035 Search Insert Position

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples. [1,3,5,6], 5 → 2 [1,3,5,6], 2

leetcode第34题--Search Insert Position

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 →

035. Search Insert Position

1 class Solution { 2 public: 3 int searchInsert(vector<int>& nums, int target) { 4 int left = 0, right = nums.size() - 1; 5 int mid = 0; 6 while (left <= right) { 7 mid = left + (right - left) / 2; 8 if (nums[mid] > target) right = mid - 1

LeetCode(35) Search Insert Position

题目 Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples. [1,3,5,6], 5 → 2 [1,3,5,6]

Leetcode35 Search Insert Position 解题思路(python)

本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第35题,这道题的tag是数组,python里面叫list,需要用到二分搜索法 35. Search Insert Position Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were in