博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Search in Rotated Sorted Array
阅读量:7060 次
发布时间:2019-06-28

本文共 1060 字,大约阅读时间需要 3 分钟。

Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might 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.

 

 to see which companies asked this question

1.做了下标映射

class Solution {public:        int binary_search(vector
&nums, int target) { int l = 0; int r = nums.size() -1; while (l<=r) { int mid = l + ((r - l) >> 1); if (nums[mid] == target) { return mid; } else if (nums[mid] < target) { l = mid + 1; } else { r = mid - 1; } } return -1; } int search(vector
& nums, int target) { //映射 int i = 0; vector
tmp_nums; vector
tmp_index; int j = 0; for (i=0; i

 

转载于:https://www.cnblogs.com/SpeakSoftlyLove/p/5184339.html

你可能感兴趣的文章
[LeetCode] Invert Binary Tree
查看>>
uincode常识
查看>>
QT两个字符串转化函数,避免文字乱码。
查看>>
2018.3.31——(4)句子
查看>>
js call
查看>>
限定符
查看>>
You cannot change what you refuse to confront.
查看>>
a 标签 跳转4种类型
查看>>
jenkins+ant+ssh远程部署服务glassfish
查看>>
洛谷—— P2543 [AHOI2004]奇怪的字符串
查看>>
洛谷——P1358 扑克牌
查看>>
MyBatis注意事项
查看>>
WCF配置文件
查看>>
VR系统的组成
查看>>
ECshop新增单页面模板的方法
查看>>
【转载】VMware vSphere 5 HA详解 1
查看>>
【原创】] windows环境下Android环境变量配置(新手专用)
查看>>
guid转int
查看>>
程序员数学四部曲
查看>>
【原】Java学习笔记024 - 包装类
查看>>