forked from MisterBooo/LeetCodeAnimation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae6f9cd
commit bbd0d2b
Showing
15 changed files
with
427 additions
and
0 deletions.
There are no files selected for viewing
Binary file added
BIN
+6.52 MB
...nd-last-position-of-element-in-sorted-array/Animation/在排序数组中查找元素的第一个和最后一个位置.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
81 changes: 81 additions & 0 deletions
81
...d-array/Article/0034-find-first-and-last-position-of-element-in-sorted-array.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# LeetCode 第 34 号问题:在排序数组中查找元素的第一个和最后一个位置 | ||
|
||
|
||
题目来源于 LeetCode 上第 34 号问题:find-first-and-last-position-of-element-in-sorted-array。题目难度为 中等。 | ||
|
||
### 题目描述 | ||
|
||
给定一个按照升序排列的整数数组 **nums**,和一个目标值 **target**。找出给定目标值在数组中的开始位置和结束位置。 | ||
|
||
你的算法时间复杂度必须是 **O(log n)** 级别。 | ||
|
||
如果数组中不存在目标值,返回 [-1, -1]。 | ||
|
||
|
||
**示例:** | ||
|
||
``` | ||
输入: nums = [5,7,7,8,8,10], target = 8 | ||
输出: [3,4] | ||
``` | ||
``` | ||
输入: nums = [5,7,7,8,8,10], target = 6 | ||
输出: [-1,-1] | ||
``` | ||
### 题目解析 | ||
|
||
题目中要求了时间复杂度为O(log n),这就很清楚要使用二分查找法了。 | ||
|
||
首先定义两个指针变量,分别存储左右两个位置的索引。首先去找目标值的最左面的索引,通过循环为了防止元素丢失,每次保留最右面的元素,左侧的指针移动时+1。在循环结束的时候判断一下数组中是否包括目标值,不包括的话直接退出。 | ||
右面的跟左侧相同,只不过正好相反。 | ||
|
||
|
||
|
||
### 动画描述 | ||
|
||
![](..\Animation\在排序数组中查找元素的第一个和最后一个位置.gif) | ||
|
||
### 代码实现 | ||
|
||
```java | ||
// 34. 下一个排列 | ||
// https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/ | ||
// 时间复杂度:O(n) | ||
// 空间复杂度:O(1) | ||
class Solution { | ||
public int[] searchRange(int[] nums, int target) { | ||
int[] res = new int[] { -1, -1 }; | ||
int left = 0; | ||
int right = nums.length - 1; | ||
int l = left; | ||
int r = right; | ||
while (left < right) { | ||
int mid = (left + right) / 2; | ||
if (nums[mid] < target) { | ||
left = mid + 1; | ||
} else { | ||
right = mid; | ||
} | ||
} | ||
if (left>right||nums[left]!=target) { | ||
return new int[]{-1,-1}; | ||
} | ||
while (l < r) { | ||
int mid = (l + r) / 2 + 1; | ||
if (nums[mid] > target) { | ||
r = mid - 1; | ||
} else { | ||
l = mid; | ||
} | ||
} | ||
if (left > right || left > r) { | ||
return new int[] { -1, -1 }; | ||
} else { | ||
return new int[] { left, r }; | ||
} | ||
} | ||
} | ||
|
||
``` | ||
|
||
![](../../Pictures/qrcode.jpg) |
33 changes: 33 additions & 0 deletions
33
0034-find-first-and-last-position-of-element-in-sorted-array/Code/1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class Solution { | ||
public int[] searchRange(int[] nums, int target) { | ||
int[] res = new int[] { -1, -1 }; | ||
int left = 0; | ||
int right = nums.length - 1; | ||
int l = left; | ||
int r = right; | ||
while (left < right) { | ||
int mid = (left + right) / 2; | ||
if (nums[mid] < target) { | ||
left = mid + 1; | ||
} else { | ||
right = mid; | ||
} | ||
} | ||
if (left>right||nums[left]!=target) { | ||
return new int[]{-1,-1}; | ||
} | ||
while (l < r) { | ||
int mid = (l + r) / 2 + 1; | ||
if (nums[mid] > target) { | ||
r = mid - 1; | ||
} else { | ||
l = mid; | ||
} | ||
} | ||
if (left > right || left > r) { | ||
return new int[] { -1, -1 }; | ||
} else { | ||
return new int[] { left, r }; | ||
} | ||
} | ||
} |
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
113 changes: 113 additions & 0 deletions
113
0035-search-insert-position/Article/0035-search-insert-position.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# LeetCode 第 35 号问题:搜索插入位置 | ||
|
||
> 本文首发于公众号「图解面试算法」,是 [图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一。 | ||
> | ||
> 同步博客:https://www.algomooc.com | ||
|
||
题目来源于 LeetCode 第 35 号问题:搜索插入位置. | ||
|
||
## 题目 | ||
|
||
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。 | ||
你可以假设数组中无重复元素。 | ||
|
||
|
||
示例 1: | ||
|
||
``` | ||
输入: [1,3,5,6], 5 | ||
输出: 2 | ||
``` | ||
|
||
示例 2: | ||
|
||
|
||
``` | ||
输入: [1,3,5,6], 2 | ||
输出: 1 | ||
``` | ||
|
||
示例 3: | ||
|
||
|
||
``` | ||
输入: [1,3,5,6], 7 | ||
输出: 4 | ||
``` | ||
|
||
|
||
示例 4: | ||
|
||
|
||
``` | ||
输入: [1,3,5,6], 0 | ||
输出: 0 | ||
``` | ||
|
||
|
||
|
||
## 思路解析 | ||
|
||
### 暴力循环法 | ||
|
||
这个题看起来就是很简单的,就是一道考验查找算法的题目。最简单的就是暴力查找了。 | ||
|
||
#### 思路 | ||
|
||
遍历这个数组,然后如果当前值和目标值target一致或小于目标值target,那么就return 当前下标。这种解法的时间复杂度是O(N) | ||
|
||
### 动画理解 | ||
|
||
![](../Animation/暴力查找.gif) | ||
|
||
#### 代码实现 | ||
|
||
|
||
```java | ||
//时间复杂度:O(n) | ||
//空间复杂度:O(1) | ||
class Solution { | ||
public int searchInsert(int[] nums, int target) { | ||
int i=0; | ||
for(;i<nums.length;i++){ | ||
if (nums[i]>=target){ | ||
break; | ||
} | ||
} | ||
return i; | ||
} | ||
} | ||
``` | ||
|
||
### 二分法 | ||
|
||
#### 思路 | ||
|
||
除了暴力法,我们在排序数组中查找值还可以用的一种方法是二分法,思路还是和改良的暴力循环法一样,先找到左右边界,然后计算,每次可以省出一半的时间。时间复杂度为O(logn) | ||
|
||
#### 代码实现 | ||
|
||
```java | ||
//时间复杂度:O(lon(n)) | ||
//空间复杂度:O(1) | ||
class Solution { | ||
public int searchInsert(int[] nums, int target) { | ||
if (target>nums[nums.length-1]) { | ||
return nums.length; | ||
} | ||
int left=0; | ||
int right=nums.length-1; | ||
while (left < right) { | ||
int mid = (left + right) / 2; | ||
if (nums[mid] < target) { | ||
left = mid + 1; | ||
} else { | ||
right = mid; | ||
} | ||
} | ||
return left; | ||
|
||
} | ||
} | ||
``` | ||
![](../../Pictures/qrcode.jpg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Solution1 { | ||
public int searchInsert(int[] nums, int target) { | ||
int i=0; | ||
for(;i<nums.length;i++){ | ||
if (nums[i]>=target){ | ||
break; | ||
} | ||
} | ||
return i; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//时间复杂度:O(lon(n)) | ||
//空间复杂度:O(1) | ||
class Solution2 { | ||
public int searchInsert(int[] nums, int target) { | ||
if (target>nums[nums.length-1]) { | ||
return nums.length; | ||
} | ||
int left=0; | ||
int right=nums.length-1; | ||
while (left < right) { | ||
int mid = (left + right) / 2; | ||
if (nums[mid] < target) { | ||
left = mid + 1; | ||
} else { | ||
right = mid; | ||
} | ||
} | ||
return left; | ||
|
||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Oops, something went wrong.