[leetcode][c++] 35. search insert position 以及vector的简单归纳

版权声明:来自 T2777 ,请访问 https://blog.csdn.net/T2777 一起交流进步吧 https://blog.csdn.net/T2777/article/details/86743931

首先对容器vector(向量)做一个简单的归纳如下图的程序: 

#include<iostream>
#include<vector>
using namespace std;
int main()
{
	//包含2个元素均为2的向量vector
	vector<int> v(2, 2);
	cout << "初始向量为:" << endl;
	for(int i = 0;i < 2; i++)
		cout << "v[" << i << "]:" << v[i] << " ";
	cout << endl;
	//通过加上*号来实现输出
	cout << "*v.begin()为:" << endl;
	cout << *v.begin() << endl;
	//迭代器的使用
	vector<int>::iterator it;
	cout << "利用迭代器显示向量:" << endl;
	for (it = v.begin(); it != v.end(); it++)
		cout << *it <<" ";
	cout << endl;
	//通过cin改变向量中元素的值,当然也可以直接使用 v[0]=? 的赋值方法
	cout << "请输入v[0]" << endl;
	cin >> v[0];
	cout << "输入v[0]后的向量为:" << endl;
	for (it = v.begin(); it != v.end(); it++)
		cout << *it <<" ";
	cout << endl;
	//v.end()是指最后一个元素的下一个位置的指针
	cout << "v.end()-1为:"<<*(v.end() - 1) << endl;
	cout << "v.size()为: "<<v.size() << endl;
	cout << "v.capacity()为: "<<v.capacity() << endl;
	cout << "v.empty:(是用0和1来进行表示的) "<< v.empty() << endl;
	cout << "v.push_back()后:" << endl;
	v.push_back(3);
	for (it = v.begin(); it != v.end(); it++)
		cout << *it <<" ";
	cout << endl;
	cout << "v.insert(v.begin()+1,5)后的向量为:)" << endl;
	v.insert(v.begin()+1,5);
	for (it = v.begin(); it != v.end(); it++)
		cout << *it <<" ";
	cout << endl;
	//执行pop_back的操作
	cout << "v.pop_back()后:" << endl;
	v.pop_back();
	for (it = v.begin(); it != v.end(); it++)
		cout << *it << " ";
	cout << endl;
	//清除指定位置的元素
	cout << "v.erase(v.begin()+1)后的向量为:" << endl;
	v.erase(v.begin()+1);
	for (it = v.begin(); it != v.end(); it++)
		cout << *it << " ";
	cout << endl;
	cout << "执行v.clear()后的size为:" << endl;
	v.clear();
	cout << v.size() << endl;
	return 0;
}

输出结果为:

初始向量为:
v[0]:2 v[1]:2
*v.begin()为:
2
利用迭代器显示向量:
2 2
请输入v[0]
6
输入v[0]后的向量为:
6 2
v.end()-1为:2
v.size()为: 2
v.capacity()为: 2
v.empty:(是用0和1来进行表示的) 0
v.push_back()后:
6 2 3
v.insert(v.begin()+1,5)后的向量为:)
6 5 2 3
v.pop_back()后:
6 5 2
v.erase(v.begin()+1)后的向量为:
6 2
执行v.clear()后的size为:
0

 回到题目,题目也很简单,是在一个已经排好序的数组中找到给定数应当插入的位置,假定数组中没有冗余的数据,因为并不需要重新排序,只要返回给定数应当插入的位置即可,所以只要找到给定数小于等于该位置原来的第一个数的位置即可。

题目:

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.

Example 1:

Input: [1,3,5,6], 5
Output: 2
Example 2:

Input: [1,3,5,6], 2
Output: 1
Example 3:

Input: [1,3,5,6], 7
Output: 4
Example 4:

Input: [1,3,5,6], 0
Output: 0

代码为:

class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        int n = nums.size();
        for(int i = 0; i < nums.size(); i++ )
        {
            if(target <= nums[i])
            {
                return i;
            }
            
        }
        return nums.size();
        }
};

当然这个答案好像简单了一点,大部分人的效率都在这个段位,集中在98%

考虑使用二分法,所谓二分法,就是在已经排好序的数组中找到应当插入的位置,通过每次找到中间位置进行比较,显然可以降低时间复杂度。不过要注意到可能会产生小数位置。

猜你喜欢

转载自blog.csdn.net/T2777/article/details/86743931