Skip to content

Commit

Permalink
Solved @zjming
Browse files Browse the repository at this point in the history
  • Loading branch information
zjming committed Apr 19, 2020
0 parents commit 5bf978c
Show file tree
Hide file tree
Showing 21 changed files with 548 additions and 0 deletions.
Binary file added 0155-min-stack/Animation/1.mp4
Binary file not shown.
Binary file added 0155-min-stack/Animation/Animation.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 77 additions & 0 deletions 0155-min-stack/Article/0155-min-stack
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#### 题目描述

> 设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。
>
> + push(x) —— 将元素 x 推入栈中。
> + pop() —— 删除栈顶的元素。
> + top() —— 获取栈顶元素。
> + getMin() —— 检索栈中的最小元素。

```java
示例:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-5);
minStack.push(1)
minStack.getMin(); --> 返回 -5.
minStack.pop();
minStack.top(); --> 返回 -5.
minStack.getMin(); --> 返回 -5.
```

#### 题目解析

为了能在常数时间内检测到栈中的最小元素,我们可以通过"空间换时间"的方式进行实现,为栈本身(数据栈\_data)增加一个辅助栈(最小值栈\_min)。每一次元素入 \_data 栈,则在 \_min 栈中增加对应的最小值;当 \_data 栈中的元素出栈,则 \_min 栈也进行出栈操作

#### 动画理解

![](../Animation/Animation.gif)

#### 代码实现

```java
class MinStack {

private Stack<Integer> _data;
private Stack<Integer> _min;

/** initialize your data structure here. */
public MinStack() {
_data = new Stack<>();
_min = new Stack<>();
}

public void push(int x) {
_data.add(x);
if (_min.isEmpty()){
_min.push(x);
}
else{
if (x > _min.peek()){
x = _min.peek();
}
_min.push(x);
}
}

public void pop() {
_data.pop();
_min.pop();
}

public int top() {
return _data.peek();
}

public int getMin() {
return _min.peek();
}
}
```

#### 复杂度分析

+ 时间复杂度:O(1)。
+ 空间复杂度:O(n)。

37 changes: 37 additions & 0 deletions 0155-min-stack/Code/1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class MinStack {

private Stack<Integer> _data;
private Stack<Integer> _min;

/** initialize your data structure here. */
public MinStack() {
_data = new Stack<>();
_min = new Stack<>();
}

public void push(int x) {
_data.add(x);
if (_min.isEmpty()){
_min.push(x);
}
else{
if (x > _min.peek()){
x = _min.peek();
}
_min.push(x);
}
}

public void pop() {
_data.pop();
_min.pop();
}

public int top() {
return _data.peek();
}

public int getMin() {
return _min.peek();
}
}
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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# LeetCode 第 215 号问题:数组中的第K个最大元素

> 本文首发于公众号「图解面试算法」,是 [图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一。
>
> 同步博客:https://www.algomooc.com

题目来源于 LeetCode 上第 215 号问题:数组中的第K个最大元素。题目难度为 Medium,目前通过率为 62.0% 。

#### 题目描述

> 在未排序的数组中找到第 **k** 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。
>

```java
示例1:
输入: [3,2,1,5,6,4] 和 k = 2
输出: 5
示例2:
输入: [3,2,3,1,2,4,5,5,6] 和 k = 4
输出: 4
```

#### 题目解析

维护一个K大小的最小堆,堆中元素个数小于K时,新元素直接进入堆中;否则,当堆顶小于新元素时,弹出堆顶,将新元素加入堆。

由于堆是最小堆,堆顶是堆中的最小元素,新元素都会保证比堆顶小(否则新元素替换堆顶),故堆中K个元素是已扫描元素里最大的K个;堆顶元素即为第K大数。

#### 动画理解

![](../Animation/Animation.gif)

#### 代码实现

Java语言

```java
class Solution {
public int findKthLargest(int[] nums, int k) {
// // 创建一个小顶堆(优先队列模拟)
PriorityQueue<Integer> heap =
new PriorityQueue<Integer>();

// 在堆中维护当前最大k个元素
for (int i = 0; i < nums.length; i++){
if(heap.size() < k){
heap.add(nums[i]);
}else if (heap.element() < nums[i]){
heap.poll();
heap.add(nums[i]);
}
}
return heap.poll();
}
}
```

C++语言实现

```c++
#include <queue>

class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
priority_queue<int, vector<int>, greater<int> > Q;
for(int i = 0; i < nums.size(); i++){
if(Q.size() < k){
Q.push(nums[i]);
}
else if(Q.top() < nums[i]){
Q.pop();
Q.push(nums[i]);
}
}

return Q.top();
}
};
```

#### 复杂度分析

+ 时间复杂度:向大小为 k 的堆中添加元素的时间复杂度为O(logK),重复该操作 N 次,故总时间复杂度为 O(NlogK)
+ 空间复杂度:O(K)



19 changes: 19 additions & 0 deletions 0215-Kth-Largest-Element-in-an-Array/Code/1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <queue>

class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
priority_queue<int, vector<int>, greater<int> > Q;
for(int i = 0; i < nums.size(); i++){
if(Q.size() < k){
Q.push(nums[i]);
}
else if(Q.top() < nums[i]){
Q.pop();
Q.push(nums[i]);
}
}

return Q.top();
}
};
18 changes: 18 additions & 0 deletions 0215-Kth-Largest-Element-in-an-Array/Code/1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public int findKthLargest(int[] nums, int k) {
// // 创建一个小顶堆(优先队列模拟)
PriorityQueue<Integer> heap =
new PriorityQueue<Integer>();

// 在堆中维护当前最大k个元素
for (int i = 0; i < nums.length; i++){
if(heap.size() < k){
heap.add(nums[i]);
}else if (heap.element() < nums[i]){
heap.poll();
heap.add(nums[i]);
}
}
return heap.poll();
}
}
Binary file added 0290-Word-Pattern/Animation/1.mp4
Binary file not shown.
Binary file added 0290-Word-Pattern/Animation/Animation.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
87 changes: 87 additions & 0 deletions 0290-Word-Pattern/Article/0290-Word-Pattern
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# LeetCode 第 290 号问题:单词规律

