LeetCode OJ: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
首先是自己一个比较蠢的方法,可能当时没怎么细细的想,大体的思路就是,将vector中元素存放到set中(因为set插入的时候已经排好序了),首先查找,找不到的话在插入,并且记下插入位置,指针递增到那个地方的时候就找到了那个位置。如果第一次找到那个位置的就直接递增找到那个位置即可,代码见下,很不优雅:
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
set<int>tmpSet(nums.begin(), nums.end());//因为set已经排好序了,所以用set
int i = ;
set<int>::iterator sItor;
if((sItor = (tmpSet.find(target))) == tmpSet.end())//不在set中的话,就先插入
sItor = tmpSet.insert(target);
for(auto itor = tmpSet.begin(); itor != it.first; ++itor){
i++;
return i;
}
};
但是其实在网上找了点好的答案,实际上这个就是二分法的一个小小的变形而已:
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int beg = ;
int end = nums.size() - ;
int mid;
while(beg <= end){
mid = (beg + end) >> ;
if(nums[mid] > target)
end = mid - ;
else if(nums[mid] < target)
beg = mid + ;
else
return mid;
}
int sz = nums.size();
if(end < ) reutrn ; //这个地方应该注意,不要搞反了
if(beg >= sz) reutrn sz;
return beg; //这一步应该注意,很关键
}
};
二分法不可小觑,细节还是很多的,仔细看看都能有不小的收获。其实实际上感觉写出来不太容易错的还是上面写的那种方法,不过那个总杆觉写出来怪怪的,不合题目的初衷了都。
java代码如下所示:
public class Solution {
public int searchInsert(int[] nums, int target) {
int beg = 0;
int end = nums.length - 1;
while(beg <= end){
int mid = beg + (end - beg)/2;
if(nums[mid] > target)
end--;
else if(nums[mid] < target)
beg++;
else
return mid;
}
if(beg >= nums.length)
return nums.length;
if(end < 0)
return 0;
return beg;
}
}
LeetCode OJ:Search Insert Position(查找插入位置)的更多相关文章
- [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 ...
- Search insert position, 查找插入位置
问题描述:给定一个有序序列,如果找到target,返回下标,如果找不到,返回插入位置. 算法分析:依旧利用二分查找算法. public int searchInsert(int[] nums, int ...
- 【LeetCode】Search Insert Position(搜索插入位置)
这道题是LeetCode里的第35道题. 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元 ...
- [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 (Medium) (C++)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- LeetCode 035 Search Insert Position
题目要求:Search Insert Position Given a sorted array and a target value, return the index if the 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(查找插入位置)
题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description 在给定的有序数组中插入一个目标数字,求出插入 ...
- Leetcode 35 Search Insert Position 二分查找(二分下标)
基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第 ...
随机推荐
- 002-使用java类调用quartz
一.工具类 package com.tech.jin.jobScheduler; import java.text.ParseException; import java.util.ArrayList ...
- 使用哈工大LTP进行句法分析
作者注:本教程旨在对哈工大LTP在github上的LTP4J(LTP的java版本)教程的补充,请结合以下参考网站一起食用. 参考网站: [1]哈工大语言技术平台云官网--LTP使用文档 http:/ ...
- PHP引用符&的用法详细解析
本文转自:http://blog.csdn.net/vip_linux/article/details/10206091PHP中引用符&的用法.关于php的引用(就是在变量或者函数.对象等前面 ...
- yum速查
yum命令是在Fedora和RedHat以及SUSE中基于rpm的软件包管理器,它可以使系统管理人员交互和自动化地更细与管理RPM软件包, 能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖 ...
- GIT学习笔记(5):变基
GIT学习笔记(5):变基rebase 变基 引入变基 在Git中整合来自不同分支的修改主要有两种方法:merge以及rebase. 整合分支最容易的方法是merge,他会把两个分支的最新快照以及两者 ...
- 在vps主机上***
一.安装 Shadowsocks服务端: 1.下载软件包 yum install python-setuptools && easy_install pip pip install s ...
- PAT 天梯赛 L1-047. 装睡 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-047 AC代码 #include <iostream> #include <cstdio&g ...
- CodeForces - 987E Petr and Permutations (思维+逆序对)
题意:初始有一个序列[1,2,...N],一次操作可以将任意两个位置的值互换,Petr做3*n次操作:Alxe做7*n+1次操作.给出最后生成的新序列,问是由谁操作得到的. 分析:一个序列的状态可以归 ...
- 对Java 注解的一些理解
转载自https://blog.csdn.net/javazejian/article/details/71860633 引入 注解最简单的使用方式 Java注解与普通修饰符(public\stati ...
- Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration info
Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the ...