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. 将struts的jar包拷贝到WEB-INF/lib导致eclipse中配置好的javadoc失效

    我通过这个步骤导入了struts的jar包并且配置好了javadoc,且亲测可用: http://www.cnblogs.com/qrlozte/p/3173805.html 但是当我把struts的 ...

  2. Linux RPM 命令参数使用详解 查看 rpm包依赖性

    转载自:http://blog.csdn.net/deutschester/article/details/6309521 rpm 执行安装包 二进制包(Binary)以及源代码包(Source)两种 ...

  3. spring 在service中需要抛出异常才能自动回滚

    在spring 事务配置中,如果service方法捕获了异常,则程序报错也不会自动回滚, 1.手动开启关闭事务 2.抛出异常,可以先捕获异常,然后自定义runtime异常,可不用声明

  4. C#中一道关于多线程的基础练习题——模拟仓库存销过程

    题目:模拟生产.入库.销售(50分) 假设某企业自产.自存.自销,需要将工厂生产的各类产品不定时的运到仓库,与此同时,需要将仓库中的货物运往超市和商场中进行销售,请编写一个程序模拟此过程(主要是存取这 ...

  5. iOS UITableView中关于cell里的按钮被点击时如何确定是哪一个section

    在section=10:row=1:的UITableView中,每一个cell都带有一个按钮,例如如下的图片一样每一个cell中都有一个“进入店铺的按钮”,但是如果我点击相应的cell要进入对应的店铺 ...

  6. jQuery插件 -- Form表单插件jquery.form.js<转>

    jQuery Form插件是一个优秀的Ajax表单插件,可以非常容易地.无侵入地升级HTML表单以支持Ajax.jQuery Form有两个核心方法 -- ajaxForm() 和 ajaxSubmi ...

  7. Quartz 一个JOB 配置多个Trigger时注意的问题

    public class SimpleExample { public void run() throws Exception { Logger log = LoggerFactory.getLogg ...

  8. DataGridView使用技巧七:列顺序的调整、操作行头列头的标题

    一.列顺序的调整 设定DataGridView的AllowUserToOrderColumns为True的时候,用户可以自由调整列的顺序. 当用户改变列的顺序的时候,其本身的Index不好改变,但是D ...

  9. No supported encrypter found. The cipher and / or key length are invalid.

    终端使用如下命令: php artisan key:generate 将生成的key复制到config/app.php替换82行的APP_KEY键值.

  10. 可能是目前最完整的前端框架 Vue.js 全面介绍

    Vue.js 是一个JavaScriptMVVM库,是一套构建用户界面的渐进式框架. 摘要 2016年最火的前端框架当属Vue.js了,很多使用过vue的程序员这样评价它,“vue.js兼具angul ...