麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

leecode 解題總結:35. Search Insert Position

2019-11-10 19:35:37
字體:
來源:轉載
供稿:網友
#include <iostream>#include <stdio.h>#include <vector>using namespace std;/*問題: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分析:這是二分查找的lowwer_bound的問題。輸入:4 51 3 5 64 21 3 5 64 71 3 5 64 01 3 5 6輸出2140關鍵:1 lowwer_bound:		//low == high時,如果找到,就返回		if(nums.at(low) >= target)		{			return low;		}		//說明是數組最后一個元素,返回low+1		else		{			return low + 1;		}*/class Solution {public:	int lower_bound(vector<int>& nums , int target)	{		if(nums.empty())		{			return -1;		}		int low = 0;		int high = nums.size() - 1;		int mid;		while(low < high)		{			mid  = low + (high - low) / 2;			//中間大于目標值,目標值,mid可能是結果,繼續在左半部分尋找			if(nums.at(mid) >= target)			{				high = mid;			}			//中間值 < 目標值,mid不可能是結果,在右半部分尋找			else			{				low = mid + 1;			}		}		//low == high時,如果找到,就返回		if(nums.at(low) >= target)		{			return low;		}		//說明是數組最后一個元素,返回low+1		else		{			return low + 1;		}	}    int searchInsert(vector<int>& nums, int target) {		int high = lower_bound(nums , target);		return high;    }};void PRocess(){	int num ;	int value;	vector<int> nums;	int target;	Solution solution;	vector<int> results;	while(cin >> num >> target)	{		nums.clear();		for(int i  = 0 ; i < num ; i++)		{			cin >> value;			nums.push_back(value);		}		int result = solution.searchInsert(nums , target);		cout << result << endl;	}}int main(int argc , char* argv[]){	process();	getchar();	return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产1区2区在线观看 | 一本色道久久综合亚洲精品小说 | 日日摸夜夜添夜夜添牛牛 | 中国国语毛片免费观看视频 | 久久久久久久久久久综合 | 国产精品久久久久久久久久电影 | 毛片在线免费视频 | omofun 动漫在线观看 | 男女生羞羞视频网站在线观看 | 俄罗斯16一20sex牲色另类 | 国产亚洲黑人性受xxxx精品 | 欧美性生活网站 | 欧美特黄一级高清免费的香蕉 | 毛片一区二区三区四区 | 久久精品站| 好吊色37pao在线观看 | 国产精品一区久久久久 | 久久久av影视 | 亚洲精品欧美在线 | 五月激情久久 | 国产男女爽爽爽爽爽免费视频 | 欧美精品一区二区三区久久久 | 久久久久久艹 | 中文字幕电影免费播放 | 黄色影院网站 | 久草在线观看福利视频 | 久久成年网站 | 亚洲一区二区免费 | 国产精品亚洲一区二区三区在线观看 | hdhdhdhd19日本人| 国产免费高清 | 免费观看黄视频 | 国产精品视频中文字幕 | 91久久国产综合久久91精品网站 | 俄罗斯16一20sex牲色另类 | 久久色在线 | 久久国产精 | 草草视频免费 | 久久精品一二三区 | 日本aⅴ在线 | 亚洲xxx在线观看 |