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. ny10 skilng

    skiing 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑 ...

  2. 发布了listener报404

    我刚才发布了这么一个listener package org.lxh.listenerdemo ; import javax.servlet.http.* ; public class HttpSes ...

  3. 【JavaFx】客户端服务器C/S架构搭建

    客户端获取服务器端软件更新版本方法: package com.platform.ui.update; import java.io.BufferedInputStream; import java.i ...

  4. Activiti - 新一代的开源BPM引擎

    Activiti 背景简介.服务和功能介绍 背景介绍 Activiti 其核心是 BPMN 2.0 的流程引擎.BPMN 是目前被各 BPM 厂商广泛接受的 BPM 标准,全称为 Business P ...

  5. 在N个元素的数组中获取K个元素的所有组合问题

    可以写循环,也可以用模块. 百度许久找到一个博客 http://blog.sina.com.cn/s/blog_4a0824490101f1kc.html 详细介绍了Algorithm::Combin ...

  6. 如何在Windows Server 2008 R2下搭建FTP服务

    在Windows Server 2008 R2下搭建FTP服务,供客户端读取和上传文件 百度经验:jingyan.baidu.com 工具/原料 Windows Server 2008 R2 百度经验 ...

  7. Java并发(二)多线程的好处

    即使编写多线程程序具有挑战性,但它仍在使用中,是因为它可以带来如下的好处: 更好的资源利用 在某些场景下程序的设计会更简单 提升程序的响应性 更好的资源利用 假设一个应用会从本地文件系统中读取和处理文 ...

  8. yum命令与使用

    check          Check for problems in the rpmdbcheck-update   Check for available package updatesclea ...

  9. 怎样统计分析CSDN博客流量

    第一.IP.PV和UV各自是什么意思? IP.实际上也就是指独立IP,它的英文为Internet ***otocol,是独立IP数的意思.00:00-24:00同样IP地址记录一次.即使你有多台电脑. ...

  10. 自然语言交流系统 phxnet团队 创新实训 个人博客 (二)

    因为项目用的到条件编译,遂专门记载: 众所周知在C和CPP中可以通过预处理语句来实现条件编译,但是在java中没有预处理语句,我们该如何实现条件编译呢? 这是一个简单的demo public clas ...