LeetCode 081 Search in Rotated Sorted Array II
2015-10-24 08:38
267 查看
题目描述
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.
代码
[code] public boolean search(int[] nums, int target) { int left = 0, right = nums.length - 1; while (left <= right) { int mid = (left + right) / 2; if (nums[mid] == target) return true; if (nums[left] < nums[mid]) { if (target <= nums[mid] && target >= nums[left]) right = mid - 1; else left = mid + 1; } else if (nums[left] > nums[mid]) { if (target >= nums[left] || target <= nums[mid]) right = mid - 1; else left = mid + 1; } else left++; } return false; }
相关文章推荐
- 运行Shell脚本的几种方式解析
- 仿微信随手指滑动页面菜单图标文字颜色渐变
- 【基础练习】【归并逆序对】codevs3324 新斯诺克题解
- Android使用应用程序资源(、颜色数组、尺寸、弦、布尔、整型)
- 课堂作业之项目目标
- android 源代码修改了之后编译生成本地的sdk,以方便在eclipse里面直接使用,调用所改动的API接口函数
- Java之判断字符串是否相等
- 日经春秋 20151024
- mod_bw下载地址
- HDU 4788 Hard Disk Drive
- BZOJ 1878 SDOI 2009 HH项链 树状数组 + 脱机处理
- 第19讲----项目2 C语言中的字符对齐
- 【日常学习】【归并逆序对】codevs1688 求逆序对题解
- 天声人語 20151024 地図上の「架空の町」
- Linux系统安装JDK后gij问题
- LeetCode 080 Remove Duplicates from Sorted Array II
- Spring事务配置的五种方式
- C++调用C函数,为什么要加extern "C"?
- zoj 2874 & poj 3308 Paratroopers (最小割)
- Android基础入门教程——8.3.3 Paint API之—— MaskFilter(面具)