旋转数组的最小数字
来源:互联网 发布:淘宝头条写手工资 编辑:程序博客网 时间:2024/06/08 10:18
题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个递增排序的的数组的旋转,输出旋转数组的最小元素,如{3,4,5,1,2}位{1,2,3,4,5}的数组旋转。最小值是1
思路:用二分查找法
#include<iostream>#include<string>#include<cstring>#include<cstdlib>#include<cstdio>using namespace std;/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int minNumberInRotateArray(vector<int> rotateArray) { if(rotateArray.size()==1) return rotateArray[0]; int index_left =0,index_right = rotateArray.size()-1; int mid = index_left; while(rotateArray[index_left]>=rotateArray[index_right]){ if(index_left +1 == index_right) { return rotateArray[index_right]; } mid = (index_left +index_right)/2; if(rotateArray[index_left]==rotateArray[index_right]&&rotateArray[index_left]==rotateArray[mid]){ int result = rotateArray[index_left]; for(int i = index_left+1;i<=index_right;i++){ if(result> rotateArray[i]) result = rotateArray[i]; } return result; } if(rotateArray[mid]>=rotateArray[index_left]){ index_left = mid; }else index_right = mid; } return rotateArray[mid]; }};
0 0
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- MYSQL数据库创建表分区
- ajax进入error()中 Access-Control-Allow-Origin
- linux svn客户端安装
- QTabWidget添加自定义样式
- 【系统】[Linux]CentOS 7之下载、制作U盘系统盘篇
- 旋转数组的最小数字
- 匹配字符串的KMP算法
- STS 中使用git进行push或pull时报错:cannot open git-receive-pack
- wx
- 微信第三方登录(Android 实现)
- Windows7下通过U盘安装Ubuntu12.04双系统
- 剖析printf函数
- JQuery学习笔记之遍历
- jQuery中ajax的用法