首页 > 其他 > 详细

[容易]二分查找

时间:2016-05-01 20:20:17      阅读:146      评论:0      收藏:0      [点我收藏+]

题目来源:http://www.lintcode.com/zh-cn/problem/first-position-of-target/

技术分享

可以accept的程序如下(用了STL):

 1 class Solution {
 2 public:
 3     /**
 4      * @param nums: The integer array.
 5      * @param target: Target number to find.
 6      * @return: The first position of target. Position starts from 0. 
 7      */
 8     int binarySearch(vector<int> &array, int target) {
 9         // write your code here
10         vector<int>::iterator p;
11         p=find(array.begin(),array.end(),target);
12         if(p!=array.end())
13             return p-array.begin();
14         else
15             return -1;
16     }
17 };

[容易]二分查找

原文:http://www.cnblogs.com/hslzju/p/5451034.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!