> 本文首发于公众号「图解面试算法」,是 [图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一。
>
> 同步博客:https://www.algomooc.com

题目来源于 LeetCode 上第 290 号问题:单词规律。题目难度为 Easy,目前通过率为 42.4% 。

#### 题目描述

> 给定一种规律 `pattern` 和一个字符串 `str` ,判断 `str` 是否遵循相同的规律。
>
> 这里的 遵循 指完全匹配,例如, pattern 里的每个字母和字符串 str 中的每个非空单词之间存在着双向连接的对应规律。
>
> 说明:你可以假设 `pattern` 只包含小写字母, `str` 包含了由单个空格分隔的小写字母。

```java
示例1:
输入: pattern = "abba", str = "dog cat cat dog"
输出: true
示例2:
输入:pattern = "abba", str = "dog cat cat fish"
输出: false
示例3:
输入: pattern = "aaaa", str = "dog cat cat dog"
输出: false
示例4:
输入: pattern = "abba", str = "dog dog dog dog"
输出: false
```

#### 题目解析

这道题目主要考察哈希表和字符串的内容。可以将题目拆解为下面三步:

1. 设置`pattern`字符到单词(字符串 `str`)的**映射(哈希)**,使用`HashMap()`存储;使用`HashSet()` 记录被使用过的单词 。
2. 若单词个数和`pattern`字符个数不匹配,返回false;
3. 遍历`pattern`,同时**对应的**向前移动 `str` 中单词的指针,每次拆分出`pattern`中的一个字符, **判断:**
+ 如果该字符**从未出现**在哈希表中:
+ 如果该字符对应的单词已被使用过 ,即`HashSet()`中包含该字符对应的单词,则返回false;
+ 将该字符与其对应的单词**做映射**,加入哈希表中;**标记**该字符指向的单词**为已使用**,并加入`HashSet()`;
+ 如果该字符在哈希表的**映射单词**与当前指向的单词不同,则返回false;

#### 动画理解

![](../Animation/Animation.gif)

#### 代码实现

Java语言

```java
class Solution {
public boolean wordPattern(String pattern, String str) {
HashMap<Character, String> map = new HashMap<>();
HashSet<String> set = new HashSet<>();
String[] array = str.split(" ");

if (pattern.length() != array.length) {
return false;
}
for (int i = 0; i < pattern.length(); i++) {
char key = pattern.charAt(i);
if (!map.containsKey(key)) {
if (set.contains(array[i])) {
return false;
}
map.put(key, array[i]);
set.add(array[i]);
} else {
if (!map.get(key).equals(array[i])) {
return false;
}
}
}
return true;
}
}
```

#### 复杂度分析

+ 时间复杂度:遍历`pattren` 一遍,时间复杂度O(n)
+ 空间复杂度:需要申请`HashMap()` 与 `HashSet()` ,还需要将字符串 `str` 分割后存储起来,空间复杂度O(n)



26 changes: 26 additions & 0 deletions 0290-Word-Pattern/Code/1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public boolean wordPattern(String pattern, String str) {
HashMap<Character, String> map = new HashMap<>();
HashSet<String> set = new HashSet<>();
String[] array = str.split(" ");

if (pattern.length() != array.length) {
return false;
}
for (int i = 0; i < pattern.length(); i++) {
char key = pattern.charAt(i);
if (!map.containsKey(key)) {
if (set.contains(array[i])) {
return false;
}
map.put(key, array[i]);
set.add(array[i]);
} else {
if (!map.get(key).equals(array[i])) {
return false;
}
}
}
return true;
}
}
Binary file added 0394-Decode-String/Animation/1.mp4
Binary file not shown.
Binary file added 0394-Decode-String/Animation/Animation.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 5bf978c

Please sign in to comment.