码迷,mamicode.com
首页 > 其他好文 > 详细

11.leetcode35_search_insert_position

时间:2018-02-05 23:34:04      阅读:307      评论:0      收藏:0      [点我收藏+]

标签:sea   target   etc   思路   描述   where   分析   gpo   for   

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函数,然后,一遍过了(#^.^#)

11.leetcode35_search_insert_position

标签:sea   target   etc   思路   描述   where   分析   gpo   for   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!