Jan 23 - Search In Rotated Array II; Array; Binary Search;

public class Solution {
    public boolean search(int[] nums, int target) {
        int len = nums.length;
        if(len == 0) return false;
        int low = 0;
        int high = len-1;
        while(low < high){
            int mid = (low+high)/2; // do not concern the integer overrange situation
            if(nums[mid] == target) return true;
            if(nums[mid] < nums[high] || nums[mid] < nums[low]){
                if(target > nums[mid] && target <= nums[high]) low = mid + 1;
                else high = mid - 1;
            }
            else if(nums[mid] > nums[low] || nums[mid] > nums[high]){
                if(target < nums[mid] && target >= nums[low]) high = mid - 1;
                else low = mid + 1;
            }
            else high--;
        }
        if(nums[low] == target) return true;
        return false;
}
}

Initially low = 0, high = len-1; the condition of while loop is low < high; mid = (low+high)/2;

if nums[mid] == target, sure we find the value same as the target, return true. If not,

We analyze three cases:

1. right part from mid to high sorted, and left part from low to mid unsorted. (nums[mid] < nums[high] || nums[mid] < nums[low])

  Then we compare the target with some specific value to detemine which part the target should be in if its in the array.

   if(target > nums[mid] && target <= nums[high]) target should be in the right part, set low = mid +1;

   otherwise the target should be in the left part, high = mid - 1;

2. left part is sorted. (nums[mid] > nums[low] || nums[mid] > nums[high])

  Similarly, if(target< nums[mid] && target >= nums[low]) high = mid -1;

otherwise, the target should be in the right part, low = mid + 1;

3. The last possibility is that nums[low] == nums[mid] == nums[high], in this case, just let high--; to reduce the duplication occurance.

时间: 2024-10-13 10:29:16

Jan 23 - Search In Rotated Array II; Array; Binary Search;的相关文章

[LeetCode] 33. Search in Rotated Sorted Array_Medium tag: Binary Search

Suppose an array sorted in ascending order 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.

LeetCode OJ - Convert Sorted Array/List to Binary Search Tree

虚函数使用的时机 为什么虚函数不总是适用? 1. 虚函数有事会带来很大的消耗: 2. 虚函数不总是提供所需的行为: 3. 当我们不考虑继承当前类时,不必使用虚函数. 必须使用虚函数的情况: 1. 当你想删除一个表面上指向基类对象,实际却是指向派生类对象的指针,就需要虚析构函数. LeetCode OJ - Convert Sorted Array/List to Binary Search Tree,布布扣,bubuko.com LeetCode OJ - Convert Sorted Arra

108.&#160;Convert Sorted Array to balanced Binary Search Tree

108. Convert Sorted Array to balanced Binary Search Tree The tricky part is the base case . Write induction part first and then test arrays of different size, 0, 1,2, 3 And finalize the base case /** * Definition for a binary tree node. * public clas

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] &lt;c++&gt;

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数大于两次,删除多余的复制,返回删除后数组长度,要求不另开内存空间. C++ 献上自己丑陋无比的代码.相当于自己实现一个带计数器的unique函数 class Solution { public: int removeDuplicates(std::vector<int>& nums) {

81. Search in Rotated Sorted Array II (Array; Divide-and-Conquer)

Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 思路:此时可能存在nums[start]=nums[end]或者nums[start]=n

Convert Sorted Array to Binary Search Tree &amp; Convert Sorted List to Binary Search Tree

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 方法:将有序数组转为平衡二叉树,我们知道平衡二叉树的中序遍历是有序数组,而且“中间”的元素可以作为根节点,那么构建树的时候,找出中间元素,构造根元素,然后继续重复上面的方法,构造左子树和右子树.代码如下: 1 /**

[Leetcode][JAVA] Convert Sorted Array to Binary Search Tree &amp;&amp; Convert Sorted List to Binary Search Tree

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 很简单的二分法,只要给出Array的开始和结束下标作为参数传入即可. 1 public TreeNode sortedArrayToBST(int[] num) { 2 return constructBST(num,

154. Find Minimum in Rotated Sorted Array II (Array; Divide-and-Conquer)

Suppose an array sorted in ascending order 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). Find the minimum element. The array may contain duplicates. 思路:有重复元素的时候,不能按I中这么分三类(反例:Input: [3,3,1,3]=>将

Convert Sorted Array to Balanced Binary Search Tree

题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST 解答: 1 public class Solution { 2 3 public static void main(String[] args) { 4 int[] num = {1,2,3,4,5}; 5 6 TreeNode root = sortedArrayToBST(num); 7 8