Search in Rotated Sorted Array

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.

思路:此题算法上不难。第一步计算旋转的步长。然后依据步长分情况得到升序的新的数组。然后二分查找target的索引。找到后再分情况返回元素的位置。

详细代码例如以下:

public class Solution {
public int search(int[] nums, int target) {
if(nums.length == 0){
return -1;
} if(nums.length == 1){
if(nums[0] == target)
return 0;
return -1;
} int rotate = 0;//旋转的步长
for(int i = nums.length - 1; i > 0; i--){
if(nums[i] < nums[i-1]){
rotate = i;
break;
}
}
int[] a = new int[nums.length];
//将数组按升序填充到新数组
for(int i = rotate; i < nums.length; i++){
a[i - rotate] = nums[i];//后面未旋转部分
}
for(int i = 0; i < rotate; i++){
a[i+nums.length-rotate] = nums[i];//前面旋转部分
} int index = -1;
//二分查找
int low = 0;
int hight = nums.length - 1;
while(low <= hight){
int mid = (low + hight)/2;
if(a[mid] > target){
hight = mid - 1;
}else if(a[mid] == target){
index = mid;
break;
}else{
low = mid + 1;
}
}
if(index == -1)
return -1;
if(index + rotate > nums.length - 1)
return index + rotate - nums.length;
return index + rotate;
}
}

兴许:这一题实在解的有些多余了。最简单的就是一次遍历。找到返回index,找不到返回-1.

代码例如以下:

public class Solution {
public int search(int[] nums, int target) {
for(int i = 0; i < nums.length; i++){
if(nums[i] == target){
return i;
}
}
return -1;
}
}

leetCode 33.Search in Rotated Sorted Array(排序旋转数组的查找) 解题思路和方法的更多相关文章

  1. [LeetCode] 33. Search in Rotated Sorted Array 在旋转有序数组中搜索

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

  2. LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>

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

  3. [array] leetcode - 33. Search in Rotated Sorted Array - Medium

    leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...

  4. [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. 思路 ...

  5. LeetCode 33. Search in Rotated Sorted Array(在旋转有序序列中搜索)

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

  6. LeetCode 33.Search in Rotated Sorted Array(M)

    题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...

  7. leetcode 33. Search in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  8. Java [leetcode 33]Search in Rotated Sorted Array

    题目描述: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 ...

  9. [leetcode]33. Search in Rotated Sorted Array旋转过有序数组里找目标值

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

随机推荐

  1. PairRDD中算子reduceByKey图解

    reduceByKey 函数原型: def reduceByKey(func: (V, V) => V): RDD[(K, V)] def reduceByKey(func: (V, V) =& ...

  2. filebeat+kafka+SparkStreaming程序报错及解决办法

    // :: WARN RandomBlockReplicationPolicy: Expecting replicas with only peer/s. // :: WARN BlockManage ...

  3. 【Java】初始化过程

    以下程序执行的结果是: class X{ Y y=new Y(); public X(){ System.out.print("X"); } } class Y{ public Y ...

  4. ORM框架为什么不流行了

    程序员的逻辑是先写sql脚本,然后在编写对应的实体代码. orm框架的逻辑是先写实体代码,然后自动生成脚本,构建数据库,这和程序员的逻辑或习惯刚好相反,所以这类ORM框架渐渐的被淘汰了,例如:Hibe ...

  5. OpenFileDialog 打开文件

    OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "选择导入的文件"; ofd.FilterIndex = 1; ofd ...

  6. 风雪之隅(Laruence PHP开发组成员, Zend兼职顾问, Yaf, Yar, Yac, Opcache等项目作者、维护者.)

    http://www.laruence.com/?from=inf&wvr=5&loc=infblog

  7. mysql插入、更新与删除

    数据库增删改查都是要熟练掌握的. 这部分就来看看前面3个比较简单的部分,增,删,改. 插入数据 为表的所有字段插入数据 insert into table_name (column_list) val ...

  8. TextBox控件的DataBindings属性

    DataBindings属性是很多控件都有的属性,作用有2方面.一方面是用于与数据库的数据进行绑定,进行数据显示.另一方面用于与控件或类的对象进行数据绑定.这里主要关注后者.主要用法是将某个对象的某个 ...

  9. WPF路由事件三:自定义路由事件

    与依赖项属性类似,WPF也为路由事件提供了WPF事件系统这一组成.为一个类型添加一个路由事件的方式与为类型添加依赖项属性的方法类似,添加一个自定义路由事件的步骤: 一.声明路由事件变量并注册:定义只读 ...

  10. java-关于getClass().getClassLoader()

    源地址:http://blog.sina.com.cn/s/blog_6ec6be0e01011xof.html InputStream is = getClass().getClassLoader( ...