LeetCode 81 Search in Rotated Sorted Array II(循环有序数组中的查找问题)
//
// 1.cpp
// test
//
// Created by PengFei_Zheng on 28/03/2017.
// Copyright © 2017 PengFei_Zheng. All rights reserved.
// #include<iostream>
#include<vector>
using namespace std; class Solution {
public:
bool search(vector<int>& nums, int target) {
int len = (int)nums.size();
int left = ;
int right = len-;
while(left<=right){
int mid = (left+right)/;
if(nums[mid]==target) return true;
if(nums[left]<nums[mid]){
if(nums[left]<=target && target<nums[mid])
right=mid-;
else
left=mid+;
}
else if(nums[left]>nums[mid]){
if(nums[mid]<=target && target<nums[right])
left=mid+;
else
right=mid-;
}
else
left++;
}
return false;
}
};
C++ Solution
package leetcode_100;
/***
*
* @author pengfei_zheng
* 循环有序可重复的数组中查找数字是否存在
*/
public class Solution81 {
public static boolean search(int nums[], int key) {
int len = nums.length;
int left = 0;
int right = len - 1;
while(left<=right){// enter the loop
int mid = (left+right)/2;
if(nums[mid]==key) return true;// find the target number
if(nums[left]<nums[mid]){//
if(nums[left]<=key && key < nums[mid])
right = mid-1;
else
left = mid+1;
}
else if(nums[left]>nums[mid]){
if(nums[mid] < key && key <= nums[right])
left = mid+1;
else
right = mid -1;
}
else
left++;
}
return false;
}
}
Java Solution
LeetCode 81 Search in Rotated Sorted Array II(循环有序数组中的查找问题)的更多相关文章
- LeetCode 33 Search in Rotated Sorted Array(循环有序数组中进行查找操作)
题目链接 :https://leetcode.com/problems/search-in-rotated-sorted-array/?tab=Description Problem :当前的数组 ...
- [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 81.Search in Rotated Sorted Array II (旋转数组的搜索II) 解题思路和方法
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- [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] 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 ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- LeetCode 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- LeetCode 81.Search in Rotated Sorted Array II(M)
题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...
随机推荐
- 前端不容错过的jQuery图片滑块插件
作为前端开发者,我们会碰到很到各种各样的jQuery插件,但老实说,很少有自己写的.今天要分享的几款jQuery图片滑块插件,也就是jQuery焦点图插件,基本上会在每个网站都有应用,可以下载看看,也 ...
- iPhone 配置使用工具
“iPhone 配置实用工具”可让您轻松地创建.维护和安装配置描述文件及对配置描述文件进行加密,跟踪和安装预置描述文件与授权的应用程序,以及采集包括控制台日志在内的设备信息. http://suppo ...
- Mac mysql 解决中文乱码
Mac mysql 解决中文乱码问题 出现"???"之类的无法识别的乱码 到/etc目录下自己建一个my.cnf文件(需要最高权限,使用sudo su),然后写入内容: [clie ...
- 一键切换hosts文件
1.新建文件host.bat 2.代码 @echo off cd.>C:\Windows\System32\drivers\etc\hosts echo .本地环境 .线上测试环境 ,切换Hos ...
- SharePoint 2013 Deploy Master Page And Page Layout
2013年9月27日的一篇随笔,其实也是自己编写的部署文档,由于客户是HK的,所以描述部分是用英文. 涉及到的内容是关于SharePoint 2013如何部署自定义的母版页和布局页. First, L ...
- ul li列表元素浮动导致border没有底边解决办法
如图,当ul li,li元素浮动,并且ul元素也overflow:hidden清除浮动的时候,给li元素加了border,但是不显示底边,这时候要看是不是没有给li元素加高,因为加了border之后默 ...
- 12 go实现几中基本排序算法
include 冒泡排序 插入排序 快速排序 选择排序 这4种算法的内涵不再做解释了 github地址 冒泡排序算法 func maoPao(intSlice []int) []int { /* 冒泡 ...
- InnoDB锁问题 & DB事务隔离级别
<参考:http://www.cnblogs.com/jack204/archive/2012/06/09/2542940.html>InnoDB行锁实现方式InnoDB行锁是通过给索引上 ...
- MySQL------报错Access denied for user 'root'@'localhost' (using password:NO)解决方法
报错:Access denied for user 'root'@'localhost' (using password:NO) 原因:没有给用户“root'@'localhost”赋予数据库权限 解 ...
- GDAL------安装GDAL
1.官网下载GDAL http://www.gisinternals.com/http://www.gisinternals.com/release.php 2.下载完后,点击安装,选择安装全部组件, ...