Skip to content

Commit

Permalink
0034、0035、0036 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterBooo committed May 6, 2020
1 parent ae6f9cd commit bbd0d2b
Show file tree
Hide file tree
Showing 15 changed files with 427 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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)
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 0035-search-insert-position/Article/0035-search-insert-position.md
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)
11 changes: 11 additions & 0 deletions 0035-search-insert-position/Code/1.java
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;
}
}
21 changes: 21 additions & 0 deletions 0035-search-insert-position/Code/2.java
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;

}
}
Binary file added 0036-valid-sudoku/Animation/HashMap.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 0036-valid-sudoku/Animation/HashMap.mp4
Binary file not shown.
Loading

0 comments on commit bbd0d2b

Please sign in to comment.