[Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 35: Search Insert Position
https://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 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 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0 ===Comments by Dabay===
二分查找。
'''
class Solution:
# @param A, a list of integers
# @param target, an integer to be inserted
# @return integer
def searchInsert(self, A, target):
left, right = 0, len(A)-1
while left < right:
m = (left + right) / 2
if A[m] == target:
return m
elif A[m] < target:
left = m + 1
else:
right = m - 1
else:
return [left, left + 1][target > A[left]] def main():
sol = Solution()
nums = [1,3,5,6]
target = 5
print sol.searchInsert(nums, target) if __name__ == '__main__':
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)
[Leetcode][Python]35: Search Insert Position的更多相关文章
- 【LeetCode】35. Search Insert Position (2 solutions)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- 【LeetCode】35. Search Insert Position 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...
- 【一天一道LeetCode】#35. Search Insert Position
一天一道LeetCode系列 (一)题目 Given a sorted array and a target value, return the index if the target is foun ...
- LeetCode:35. Search Insert Position(Easy)
1. 原题链接 https://leetcode.com/problems/search-insert-position/description/ 2. 题目要求 给定一个已经排好序的数组和一个目标值 ...
- Leetcode No.35 Search Insert Position(c++实现)
1. 题目 1.1 英文题目 Given a sorted array of distinct integers and a target value, return the index if the ...
- LeetCode OJ 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 Problem 35:Search Insert Position
描述:Given a sorted array and a target value, return the index if the target is found. If not, return ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version
704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...
随机推荐
- JDK安装与环境变量配置(Win7)
1.下载JDK(Java SE Development Kit) 地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-dow ...
- VS2008 自定义向导的default.js设置(DLL文件)
function OnFinish(selProj, selObj) { try { var strProjectPath = wizard.FindSymbol('PROJECT_PATH'); v ...
- C++ Primer第四版 15.9 再谈文本查询 程序实现
编程过程中发现书本中的示例程序并不完全,某些地方存在错误,现已改正并添加少许注释.. 1 #include<iostream> 2 #include<fstream> #inc ...
- React 同构
React 同构 搬运 https://segmentfault.com/a/1190000004671209 究竟什么是同构呢? 同构就是希望前端 后端都使用同一套逻辑 同一套代码 Nodejs出现 ...
- sudo apt-get update
要用apt-get这种方式安装LAMP时,最好先运行下面在命令升级自己的系统这样是为了更新源,而如果你找的源不好,可能安装LMAP失败.#sudo apt-get update 获得最近的软件包的列表 ...
- 5款免费Windows远程连接Linux桌面软件(VNC客户端)
不论我们出于何种的用途目的,很多朋友有需要用到VNC链接Linux桌面环境,之前老左有分享过VNC Viewer绿色软件,昨天有朋友提出来使用之后登录远程桌面的界面.分辨率等有些色差.流畅程度 ...
- EventLog监控
http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/logs/eventlogs/ http://etuto ...
- C++vptr初始化时间
给出如下代码段: #include <iostream> #include "stdio.h" using namespace std; class A { publi ...
- 转自:Python函数式编程指南(二):函数
2. 从函数开始 2.1. 定义一个函数 如下定义了一个求和函数: 1 2 def add(x, y): return x + y 关于参数和返回值的语法细节可以参考其他文档,这里就略过了. ...
- 几个关于JPEGLIB库的博客
1.http://blog.csdn.net/huxiangyang4/archive/2010/07/12/5728888.aspx 我认为是最好的 2.http://blog.csdn.net/a ...