Skip to content

Commit eb96665

Browse files
authored
Create week01.js
1 parent 0fd6bbe commit eb96665

1 file changed

Lines changed: 255 additions & 0 deletions

File tree

Week01/week01.js

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
// 加一(谷歌、字节跳动、Facebook 在半年内面试中考过)
2+
var plusOne = function(digits) {
3+
for(let i = digits.length - 1;i >= 0;i--){
4+
if(digits[i] == 9){
5+
digits[i] = 0;
6+
}else{
7+
digits[i]++;
8+
return digits;
9+
}
10+
}
11+
digits.unshift(1);
12+
return digits;
13+
};
14+
15+
// 零移动
16+
var moveZeroes = function(nums) {
17+
// let i = 0, j = 0;
18+
// for (; i < nums.length; i++) {
19+
// if (nums[i] !== 0) {
20+
// let temp = nums[j];
21+
// nums[j] = nums[i];
22+
// nums[i] = temp;
23+
// j++;
24+
// }
25+
// }
26+
// let j = nums.length;
27+
for (var j = nums.length; j--;) {
28+
if (nums[j] === 0) {
29+
nums.splice(j, 1);
30+
nums.push(0);
31+
}
32+
}
33+
};
34+
35+
// 两数之和
36+
var twoSum = function(nums, target) {
37+
let temArr = [];
38+
for(var i=0;i<nums.length;i++){
39+
var dif = target - nums[i];
40+
if(temArr[dif] != undefined){
41+
return [temArr[dif],i];
42+
}
43+
temArr[nums[i]] = i;
44+
}
45+
};
46+
47+
// 合并两个有序数组(Facebook 在半年内面试常考)
48+
var merge = function(nums1, m, nums2, n) {
49+
for (var i = 0;i<nums2.length;i++) {
50+
nums1[m+i] = nums2[i];
51+
}
52+
nums1.sort(function (a,b) {
53+
return a-b
54+
})
55+
};
56+
57+
// 合并两个有序链表 1->2->4, 1->3->4
58+
var mergeTwoLists = function(l1, l2) {
59+
let pHead = null;
60+
let pHead1 = l1; // 此处l1是指1->2->4中的1 还是 1->2->4 ??
61+
let pHead2 = l2;
62+
let p1 = l1;
63+
let p2 = l2;
64+
let p = null;
65+
if (!l1) {
66+
return l2;
67+
} else if (!l2) {
68+
return l1;
69+
}
70+
71+
if (l1.val < l2.val) {
72+
pHead = p1;
73+
p = p1;
74+
p1 = p1.next;
75+
} else {
76+
pHead = p2;
77+
p = p2;
78+
p2 = p2.next;
79+
}
80+
81+
while (p1 && p2) {
82+
if (p1.val < p2.val) {
83+
p.next = p1;
84+
p = p.next;
85+
p1 = p1.next;
86+
} else {
87+
p.next = p2;
88+
p = p.next;
89+
p2 = p2.next;
90+
}
91+
}
92+
93+
if (p1) {
94+
p.next = p1;
95+
} else if (p2) {
96+
p.next = p2;
97+
}
98+
99+
return pHead;
100+
};
101+
102+
103+
// 旋转数组
104+
var rotate = function(nums, k) {
105+
nums.unshift(...nums.splice(nums.length - k));
106+
};
107+
108+
// 删除排序数组中的重复项(Facebook、字节跳动、微软在半年内面试中考过)
109+
var removeDuplicates = function(nums) {
110+
let index = 0;
111+
for(let i = 0; i< nums.length; i++) {
112+
if( nums[i] != nums[i+1] ) {
113+
nums[index] = nums[i]
114+
index++
115+
}
116+
}
117+
return index
118+
};
119+
120+
// 双端循环队列 (还需继续加强)
121+
/**
122+
* Initialize your data structure here. Set the size of the deque to be k.
123+
* @param {number} k
124+
*/
125+
var MyCircularDeque = function(k) {
126+
this.arr = [];
127+
this.k = k;
128+
};
129+
130+
/**
131+
* Adds an item at the front of Deque. Return true if the operation is successful.
132+
* @param {number} value
133+
* @return {boolean}
134+
*/
135+
MyCircularDeque.prototype.insertFront = function(value) {
136+
if (this.isEmpty()) {
137+
this.arr[0] = value;
138+
return true;
139+
} else if (this.isFull()) {
140+
return false;
141+
} else {
142+
this.arr.unshift(value);
143+
return true;
144+
}
145+
};
146+
147+
/**
148+
* Adds an item at the rear of Deque. Return true if the operation is successful.
149+
* @param {number} value
150+
* @return {boolean}
151+
*/
152+
MyCircularDeque.prototype.insertLast = function(value) {
153+
if (this.isEmpty()) {
154+
this.arr[0] = value;
155+
return true;
156+
} else if (this.isFull()) {
157+
return false;
158+
} else {
159+
this.arr.push(value);
160+
return true;
161+
}
162+
};
163+
164+
/**
165+
* Deletes an item from the front of Deque. Return true if the operation is successful.
166+
* @return {boolean}
167+
*/
168+
MyCircularDeque.prototype.deleteFront = function() {
169+
if (this.isEmpty()) {
170+
return false;
171+
} else {
172+
this.arr.shift();
173+
return true;
174+
}
175+
};
176+
177+
/**
178+
* Deletes an item from the rear of Deque. Return true if the operation is successful.
179+
* @return {boolean}
180+
*/
181+
MyCircularDeque.prototype.deleteLast = function() {
182+
if (this.isEmpty()) {
183+
return false;
184+
} else {
185+
this.arr.pop();
186+
return true;
187+
}
188+
};
189+
190+
/**
191+
* Get the front item from the deque.
192+
* @return {number}
193+
*/
194+
MyCircularDeque.prototype.getFront = function() {
195+
if (this.isEmpty()) {
196+
return -1;
197+
} else {
198+
return this.arr[0];
199+
}
200+
};
201+
202+
/**
203+
* Get the last item from the deque.
204+
* @return {number}
205+
*/
206+
MyCircularDeque.prototype.getRear = function() {
207+
if (this.isEmpty()) {
208+
return -1;
209+
} else {
210+
return this.arr[this.arr.length - 1];
211+
}
212+
};
213+
214+
/**
215+
* Checks whether the circular deque is empty or not.
216+
* @return {boolean}
217+
*/
218+
MyCircularDeque.prototype.isEmpty = function() {
219+
return this.arr.length == 0;
220+
};
221+
222+
/**
223+
* Checks whether the circular deque is full or not.
224+
* @return {boolean}
225+
*/
226+
MyCircularDeque.prototype.isFull = function() {
227+
return this.arr.length >= this.k;
228+
};
229+
230+
231+
// 接雨水
232+
233+
var trap = function(height) {
234+
let max = 0;
235+
let volumn = 0;
236+
const leftMax = [];
237+
const rightMax = [];
238+
239+
for(let i = 0; i < height.length; i++) {
240+
leftMax[i] = max = Math.max(height[i], max);
241+
}
242+
243+
max = 0;
244+
245+
for(let i = height.length - 1; i >= 0; i--) {
246+
rightMax[i] = max = Math.max(height[i], max);
247+
}
248+
249+
for(let i = 0; i < height.length; i++) {
250+
volumn = volumn + Math.min(leftMax[i], rightMax[i]) - height[i]
251+
}
252+
253+
return volumn;
254+
255+
};

0 commit comments

Comments
 (0)