11.leetcode35_search_insert_position

1.题目描述

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.

给出一串有序数字和一个目标数字,找出目标数字在序列中的位置

2.题目分析

与列表中的每个元素一次比大小,找出其所在位置

3.解题思路

 1 class Solution(object):
 2     def searchInsert(self, nums, target):
 3         """
 4         :type nums: List[int]
 5         :type target: int
 6         :rtype: int
 7         """  
 8         for index,item in enumerate(nums):
 9             if target<=nums[0]:
10                 return 0
11             elif nums[index-1]<target<=nums[index]:
12                 return index
13             elif target>nums[len(nums)-1]:
14                 return len(nums)

4.解题收获

自己尝试用了一次enumerate函数,然后,一遍过了(#^.^#)

原文地址:https://www.cnblogs.com/19991201xiao/p/8419469.html