【leetcode刷题笔记】Search in Rotated Sorted Array II
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.
题解:如果没有重复的元素,那么就可以根据target是否在某一半而扔掉另外一半。但是如果有重复的元素,就有可能不知道往哪边跳转:
例如A = {1,3,3,3,3,3},经过变换后得到数组{3,1,3,3,3,3},此时A[mid] = 3 = A[left] = A[right],如果target = 1,两边都不能扔,所以不能用二分的方法。
直接用遍历O(n)的方法也可以AC:
public class Solution {
public boolean search(int[] A, int target) {
if(A == null || A.length == 0)
return false;
for(int i = 0;i < A.length;i++)
if(target == A[i])
return true;
return false;
}
}
【leetcode刷题笔记】Search in Rotated Sorted Array II的更多相关文章
- 刷题33. Search in Rotated Sorted Array
一.题目说明 这个题目是33. Search in Rotated Sorted Array,说的是在一个"扭转"的有序列表中,查找一个元素,时间复杂度O(logn). 二.我的解 ...
- LeetCode 新题: Find Minimum in Rotated Sorted Array II 解题报告-二分法模板解法
Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...
- LeetCode 新题: Find Minimum in Rotated Sorted Array 解题报告-二分法模板解法
Find Minimum in Rotated Sorted Array Question Solution Suppose a sorted array is rotated at some piv ...
- LeetCode: Search in Rotated Sorted Array II 解题报告
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...
- [LeetCode] Search in Rotated Sorted Array II [36]
称号 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...
- LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- 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 ...
- LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 【leetcode】Search in Rotated Sorted Array II
Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...
- 【LeetCode】81. Search in Rotated Sorted Array II (2 solutions)
Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...
随机推荐
- NSMutableAttributedString 的使用方法,设置格式
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:title]; NS ...
- Delphi里J+开关作用类似C语言的static变量
從前筆者曾經對以下的程式產生過疑惑:{$J+}procedure TForm1.Button1Click(Sender: TObject);const VarConst: integer = 4;b ...
- unity4.6 Beta版 UI控件之Button
近期需求,须要用到4.6版本号uGui了,所以抽时间来学习学习,就UI控件在Unity工具里创建预设这块来说相比較于NGUI,我认为是没有什么太大的差别的. 比方:Canvas--Camera . T ...
- Python script to create Screen from all Items/Graphs of a host
#!/usr/bin/env python import urllib2 import json import argparse def authenticate(url, username, pas ...
- jquery的json的遍历
jquery遍历解析json对象1: var json = [{dd:'SB',AA:'东东',re1:123},{cccc:'dd',lk:'1qw'}]; for(var i=0,l=json.l ...
- lua学习笔记(十三)
math库 定义在math中 所有三角函数都使用弧度 指数和对数函数 取整函数 伪随机数math.random 调用时没有参数返回0~1之间的随 ...
- java中已经排序的列表中插入新值
static List<Integer> insertSortedList(){ List<Integer> nums = new ArrayList<Integer&g ...
- mysql数据库去除重复数据
(一)最原始的方法: delete from test where id not in (select * from ((select min(id) from test group by(name) ...
- Tomcat9源码分析:BootStrap
概览 BootStrap源码所在的位置是:org.apache.catalina.startup.Bootstrap 这个类是Tomcat项目的启动类,也就是main函数所在的地方,起始tomcat就 ...
- win10 下eclipse tomcat 热部署问题?
前言: 问题的描述: 用的环境是maven,java,tomcat,win10 tomcat server配置如下 项目发布之后,修改jsp,报错,错误详情如下: 解决办法.勾选server opti ...