Leetcode-34-Search Insert Position-(Medium)
二分法查找需要插入的位置,需要注意两点
1、如果元素不存在,停止的时候start Index刚好是需要插入的位置
2、如果元素存在,需要向前追溯找到非目标元素的起始边界
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
__author__ = 'qqvipfunction' class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
""" return self.binary_search(nums, 0, len(nums) - 1, target) #二分查找算法
def binary_search(self, nums, start, end, targrt):
if start > end:
print(start, end)
return start if start > 0 else 0
mid = start + (end - start)//2
if nums[mid] > targrt:
return self.binary_search(nums, start, mid - 1, targrt)
elif nums[mid] < targrt:
return self.binary_search(nums, mid + 1, end, targrt)
else:
index = mid
for i in range(mid - 1, -1, -1):
if nums[i] == targrt:
index = i;
else:
break
return index if __name__ == '__main__':
s = Solution()
print(s.searchInsert([1], 0))
Leetcode-34-Search Insert Position-(Medium)的更多相关文章
- [LeetCode] 035. Search Insert Position (Medium) (C++)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- 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 ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- LeetCode 035 Search Insert Position
题目要求:Search Insert Position Given a sorted array and a target value, return the index if the target ...
- [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 ...
- [leetcode 35] Search Insert Position
1 题目: Given a sorted array and a target value, return the index if the target is found. If not, retu ...
- Leetcode 35 Search Insert Position 二分查找(二分下标)
基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...
- 【题解】【数组】【查找】【Leetcode】Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [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 ...
- 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 ...
随机推荐
- iOS后向兼容:如何发现过期接口
以4.3以下兼容性为例,在项目预编译头文件(xx.pch)中加入如下代码: #import <Availability.h> #define __AVAILABILITY_INTERNAL ...
- Java中的嵌套类和内部类
以前看<Java编程思想>的时候,看到过嵌套类跟内部类的区别,不过后来就把它们的概念给忘了吧.昨天在看<数据结构与算法分析(Java语言版)>的时候,又遇到了这个概念,当时就很 ...
- oracle琐碎笔记
Oracle知识点 ps:由于是自己看的所以笔记比较乱,大家谅解 Commit rollback Sql核心语句之select Selct中要用到以下语句 From语句 Where语句 Group b ...
- 何为分类,UIImageView举例
终于开始自己这个“聚水成洋”的路程了. 经过近一年的iOS学习和开发,遇到很多困难,一开始的陌生和畏惧,中途的困惑和纠结,解决问题后的豁然开朗和总结提升,自己就在这样一个不断的循环中逐渐成长起来了. ...
- 从异步更新进度想起的事儿——IProgress
今天,在群里向大家请教了这样一个问题:“两个对象(类.窗体或什么)之间,要完成比较频繁的报告进度更新都有哪些好的方式”,Somebody 跳出来给出了个“IProgress”,没了解过,后面围绕着它讨 ...
- SPI and API
目录 背景从面向接口编程说起“接口”位于“调用方”所在的“包”中“接口”位于“实现方”所在的“包”中“接口”位于独立的“包”中需要注意的事项另外一张图备注 背景返回目录 第一次听说 SPI 是阅读&l ...
- 竞价广告系统-ZooKeeper介绍
ZooKeeper介绍 为了讲述的完整性,介绍一下ZooKeeper.ZooKeeper在Index和Ad Server里使用比较多,虽然它可能没有google的Chubby好,但它是开源的工具.举一 ...
- Lucene.net入门学习系列(2)
Lucene.net入门学习系列(2) Lucene.net入门学习系列(1)-分词 Lucene.net入门学习系列(2)-创建索引 Lucene.net入门学习系列(3)-全文检索 在使用Luce ...
- VC 编程ANSI环境下读写Unicode文件
没有注意到文件编码的不同会产生这么多的问题,在动手以前查询了很多资料,在本博客中收藏了不少先辈的成果,在这里一并表示致敬! 关于ANSI和Unicode编码的原理在这里也不说了,主要讲下如 ...
- jQuery.fn.attr与jQuery.fn.prop
jQuery.fn.attr与jQuery.fn.prop jQuery.fn.attr.jQuery.fn.prop的区别 假设页面有下面这么个标签,$('#ddd').attr('nick').$ ...