一句话思路:反正只是寻找一个最小区间,断开也能二分。根据m第一次的落点,来分情况讨论。

一刷报错:

  1. 结构上有根本性错误:应该是while里面包括if,不然会把代码重复写两遍,不好。
 //situation1
if (nums[mid] > nums[start]) {
while (start + 1 < mid) {
mid = start + (end - start) / 2;
if (nums[mid] == target) {
return mid;
}
else if (nums[start] <= target && target <= nums[mid]) {
end = mid;
}
else start = mid;
}
} //situantion2
else {
while (start + 1 < mid) {
mid = start + (end - start) / 2;
if (nums[mid] == target) {
return mid;
}
if (nums[mid] <= target && target <= nums[end]) {
start = mid;
}
else end = mid;
}
}
  1. int方法的corner case返回值:数组为空,数组长度为0
  2. 居然没有写target== nums[mid]的特殊情况,该死!

一句话总结思路:要把while放在外面,if放在里面。

二刷ac

public class Solution {
/*
* @param nums: an integer rotated sorted array
* @param target: an integer to be searched
* @return: an integer
*/
public int search(int[] nums, int target) {
// write your code here
if (nums.length == 0 || nums == null) {
return -1;
} int start = 0;
int end = nums.length - 1;
int mid; while (start + 1 < end) {
mid = start + (end - start) / 2;
//situation1,red line
if (nums[mid] > nums[start]) {
if (nums[mid] == target) {
end = mid;
}
else if (nums[start] <= target && target <= nums[mid]) {
end = mid;
}
else start = mid;
}
//situation2,green line
else {
if (nums[mid] == target) {
end = mid;
}
if (nums[mid] <= target && target <= nums[end]) {
start = mid;
}
else end = mid;
}
} if (nums[start] == target) {
return start;
}
if (nums[end] == target) {
return end;
} return -1;
}
}

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

    33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...

  3. 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 ...

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

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

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

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

  6. [Leetcode][Python]33: Search in Rotated Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...

  7. 33. Search in Rotated Sorted Array & 81. Search in Rotated Sorted Array II

    33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...

  8. 刷题33. Search in Rotated Sorted Array

    一.题目说明 这个题目是33. Search in Rotated Sorted Array,说的是在一个"扭转"的有序列表中,查找一个元素,时间复杂度O(logn). 二.我的解 ...

  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. SQL的三种连接方式内连接、左连接、外连接

    1.内连接 select * from table_a x inner join table_b y on x.a_id = y.b_id 返回两个表关键字x.a_id = y.b_id的交集数据集 ...

  2. 【Python编程:从入门到实践】chapter9 类

    chapter9 类 9.1 创建和使用类 9.1.1 创建Dog类 class Dog(): """一次模拟小狗的简单尝试""" def ...

  3. Design a high performance cache for multi-threaded environment

    如何设计一个支持高并发的高性能缓存库 不 考虑并发情况下的缓存的设计大家应该都比较清楚,基本上就是用map/hashmap存储键值,然后用双向链表记录一个LRU来用于缓存的清理.这篇文章 应该是讲得很 ...

  4. Jenkins 邮件发送设置(jenkins自带邮件设置)

    首先进入系统设置,找到Jenkins Location部分 这里设置 系统管理员邮件地址,然后设置邮件通知部分,这里为了方便我使用了QQ邮箱(作为发送邮件地址) 这里的 用户名 必须与上面的 系统管理 ...

  5. 二维数组 cudaMallocPitch() 和三维数组 cudaMalloc3D() 的使用

    ▶ 使用函数 cudaMallocPitch() 和配套的函数 cudaMemcpy2D() 来使用二维数组.C 中二维数组内存分配是转化为一维数组,连贯紧凑,每次访问数组中的元素都必须从数组首元素开 ...

  6. jpa 多对多

    entity   Item package entity; import java.util.HashSet; import java.util.Set; import javax.persisten ...

  7. 微信公众平台开发者认证,node

    纯属分享 app.js var express = require('express'); var path = require('path'); var app = express(); ; var ...

  8. c++builder XE6 线程 tthread

    thread TThread class TSleepFunc : public TCppInterfacedObject<TProc> { public: TSleepFunc(TFor ...

  9. as3 对于加载进来多层swf缩放操作

    //swf实际尺寸 var oldWidth:Number = frameLder.contentLoaderInfo.content.width; var oldHeight:Number = fr ...

  10. Simple2D-20(重构)

    为什么重构 Simple2D 开始的时候打算使用几周的时间来实现 Simple2D 的,主要是实现一些简单的 2D 渲染功能.但是编写的过程中不满于它只能实现简单的功能,后来添加了诸如Alpha测试. ...