[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导致的溢 ...
随机推荐
- Scala基础入门-2
简单类和无参方法 class Counter { private var value = 0 // 必须初始化字段 def increment() { value += 1 } // 方法默认公有 d ...
- RTC定时开机闹钟
RTC是Real Time Clock的简称,它在硬件电路上单独供电,当系统关机时,CPU和其他外部硬件设备全部掉电,但是RTC仍然继续工作. HWCR (Hibernate Wakeup Contr ...
- ubuntu下lnmp的安装
适用于ubuntu14.04和源是14.04的ubuntu上安装nginx(我学在线Moodle工作室---注这里安装的是最新版的nginx,并且解决pathinfo问题,特别适用于Moodle安装) ...
- python学习day2(一)
一.上周作业回顾 1.登陆接口: 思路流程: 1.登陆,三次锁定用户 2.用户信息文件,黑名单文件 3.检测黑名单,如输入账号在黑名单中存在,不允许登陆 4.用户密码判断 主要知识点:while,fo ...
- eclipse中调出android sdk manager和android virtual device manager图标
有时候在安装ADT插件后,eclipse菜单栏上不会显示android sdk manager和android virtual device manager两个图标, 这个时候,如果安装ADT插件的步 ...
- 【自学php】第二天 - php快速入门
打算看<php和mysql web开发>来学习php,所以也算是这本书的学习笔记吧,也按照书里的例子来练习,但是也有些取舍.第一章是一个订单表单的例子,php用于处理提交的表单. 1.先创 ...
- DELPHI 任务栏无EXE显示
需要用到的一个函数: LONG SetWindowLong( HWND hWnd, int nIndex, LONG dwNewLong ); program Project; usesForms, ...
- 【LeetCode】4Sum 解题报告
[题目] Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...
- 修改SharePoint平台登录者显示名称
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { //提升权限,拿系统服务帐号来执行此段代码 ...
- 在WPF中将某张表中的数据显示到datagrid
a.在.xaml文件中拖入一个datagrid,然后添加列名,使用Binding="{Binding 数据库中的 列名称}",如下: <DataGrid AutoGenera ...