2.1.3—线性表—Search in Rotated Sorted Array
来源:互联网 发布:单片机什么是硬件,软件 编辑:程序博客网 时间:2024/05/17 07:19
描述
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e.,0 1 2 4 5 6 7might become 4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e.,0 1 2 4 5 6 7might become 4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
#include<iostream>using namespace std;int FindNumRotatedArray(int a[], int n, int value){if (a == NULL || n <= 0)return -1;int begin = 0;int end = n - 1;while (begin <= end){int mid = (begin + end) / 2;if (a[mid] == value)return mid;if (a[mid] >= a[begin]){if (a[begin]<=value&&value < a[mid])end = mid-1;elsebegin = mid + 1;}else{if (a[mid] < value &&value <= a[end])begin = mid + 1;elseend = mid-1;}}return -1;}void main(){const int n = 10;int value = 6;int a[10] = { 6,7,9,10,13,24,-1,0,3,4 };int index = FindNumRotatedArray(a, n,value);if (index != -1)cout << a[index] << endl;}
阅读全文
0 0
- 2.1.3—线性表—Search in Rotated Sorted Array
- 2.1.4—线性表—Search in Rotated Sorted Array II
- 2.1.3 Search in Rotated Sorted Array
- 2.1.3 Search in Rotated Sorted Array
- 线性表-33. Search in Rotated Sorted Array
- 3:Search in Rotated Sorted Array
- LeetCode —— Search in Rotated Sorted Array
- LeetCode——Search in Rotated Sorted Array
- LeetCode——Search in Rotated Sorted Array II
- LeetCode——Search in Rotated Sorted Array
- LeetCode——Search in Rotated Sorted Array II
- Search in Rotated Sorted Array——LeetCode
- leetcode 033 —— Search in Rotated Sorted Array
- leetcode 081 —— Search in Rotated Sorted Array II
- LeetCode33——Search in Rotated Sorted Array
- LeetCode81——Search in Rotated Sorted Array II
- leetcode——Search in Rotated Sorted Array
- 二分查找——Search in Rotated Sorted Array
- 2017年8月1日,星期二
- HDU-6060 RXD and dividing
- 乐观锁的两种实现方式
- 动态添加一个HTML标记
- HEVC函数入门(17)——编码一个CU
- 2.1.3—线性表—Search in Rotated Sorted Array
- JAVA学习_day02
- 分享 牛津学生 英汉词典 外研社最新 简明英汉词典 等 pdf
- VS本地调试x64 平台 “Visual Studio Remote Debugging Monitor 已停止工作”
- JavaScript之表单脚本
- 每日MySQL之006:MySQL中的binary log
- [Cocos2d塔防游戏开发]Cocos2dx-3.X完成塔防游戏《王国保卫战》--简介+代码+资源
- java研发网页数据采集
- CSU-ACM2017暑期训练5-三分 D