forked from algorithm004-04/algorithm004-04
-
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.
Merge pull request algorithm004-04#261 from AdTomato/master
344-Weak 01
- Loading branch information
Showing
8 changed files
with
194 additions
and
1 deletion.
There are no files selected for viewing
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,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]; | ||
} | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -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 not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.