81:搜索旋转排序数组 II
This commit is contained in:
parent
efb142b920
commit
14a0535ee0
@ -0,0 +1,67 @@
|
|||||||
|
//已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同。
|
||||||
|
//
|
||||||
|
// 在传递给函数之前,nums 在预先未知的某个下标 k(0 <= k < nums.length)上进行了 旋转 ,使数组变为 [nums[k], nums
|
||||||
|
//[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]](下标 从 0 开始 计数)。例如, [0,1,
|
||||||
|
//2,4,4,4,5,6,6,7] 在下标 5 处经旋转后可能变为 [4,5,6,6,7,0,1,2,4,4] 。
|
||||||
|
//
|
||||||
|
// 给你 旋转后 的数组 nums 和一个整数 target ,请你编写一个函数来判断给定的目标值是否存在于数组中。如果 nums 中存在这个目标值 targ
|
||||||
|
//et ,则返回 true ,否则返回 false 。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [2,5,6,0,0,1,2], target = 0
|
||||||
|
//输出:true
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [2,5,6,0,0,1,2], target = 3
|
||||||
|
//输出:false
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 1 <= nums.length <= 5000
|
||||||
|
// -104 <= nums[i] <= 104
|
||||||
|
// 题目数据保证 nums 在预先未知的某个下标上进行了旋转
|
||||||
|
// -104 <= target <= 104
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 进阶:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 这是 搜索旋转排序数组 的延伸题目,本题中的 nums 可能包含重复元素。
|
||||||
|
// 这会影响到程序的时间复杂度吗?会有怎样的影响,为什么?
|
||||||
|
//
|
||||||
|
// Related Topics 数组 二分查找
|
||||||
|
// 👍 367 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
//81:搜索旋转排序数组 II
|
||||||
|
public class SearchInRotatedSortedArrayIi{
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new SearchInRotatedSortedArrayIi().new Solution();
|
||||||
|
}
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public boolean search(int[] nums, int target) {
|
||||||
|
return Arrays.stream(nums).boxed().collect(Collectors.toList()).contains(target);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
<p>已知存在一个按非降序排列的整数数组 <code>nums</code> ,数组中的值不必互不相同。</p>
|
||||||
|
|
||||||
|
<p>在传递给函数之前,<code>nums</code> 在预先未知的某个下标 <code>k</code>(<code>0 <= k < nums.length</code>)上进行了 <strong>旋转 </strong>,使数组变为 <code>[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]</code>(下标 <strong>从 0 开始</strong> 计数)。例如, <code>[0,1,2,4,4,4,5,6,6,7]</code> 在下标 <code>5</code> 处经旋转后可能变为 <code>[4,5,6,6,7,0,1,2,4,4]</code> 。</p>
|
||||||
|
|
||||||
|
<p>给你 <strong>旋转后</strong> 的数组 <code>nums</code> 和一个整数 <code>target</code> ,请你编写一个函数来判断给定的目标值是否存在于数组中。如果 <code>nums</code> 中存在这个目标值 <code>target</code> ,则返回 <code>true</code> ,否则返回 <code>false</code> 。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [2<code>,5,6,0,0,1,2]</code>, target = 0
|
||||||
|
<strong>输出:</strong>true
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [2<code>,5,6,0,0,1,2]</code>, target = 3
|
||||||
|
<strong>输出:</strong>false</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= nums.length <= 5000</code></li>
|
||||||
|
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
|
||||||
|
<li>题目数据保证 <code>nums</code> 在预先未知的某个下标上进行了旋转</li>
|
||||||
|
<li><code>-10<sup>4</sup> <= target <= 10<sup>4</sup></code></li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>进阶:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>这是 <a href="https://leetcode-cn.com/problems/search-in-rotated-sorted-array/description/">搜索旋转排序数组</a> 的延伸题目,本题中的 <code>nums</code> 可能包含重复元素。</li>
|
||||||
|
<li>这会影响到程序的时间复杂度吗?会有怎样的影响,为什么?</li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>数组</li><li>二分查找</li></div></div>\n<div><li>👍 367</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user