leetCode 33.Search in Rotated Sorted Array(排序旋转数组的查找) 解题思路和方法
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(排序旋转数组的查找) 解题思路和方法的更多相关文章
- [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. ...
- LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>
LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...
- [array] leetcode - 33. Search in Rotated Sorted Array - Medium
leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...
- [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. 思路 ...
- 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. ...
- LeetCode 33.Search in Rotated Sorted Array(M)
题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...
- 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 ...
- 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 ...
- [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. ...
随机推荐
- Freemarker-2.3.22 Demo - No03_使用map绑定多个参数
package No03_使用map绑定多个参数; import java.io.File; import java.io.FileOutputStream; import java.io.Outpu ...
- Linux中查看CPU信息【转】
[转自]:http://blog.chinaunix.net/uid-23622436-id-3311579.html cat /proc/cpuinfo中的信息 processor 逻辑 ...
- js中将文件的base64转换成file并上传到服务器
** * @param base64Codes * 图片的base64编码 */ function sumitImageFile(base64Codes){ var form=document.for ...
- C#中WebBrowser控件的使用
今天在YouTube上看了一个关于WebBrowser控件用法的小视频,做一下总结. 首先创建一个WinForm程序,拖入一个textbox控件和一个button按钮,然后拖入一个panel控件,如图 ...
- python AES 加密
pad: ZeroPadding mode: cbc #!/usr/bin/env python# -*- coding:utf-8 -*-# 这里使用pycrypto库# 按照方法:easy_in ...
- 一款基于TweenMax.js的网页幻灯片
之前介绍了好多网页幻灯片.今天给大家带来一款基于TweenMax.js的网页幻灯片.这款幻灯片以不规则的碎片百叶窗的形式切换.切换效果非常漂亮.一起看下效果图: 在线预览 源码下载 实现的代码. ...
- Winform DatagridviewcomboboxColumn Disable Style
DataGridViewComboBoxCell cell =(DataGridViewComboBoxCell)dataGridView1[e.ColumnIndex, e.RowIndex]; c ...
- u-boot 2011.09 使用自己的board 以及config.h
一个新的方案,用的UBOOT 可能和上一个方案是同一个,但是配置有可能不一样,今天记录一下通过修改配置文件使用新的 board 文件以及 config.h 进入 u-boot 2011.09 // 打 ...
- [pthread]Linux C 多线程简单示例
#include <stdio.h> #include <pthread.h> pthread_mutex_t mutex; pthread_cond_t cond; void ...
- Ubuntu 下查看中文man手册方法
转载自:http://blog.chinaunix.net/uid-24830506-id-3266493.html Ubuntu 中文man手册安装方法 分类: LINUX Ubuntu 下查看中文 ...