Skip to content

Commit

Permalink
Merge pull request algorithm004-04#261 from AdTomato/master
Browse files Browse the repository at this point in the history
344-Weak 01
  • Loading branch information
Daisy3485 authored Oct 21, 2019
2 parents 6372723 + fc8af99 commit f67fb30
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Week 01/id_344/LeetCode_189_344.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public void rotate(int[] nums, int k) {
int[] nums2 = new int[nums.length];
int j = 0;
for (int i = 0; i < nums.length; i++) {
nums2[(i + k) % nums.length] = nums[i];
}
for (int i = 0; i < nums2.length; i++) {
nums[i] = nums2[i];
}
}
}
14 changes: 14 additions & 0 deletions Week 01/id_344/LeetCode_26_344.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public int removeDuplicates(int[] nums) {
int j = 0;
if (nums.length == 0) {
return j;
}
for (int i = 0; i < nums.length; i++) {
if (nums[j] != nums[i]) {
nums[++j] = nums[i];
}
}
return j + 1;
}
}
119 changes: 119 additions & 0 deletions Week 01/id_344/LeetCode_641_344.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
class MyCircularDeque {
int[] queue;
int first; // 头指针
int last; // 尾指针
int size; // 当前队列长度
int capacity; // 队列的容量

/** Initialize your data structure here. Set the size of the deque to be k. */
public MyCircularDeque(int k) {
queue = new int[k];
first = 0;
last = 0;
size = 0;
capacity = k;
}

/** Adds an item at the front of Deque. Return true if the operation is successful. */
public boolean insertFront(int value) {
if (first == last && size == capacity) {
return false;
} else {
queue[first] = value;
if (first == 0) {
first = capacity - 1;
} else {
first--;
}
size++;
return true;
}

}

/** Adds an item at the rear of Deque. Return true if the operation is successful. */
public boolean insertLast(int value) {
if (first == last && size == capacity) {
return false;
} else {
if (last == capacity - 1) {
last = 0;
} else {
last++;
}
queue[last] = value;
size++;
return true;
}
}

/** Deletes an item from the front of Deque. Return true if the operation is successful. */
public boolean deleteFront() {
if (size == 0 && first == last) {
return false;
} else {
if (first == capacity - 1) {
first = 0;
} else {
first++;
}
size--;
return true;
}
}

/** Deletes an item from the rear of Deque. Return true if the operation is successful. */
public boolean deleteLast() {
if (size == 0 && first == last) {
return false;
} else {
if (last == 0) {
last = capacity - 1;
} else {
last--;
}
size--;
return true;
}
}

/** Get the front item from the deque. */
public int getFront() {
if (size == 0 && first == last) {
return -1;
} else {
if (first == capacity - 1) {
return queue[0];
} else {
return queue[first + 1];
}
}
}

/** Get the last item from the deque. */
public int getRear() {
if (size == 0 && first == last) {
return -1;
} else {
return queue[last];
}
}

/** Checks whether the circular deque is empty or not. */
public boolean isEmpty() {
if (size == 0 && first == last) {
return true;
} else {
return false;
}
}

/** Checks whether the circular deque is full or not. */
public boolean isFull() {
if (size == capacity && first == last) {
return true;
} else {
return false;
}
}
}
50 changes: 49 additions & 1 deletion Week 01/id_344/NOTE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,52 @@
# NOTE


## 数组Array

prepend O(1)
append O(1)
lookup O(1)
insert O(n)
delete O(n)

## 链表LinkedList

prepend O(1)
append O(1)
lookup O(n)
insert O(1)
delete O(1)

**ps:因为数组的插入和删除,如果从中间插入或删除的话需要将后面的元素往前移动一位,所以数组的插入和删除操作的时间复杂度为O(n)。链表的查询操作需要一个节点一个节点的遍历,所以链表额查询操作时间复杂度为O(n)。**

## 跳表SkipList


因为链表的查询需要遍历链表才能查询到所需要查找的数据,时间复杂度为O(n)。
跳表就是通过添加索引来给链表的查询加速。

跳表查询的时间复杂度为O(logn)

## 栈Stack

先入后出FILO

insert O(1)
delete O(1)

## 队列Queue

先入先出FIFO

insert O(1)
delete O(1)

## 双端队列Deque

两端都可以进出的队列Queue

insert O(1)
delete O(1)

**各个语言的链表、栈、队列的实现可以通过直接Google的方式查询到源码。**

**Python关于这些数据类型的中文网址为:https://docs.python.org/zh-cn/3/library/collections.html#module-collections**
Binary file added Week 预习周/id_344/数据结构脑图.xmind
Binary file not shown.
Binary file added Week 预习周/id_344/算法.xmind
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit f67fb30

Please sign in to comment.