You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
// Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
2
+
3
+
// Do not allocate extra space for another array, you must do this in place with constant memory.
4
+
5
+
// For example,
6
+
// Given input array nums = [1,1,2],
7
+
8
+
// Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.
9
+
10
+
1
11
/**
2
-
*@param {number[]}A
12
+
*@param {number[]}nums
3
13
*@return {number}
4
14
*/
5
-
varremoveDuplicates=function(A){
6
-
if(A===null){
15
+
varremoveDuplicates=function(nums){
16
+
if(!nums||nums.length===0){
7
17
return0;
8
18
}
19
+
20
+
varend=0;
9
21
10
-
varindex=1;
22
+
// index: 012345678
23
+
// vals: 111222333
24
+
// first swap happen when end = 0; i points at index 3 with val 2
25
+
// end++ becomes end points at index 1 and swap with index 3
// You are given a m x n 2D grid initialized with these three possible values.
4
2
5
3
// -1 - A wall or an obstacle.
6
4
// 0 - A gate.
7
-
// INF - Infinity means an empty room. We use the value 231 - 1 = 2147483647 to represent INF as you may assume that the distance to a gate is less than 2147483647. Fill each empty room with the distance to its nearest gate. If it is impossible to reach a gate, it should be filled with INF.
8
-
//For example, giventhe2D grid:
5
+
// INF - Infinity means an empty room. We use the value 231 - 1 = 2147483647 to represent INF as you may assume that the distance to a gate is less than 2147483647.
6
+
//Fill each empty room withthedistance to its nearest gate. If it is impossible to reach a gate, it should be filled with INF.
9
7
8
+
// For example, given the 2D grid:
10
9
// INF -1 0 INF
11
10
// INF INF INF -1
12
11
// INF -1 INF -1
13
-
// 0 -1 INF INF
12
+
// 0 -1 INF INF
14
13
// After running your function, the 2D grid should be: