lintcode :搜索旋转排序数组
题目
搜索旋转排序数组
假设有一个排序的按未知的旋转轴旋转的数组(比如,0 1 2 4 5 6 7 可能成为4 5 6 7 0 1 2)。给定一个目标值进行搜索,如果在数组中找到目标值返回数组中的索引位置,否则返回-1。
你可以假设数组中不存在重复的元素。
给出[4, 5, 1, 2, 3]和target=1,返回 2
给出[4, 5, 1, 2, 3]和target=0,返回 -1
解题
直接找,时间复杂度O(N)
class Solution:
"""
@param A : a list of integers
@param target : an integer to be searched
@return : an integer
"""
def search(self, A, target):
# write your code here
if A == None:
return -1
l = len(A)
for i in range(l):
if A[i]==target:
return i
return -1
这里题目说了是有序的数组进行了旋转,为什么直接遍历这个蠢的方法?已知是排序的,只是进行了旋转,能否用二分法?
public class Solution {
/**
*@param A : an integer rotated sorted array
*@param target : an integer to be searched
*return : an integer
*/
public int search(int[] A, int target) {
if (A == null || A.length == 0) {
return -1;
}
int start = 0;
int end = A.length - 1;
int mid;
while (start + 1 < end) {
mid = (end + start) / 2;
if (A[mid] == target) {
return mid;
}
if (A[start] < A[mid]) { // 左边升序
// situation 1, red line
if (A[start] <= target && target <= A[mid]) {
end = mid;
} else {
start = mid;
}
} else { // 右边升序
// situation 2, green line
if (A[mid] <= target && target <= A[end]) {
start = mid;
} else {
end = mid;
}
}
} // while
if (A[start] == target) {
return start;
}
if (A[end] == target) {
return end;
}
return -1;
}
}
lintcode :搜索旋转排序数组的更多相关文章
- lintcode 中等题:搜索旋转排序数组II
题目 搜索旋转排序数组 II 跟进“搜索旋转排序数组”,假如有重复元素又将如何? 是否会影响运行时间复杂度? 如何影响? 为何会影响? 写出一个函数判断给定的目标值是否出现在数组中. 样例 给出[3, ...
- 63 搜索旋转排序数组II
原题网址:https://www.lintcode.com/problem/search-in-rotated-sorted-array-ii/description 描述 跟进“搜索旋转排序数组”, ...
- [Swift]LeetCode81. 搜索旋转排序数组 II | Search in Rotated Sorted Array II
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- LeetCode 81 - 搜索旋转排序数组 II - [二分+暴力]
假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,0,1,2,2,5,6] 可能变为 [2,5,6,0,0,1,2] ). 编写一个函数来判断给定的目标值是否存在于数组中. ...
- 【1】【leetcode-33,81】 搜索旋转排序数组
(没思路) 33. 搜索旋转排序数组 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 搜索一个给 ...
- LeetCode(81): 搜索旋转排序数组 II
Medium! 题目描述: 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,0,1,2,2,5,6] 可能变为 [2,5,6,0,0,1,2] ). 编写一个函数来判断给 ...
- LeetCode 81 搜索旋转排序数组II
题目: 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,0,1,2,2,5,6] 可能变为 [2,5,6,0,0,1,2] ). 编写一个函数来判断给定的目标值是否存在于 ...
- 搜索旋转排序数组 II
跟进“搜索旋转排序数组”,假如有重复元素又将如何? 一句话思路:不能二分,因为复杂度是n eg全是0,找一个1 class Solution { public boolean search(int[] ...
- LeetCode33 搜索旋转排序数组
搜索旋转排序数组 题目描述: 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 搜索一个给定的目标 ...
随机推荐
- iOS屏幕尺寸和分辨率
iOS平台家族成员主要包括iPhone.iPod Touch和iPad,但是各类设备的分辨率各不相同,目前存在的尺寸主要有: iOS设备的尺寸多种多样,此外,屏幕的分辨率也有多种,总结如下表所示: 其 ...
- Android app version code and name
android:versionCode和android:versionName 区别 Android的版本可以在androidmainfest.xml中定义, 主要有android:version ...
- textarea 在光标处插入文字
效果演示 // 欢迎访问cssfirefly.cnblogs.com html: <textarea id="text" style="width:500px;he ...
- php随机验证码
今天同学问我,用php怎么写验证码,由于是新手所以花了半天的时间才完成.而且功能很是简单呵呵.今天本来打算写session和cookie的看来是要明天了. <?php $image_width= ...
- 如何重建Octopress本地环境
# 安装rvm, ruby, bundler 略 # 克隆octopress $ git clone git://github.com/imathis/octopress.git octopress ...
- 如何计算IP地址及CIDR(收藏)
如何计算IP地址及CIDR 一. IP地址概念 IP地址是一个32位的二进制数,它由网络ID和主机ID两部份组成,用来在网络中唯一的标识的一台计算机.网络ID用来标识计算机所处的网段:主 机ID用来标 ...
- Leeo 智能夜灯:默默守护你的家
http://www.ifanr.com/462377 Leeo 智能夜灯是一个低调的设备.它不需要你与之交互,也不会产生多余的费用.当你把它插到墙上插座,然后下载应用后,就不用再管它了.然后,它就开 ...
- JDBC增删改查
/* db.properties的配置 driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/day14 username=root ...
- audio 设置 currentTime 失效 的解决办法
当服务端返回的 音频文件标示 no-cache 的时候,会引起currentTime 失败. 改掉server 返回头信息.解除禁止缓存,一切ok.
- linux 命令小结
chkconfig --list 查询所有服务运行情况 修改文件夹权限: 在Linux中,权限的所有者分为用户权限,组权限和其他权限,分别是用字母u, g, o 代表权限分为:读 r , 写 w , ...