[LeetCode]题解(python):035-Search Insert Position
题目来源
https://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.
题意分析
Input: a sorted list of int
Output: the index if the target is found. If not, return the index where it would be if it were inserted in order.
Conditions:找到target所在的位置或者应该插入的位置
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
题目思路
使用二分法找元素,如果找到直接返回index,如果不能找到,此时又first和last两个值,注意到target总是不小于nums[first],所以比较target和first即可,
- 如果target大于nums[first],返回first + 1
- 反之返回first
注意到在二分法当中first可能会加1溢出数组,所以在上面步骤当中要在比较前加入判断first是否溢出数组
AC代码(Python)
_author_ = "YE"
# -*- coding:utf-8 -*- class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
len1 = len(nums)
first = 0
last = len1
while first < last:
mid = (first + last) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
first = mid + 1
else:
last = mid - 1
# print(first,last)
if first < len1 and nums[first] < target:
return first + 1
return first s = Solution()
nums = [1,3]
target = 2
print(s.searchInsert(nums, target))
[LeetCode]题解(python):035-Search Insert Position的更多相关文章
- [LeetCode] 035. Search Insert Position (Medium) (C++)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- LeetCode 035 Search Insert Position
题目要求:Search Insert Position Given a sorted array and a target value, return the index if the target ...
- Leetcode 题目整理 Sqrt && Search Insert Position
Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt ...
- LeetCode练题——35. Search Insert Position
1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...
- LeetCode Arrary Easy 35. Search Insert Position 题解
Description Given a sorted array and a target value, return the index if the target is found. If not ...
- Java for LeetCode 035 Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 035 Search Insert Position 搜索插入位置
给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引.如果没有,返回到它将会被按顺序插入的位置.你可以假设在数组中无重复元素.案例 1:输入: [1,3,5,6], 5输出: 2案例 2:输 ...
- LeetCode(35) Search Insert Position
题目 Given a sorted array and a target value, return the index if the target is found. If not, return ...
- LeetCode记录之35——Search Insert Position
这道题难度较低,没有必要作说明. Given a sorted array and a target value, return the index if the target is found. I ...
随机推荐
- Java读数据是的编码问题。
今天使用Java的I/O读写数据的时候,出现了中文乱码问题,在老师的帮助下找到了问题的根源: 在window中新建文件时,如果你新建的是文本文件或者是一个windows无法识别的文件,他默认使用的编码 ...
- 转:深入理解JavaScript闭包概念
闭包向来给包括JavaScript程序员在内的程序员以神秘,高深的感觉,事实上,闭包的概念在函数式编程语言中算不上是难以理解的知识.如果对作用域,函数为独立的对象这样的基本概念理解较好的话,理解闭包的 ...
- 【wikioi】1913 数字梯形问题(费用流)
http://wikioi.com/problem/1913/ 如果本题没有询问2和3,那么本题和蚯蚓那题一模一样.http://www.cnblogs.com/iwtwiioi/p/3935039. ...
- Linux_使用Linux之安装jdk 7
工具/原料 jdk7源码安装压缩包 方法/步骤 卸载OpenJDK rpm -qa | grep java rpm -e --nodeps java-1.6.0-openjdk-1.6.0.0-1 ...
- java截取图片部分尺寸
package util; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.io.File; i ...
- tomcat6.0安装
tomcat的安装基本是一步一步按提示来就行了: 这里填写用户名和密码,自己一定要记住啊,下面是路径的选择 然后查看安装成功与否,打开浏览器输入 然后看到下面页面就可以了
- Codeforeces 617E XOR and Favorite Number(莫队+小技巧)
E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- HTML5 javascript CSS3 jQuery Mobile一些好用的网站
jQueryMobile:学习 http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html 百度 CDN: http://cdn.cod ...
- [ZZ] GTX 280 GPU architecture
http://anandtech.com/show/2549 Now that NVIDIA’s has announced its newest GPU architecture (the GeFo ...
- Javascript 笔记与总结(1-6)Javascript 面向对象
在 JavaScript 中,有对象,没有类(但有构造函数). 在 JavaScript 中,对象不依赖于类而存在,可以直接生成. {key:value, key:value} 这种格式的对象,成为 ...