[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 ...
随机推荐
- cocos2d ccmenulabel
// // TestMenu.hpp // mario // // Created by sun on 15/12/22. // // #ifndef TestMenu_hpp #define Tes ...
- bug 修改心得
我在做一个项目的时候,分页无法显示,于是我就开始进行各种修改. 最后我发现竟然是因为配置文件写错了,结果页面跳到别的页面, 跳转到了单项详细页面.
- 【BZOJ】1002: [FJOI2007]轮状病毒(DP+规律+高精度)
http://www.lydsy.com/JudgeOnline/problem.php?id=1002 其实我还是看题解的,而且看了题解也没明白那公式怎么来的T_T,先水过了先把....以后研究一下 ...
- rbegin 和 end 区别
在使用C++的STL时,经常会用到迭代器,那么不得不搞清楚里面的一些指针 begin(), end(), rbegin(), rend()之间的区别与联系,以及它们分别都指向哪个元素.首先要明白的一点 ...
- Programming with gtkmm 3
https://developer.gnome.org/gtkmm-tutorial/unstable/index.html.zh_CN 1. 序言 1.1. 本书 1.2. gtkmm 2. 安 ...
- poj2387 初涉最短路
前两天自学了一点点最短路..看起来很简单的样子... 就去kuangbin的专题找了最简单的一道题练手..然后被自己萌萌的三重for循环超时虐的不要不要的~ 松弛虽然会但是用的十分之不熟练... 代码 ...
- Swing 刷新容器
JPanel pchks = new JPanel();// 容器刷新(重新layout所有空间)pchks.validate();// 容器重绘(当容器内的东西由多变少时,防止多出来的部分没有清楚) ...
- jquery选中将select下拉框中一项后赋值给text文本框
jquery选中将select下拉框中一项后赋值给text文本框,出现无法将第一个下拉框的value赋值给文本框 因为select默认选中第一项..在选择第一项时,便导致无法激发onchange事件. ...
- heapsort
Introduction to Algorithms Third Edition The (binary) heap data structure is an array object that we ...
- 用c++builder读取一个一行有多行变量的文件
文件内容如下: C DXDY.INP FILE, IN FREE FORMAT ACROSS COLUMNS for 83658 Active CellsC 2013-5-25 上午 10:43 ...