// ConsoleApplication10.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;

class BinarySearch {
public:
	int getPos(vector<int> A, int n, int val) {
		// write code here
		int beg = 0;
		int end = n-1;
		int mid = (beg + end) / 2;
		int pos = -1;
		while (beg<=end)
		{
			if (A[mid] == val)
			{
				--mid;
				while (A[mid]==val && mid>=0)
				{
					--mid;
				}
				pos = mid + 1;
				break;
			}
			else if (A[mid] < val)
			{
				beg = mid + 1;
				mid= (beg + end) / 2;
			}
			else
			{
				end = mid - 1;
				mid = (beg + end) / 2;
			}
		}
		return pos;
	}
};
int main()
{
	vector<int> A = { 3,3,5,7,9 };
	int n = 5, val = 3;
	BinarySearch bs;
	cout << bs.getPos(A, n, 3) << endl;

	return 0;
};

相关文章:

  • 2022-12-23
  • 2021-08-23
  • 2022-12-23
  • 2021-12-10
  • 2022-12-23
  • 2021-07-22
  • 2022-12-23
  • 2021-09-21
猜你喜欢
  • 2022-12-23
  • 2021-07-14
  • 2021-09-04
  • 2021-08-24
  • 2022-12-23
  • 2021-11-18
  • 2022-12-23
相关资源
相似解决方案