剑指Offer之 - 旋转数组的最小数字
2015-05-02 16:32
295 查看
题目:
找出旋转数组中的最小数字。思路:
有两种情况,数组没有重复数字和有重复数字分别参考:http://blog.csdn.net/u012243115/article/details/41923667 和 http://blog.csdn.net/u012243115/article/details/41924217 。
代码:
#include <iostream> using namespace std; //查找一个旋转数组中的最小数字,注意数组有可能有重复的数字,也有可能根本没有旋转 int Min(int *num , int len) { if(num == NULL || len <= 0) return -1; int begin = 0; int end = len - 1; while(begin < end && num[begin] >= num[end]) { int mid = (begin + end) / 2; if(num[mid] > num[end]) begin = mid + 1; else if(num[mid] < num[end]) end = mid; else begin++; } return num[begin]; } int main() { int num[] = {1,0,1,1,1,1,1}; //int num[] = {1,2,3,4,5,6,7}; int min = Min(num , sizeof(num) / sizeof(*num)); cout<<min<<endl; }
相关题目:
在旋转数组中查找给定的目标值,也分两种情况。参考:http://blog.csdn.net/u012243115/article/details/42002283 和 http://blog.csdn.net/u012243115/article/details/42550131 。
相关文章推荐
- 【剑指offer】面试题8:旋转数组的最小数字
- 【剑指offer】旋转数组中的最小数字
- 剑指Offer(6)______旋转数组的最小数字
- 【剑指offer】旋转数组的最小数字
- 剑指offer第8题:旋转数组的最小数字
- [牛客网,剑指offer,python] 旋转数组的最小数字
- (六)剑指offer之旋转数组的最小数字
- 剑指offer-旋转数组的最小数字
- 剑指offer 旋转数组的最小数字
- 剑指offer---旋转数组的最小数字
- 剑指offer 旋转数组的最小数字 @python
- 剑指offer之旋转数组的最小数字
- 剑指offer:旋转数组的最小数字
- 剑指offer:旋转数组的最小数字
- 剑指offer 面试题8 旋转数组的最小数字
- 剑指offer面试题8:旋转数组的最小数字
- 剑指offer8:旋转数组的最小数字
- 【剑指offer】面试题8:旋转数组中的最小数字
- 剑指offer—旋转数组的最小数字
- 剑指offer源码-旋转数组的最小数字