Skip to content

Commit a406bca

Browse files
committed
401-Week 01
1 parent 04d2302 commit a406bca

File tree

4 files changed

+133
-1
lines changed

4 files changed

+133
-1
lines changed

Week 01/id_401/189.rotate-array.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {void} Do not return anything, modify nums in-place instead.
5+
*/
6+
var rotate = function(nums, k) {
7+
if(!nums || nums.length==0 || k<=0) return nums;
8+
for(i=0;i<k;i++) {
9+
const temp = nums.pop();
10+
nums.unshift(temp);
11+
}
12+
return nums;
13+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var removeDuplicates = function(nums) {
6+
if(!nums || nums.length<=1) return nums.length;
7+
let temp=nums[0];
8+
let i=1;
9+
while(i<nums.length){
10+
if(temp===nums[i]){
11+
nums.splice(i,1);
12+
}
13+
else{
14+
temp=nums[i];
15+
i++;
16+
}
17+
}
18+
return nums.length;
19+
};
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* Initialize your data structure here. Set the size of the deque to be k.
3+
* @param {number} k
4+
*/
5+
var MyCircularDeque = function(k) {
6+
this.size=k;
7+
this.data=[];
8+
};
9+
10+
/**
11+
* Adds an item at the front of Deque. Return true if the operation is successful.
12+
* @param {number} value
13+
* @return {boolean}
14+
*/
15+
MyCircularDeque.prototype.insertFront = function(value) {
16+
if(this.isFull())return false;
17+
this.data.unshift(value);
18+
return true;
19+
};
20+
21+
/**
22+
* Adds an item at the rear of Deque. Return true if the operation is successful.
23+
* @param {number} value
24+
* @return {boolean}
25+
*/
26+
MyCircularDeque.prototype.insertLast = function(value) {
27+
if(this.isFull())return false;
28+
this.data.push(value)
29+
return true;
30+
};
31+
32+
/**
33+
* Deletes an item from the front of Deque. Return true if the operation is successful.
34+
* @return {boolean}
35+
*/
36+
MyCircularDeque.prototype.deleteFront = function() {
37+
if(this.isEmpty())return false;
38+
this.data.shift();
39+
return true;
40+
};
41+
42+
/**
43+
* Deletes an item from the rear of Deque. Return true if the operation is successful.
44+
* @return {boolean}
45+
*/
46+
MyCircularDeque.prototype.deleteLast = function() {
47+
if(this.isEmpty())return false;
48+
this.data.pop();
49+
return true;
50+
};
51+
52+
/**
53+
* Get the front item from the deque.
54+
* @return {number}
55+
*/
56+
MyCircularDeque.prototype.getFront = function() {
57+
if(this.data.length===0) return -1;
58+
return this.data[0];
59+
};
60+
61+
/**
62+
* Get the last item from the deque.
63+
* @return {number}
64+
*/
65+
MyCircularDeque.prototype.getRear = function() {
66+
let length=this.data.length;
67+
if(length===0)return -1;
68+
return this.data[length-1];
69+
};
70+
71+
/**
72+
* Checks whether the circular deque is empty or not.
73+
* @return {boolean}
74+
*/
75+
MyCircularDeque.prototype.isEmpty = function() {
76+
return this.data.length===0;
77+
};
78+
79+
/**
80+
* Checks whether the circular deque is full or not.
81+
* @return {boolean}
82+
*/
83+
MyCircularDeque.prototype.isFull = function() {
84+
return this.data.length===this.size;
85+
};
86+
87+
/**
88+
* Your MyCircularDeque object will be instantiated and called as such:
89+
* var obj = new MyCircularDeque(k)
90+
* var param_1 = obj.insertFront(value)
91+
* var param_2 = obj.insertLast(value)
92+
* var param_3 = obj.deleteFront()
93+
* var param_4 = obj.deleteLast()
94+
* var param_5 = obj.getFront()
95+
* var param_6 = obj.getRear()
96+
* var param_7 = obj.isEmpty()
97+
* var param_8 = obj.isFull()
98+
*/

Week 01/id_401/NOTE.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# NOTE
22

3-
3+
一直对算法很有兴趣,总是坚持不下来
4+
5+
现在工作特别忙,心理时时惦记着要来刷题,希望可以坚持下去
46

0 commit comments

Comments
 (0)