题目:

Suppose an array sorted in ascending order 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).

Write a function to determine if a given target is in the array.

The array may contain duplicates.

思路:

1.当数组长度为0时,return false;

2.当数组长度为1时,比较nums[0]与target关系;

3.当数组长度大于1,且为rotated sorted array时,根据nums[mid]与nums[start]关系判断如何移动,见下图:

4.当数组长度大于1,但不是rotated sorted array时,用传统二分法。

代码:

 public class Solution {
public boolean search(int[] nums, int target) {
int start = 0,end = nums.length-1,mid = 0;
if(nums.length == 0){
return false;
}
if(nums.length == 1){
if(nums[0] == target){
return true;
}else{
return false;
}
}
while(nums[start]>= nums[end] && start + 1<end){
mid = start + (end - start)/2;
if(nums[mid] == target || nums[start] == target ||nums[end] == target ){
return true;
}else if(nums[mid] > nums[start]){
if(nums[mid] < target){
start = mid;
}else if(nums[start] < target){
end = mid;
}else if(nums[start] > target){
start = mid;
}
}else if(nums[mid] < nums[start]){
if(nums[mid] > target){
end = mid;
}else if(nums[end] > target){
start = mid;
}else if(nums[end] < target){
end = mid;
}
}else{
start++;
}
}
while(nums[start]< nums[end] && start + 1<end){
mid = start + (end - start)/2;
if(nums[mid] == target){
return true;
}else if(nums[mid] > target){
end = mid;
}else if(nums[mid] < target){
start = mid;
}else{
start++;
}
}
if(nums[start] == target || nums[end] == target){
return true;
}
return false;
}
}

LeetCode 81.Search in Rotated Sorted Array II(M)的更多相关文章

  1. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

  2. [leetcode]81. Search in Rotated Sorted Array II旋转过有序数组里找目标值II(有重)

    This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates. 思路 ...

  3. [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  4. [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  5. LeetCode 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  6. leetCode 81.Search in Rotated Sorted Array II (旋转数组的搜索II) 解题思路和方法

    Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...

  7. LeetCode 81 Search in Rotated Sorted Array II(循环有序数组中的查找问题)

    题目链接:https://leetcode.com/problems/search-in-rotated-sorted-array-ii/#/description   姊妹篇:http://www. ...

  8. Leetcode#81 Search in Rotated Sorted Array II

    原题地址 如果不存在重复元素,仅通过判断数组的首尾元素即可判断数组是否连续,但是有重复元素的话就不行了,最坏情况下所有元素都一样,此时只能通过线性扫描确定是否连续. 设对于规模为n的问题的工作量为T( ...

  9. leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search

    这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...

随机推荐

  1. UUID与时间戳

    /** * 32位去除'-'的UUID */ public static String getUUID() { String uuid = java.util.UUID.randomUUID().to ...

  2. AOP实现防止接口重复提交

    项目中对于状态变更接口存在重复提交的问题. package com.yxx.survey.foundation.aop; import com.alibaba.fastjson.JSON; impor ...

  3. VMware安装 RHEL安装完后,选择桥接模式如何与主机网络连通

    .查看主机的ip与网关,dns配置,如图 2.在linux系统中打开cd  /etc/sysconfig/network-scripts/  #进入网络配置文件目录 vi  ifcfg-eno1677 ...

  4. 2.Redis数据类型

    Redis中存储数据是通过key-value存储的,对于value的类型有以下几种: 字符串 Hash类型 List Set SortedSet(zset) 注:在redis中的命令语句中,命令是忽略 ...

  5. cs231n spring 2017 lecture2 Image Classification

    1. 相比于传统的人工提取特征(边.角等),深度学习是一种Data-Driven Approach.深度学习有统一的框架,喂不同的数据集,可以训练识别不同的物体.而人工提取特征的方式很脆弱,换一个物体 ...

  6. vue-cli3初始化项目

    1 npm install -g @vue/cli 创建配置 创建 1 vue create vue-app 选择配置 1234 ? Please pick a preset: (Use arrow ...

  7. Log4j输出的日志乱码问题

    设置日志输出编码: log4j.appender.stdout.Encoding=UTF-8

  8. Qt QByteArray 与 char* 的转换

    QByteArray 转换为 char * char *ch;//不要定义成ch[n]; QByteArray byte; ch = byte.data(); char * 转换为 QByteArra ...

  9. ZOJ-1177-K-Magic Number

    就是分别以1到9作为开头构造结果,取最小答案.看了参考书之后才做出来,对参考书上的代码进行了一些改进 Accepted 1177 C++11 0 408 #include "bits/std ...

  10. Java IO: PipedInputStream

    原文链接 作者: Jakob Jenkov 译者: 李璟(jlee381344197@gmail.com) PipedInputStream可以从管道中读取字节流数据,代码如下: 01 InputSt ...