【leetcode 简单】第十一题 搜索插入位置
生活随笔
收集整理的這篇文章主要介紹了
【leetcode 简单】第十一题 搜索插入位置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
給定一個排序數組和一個目標值,在數組中找到目標值,并返回其索引。如果目標值不存在于數組中,返回它將會被按順序插入的位置。
你可以假設數組中無重復元素。
示例 1:
輸入: [1,3,5,6], 5 輸出: 2示例?2:
輸入: [1,3,5,6], 2 輸出: 1示例 3:
輸入: [1,3,5,6], 7 輸出: 4示例 4:
輸入: [1,3,5,6], 0 輸出: 0 class Solution:def searchInsert(self, nums, target):""":type nums: List[int]:type target: int:rtype: int"""index=-1last=0try:return nums.index(target)except:for i in range(len(nums)):if target > nums[i]:last+=1nums.insert(last,target)return index if index > 1 else last?
轉載于:https://www.cnblogs.com/flashBoxer/p/9440524.html
總結
以上是生活随笔為你收集整理的【leetcode 简单】第十一题 搜索插入位置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Github使用初体验2018.08.0
- 下一篇: spark源码编译记录