From eb9666522f106d9ae2a96055ac4a1cd97d452111 Mon Sep 17 00:00:00 2001 From: PeterGW Date: Sun, 14 Jun 2020 23:22:16 +0800 Subject: [PATCH 1/4] Create week01.js --- Week01/week01.js | 255 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 255 insertions(+) create mode 100644 Week01/week01.js diff --git a/Week01/week01.js b/Week01/week01.js new file mode 100644 index 000000000..a2c975633 --- /dev/null +++ b/Week01/week01.js @@ -0,0 +1,255 @@ +// 加一(谷歌、字节跳动、Facebook 在半年内面试中考过) +var plusOne = function(digits) { + for(let i = digits.length - 1;i >= 0;i--){ + if(digits[i] == 9){ + digits[i] = 0; + }else{ + digits[i]++; + return digits; + } + } + digits.unshift(1); + return digits; +}; + +// 零移动 +var moveZeroes = function(nums) { + // let i = 0, j = 0; + // for (; i < nums.length; i++) { + // if (nums[i] !== 0) { + // let temp = nums[j]; + // nums[j] = nums[i]; + // nums[i] = temp; + // j++; + // } + // } + // let j = nums.length; + for (var j = nums.length; j--;) { + if (nums[j] === 0) { + nums.splice(j, 1); + nums.push(0); + } + } +}; + +// 两数之和 +var twoSum = function(nums, target) { + let temArr = []; + for(var i=0;i2->4, 1->3->4 +var mergeTwoLists = function(l1, l2) { + let pHead = null; + let pHead1 = l1; // 此处l1是指1->2->4中的1 还是 1->2->4 ?? + let pHead2 = l2; + let p1 = l1; + let p2 = l2; + let p = null; + if (!l1) { + return l2; + } else if (!l2) { + return l1; + } + + if (l1.val < l2.val) { + pHead = p1; + p = p1; + p1 = p1.next; + } else { + pHead = p2; + p = p2; + p2 = p2.next; + } + + while (p1 && p2) { + if (p1.val < p2.val) { + p.next = p1; + p = p.next; + p1 = p1.next; + } else { + p.next = p2; + p = p.next; + p2 = p2.next; + } + } + + if (p1) { + p.next = p1; + } else if (p2) { + p.next = p2; + } + + return pHead; +}; + + +// 旋转数组 +var rotate = function(nums, k) { + nums.unshift(...nums.splice(nums.length - k)); +}; + +// 删除排序数组中的重复项(Facebook、字节跳动、微软在半年内面试中考过) +var removeDuplicates = function(nums) { + let index = 0; + for(let i = 0; i< nums.length; i++) { + if( nums[i] != nums[i+1] ) { + nums[index] = nums[i] + index++ + } + } + return index +}; + +// 双端循环队列 (还需继续加强) +/** + * Initialize your data structure here. Set the size of the deque to be k. + * @param {number} k + */ +var MyCircularDeque = function(k) { + this.arr = []; + this.k = k; +}; + +/** + * Adds an item at the front of Deque. Return true if the operation is successful. + * @param {number} value + * @return {boolean} + */ +MyCircularDeque.prototype.insertFront = function(value) { + if (this.isEmpty()) { + this.arr[0] = value; + return true; + } else if (this.isFull()) { + return false; + } else { + this.arr.unshift(value); + return true; + } +}; + +/** + * Adds an item at the rear of Deque. Return true if the operation is successful. + * @param {number} value + * @return {boolean} + */ +MyCircularDeque.prototype.insertLast = function(value) { + if (this.isEmpty()) { + this.arr[0] = value; + return true; + } else if (this.isFull()) { + return false; + } else { + this.arr.push(value); + return true; + } +}; + +/** + * Deletes an item from the front of Deque. Return true if the operation is successful. + * @return {boolean} + */ +MyCircularDeque.prototype.deleteFront = function() { + if (this.isEmpty()) { + return false; + } else { + this.arr.shift(); + return true; + } +}; + +/** + * Deletes an item from the rear of Deque. Return true if the operation is successful. + * @return {boolean} + */ +MyCircularDeque.prototype.deleteLast = function() { + if (this.isEmpty()) { + return false; + } else { + this.arr.pop(); + return true; + } +}; + +/** + * Get the front item from the deque. + * @return {number} + */ +MyCircularDeque.prototype.getFront = function() { + if (this.isEmpty()) { + return -1; + } else { + return this.arr[0]; + } +}; + +/** + * Get the last item from the deque. + * @return {number} + */ +MyCircularDeque.prototype.getRear = function() { + if (this.isEmpty()) { + return -1; + } else { + return this.arr[this.arr.length - 1]; + } +}; + +/** + * Checks whether the circular deque is empty or not. + * @return {boolean} + */ +MyCircularDeque.prototype.isEmpty = function() { + return this.arr.length == 0; +}; + +/** + * Checks whether the circular deque is full or not. + * @return {boolean} + */ +MyCircularDeque.prototype.isFull = function() { + return this.arr.length >= this.k; +}; + + + // 接雨水 + + var trap = function(height) { + let max = 0; + let volumn = 0; + const leftMax = []; + const rightMax = []; + + for(let i = 0; i < height.length; i++) { + leftMax[i] = max = Math.max(height[i], max); + } + + max = 0; + + for(let i = height.length - 1; i >= 0; i--) { + rightMax[i] = max = Math.max(height[i], max); + } + + for(let i = 0; i < height.length; i++) { + volumn = volumn + Math.min(leftMax[i], rightMax[i]) - height[i] + } + + return volumn; + +}; From 9d8911ba44ecb9139ed00bb827158e8a82f8a174 Mon Sep 17 00:00:00 2001 From: gaowei Date: Sun, 14 Jun 2020 23:27:35 +0800 Subject: [PATCH 2/4] add --- Week01/NOTE.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Week01/NOTE.md b/Week01/NOTE.md index 50de30414..e9c3ea972 100644 --- a/Week01/NOTE.md +++ b/Week01/NOTE.md @@ -1 +1,19 @@ -学习笔记 \ No newline at end of file +学习笔记 + +时间复杂度: + 代码段执行时间,一般可以根据最复杂代码段判断整段代码的时间复杂度 + +空间复杂度: + 代码段执行所占内存的空间 + +数组: + 内存中连续分配的空间,javascript中的数组为自动扩容 + 查找元素的时间复杂度为O(1),插入删除的时间复杂度一般O(n); + +链表: + 头尾指针,HEAD/TAIL,通过next指向下一节点,如果只有一个是单链表,有两个指针是双链表,尾指针指向头的话为循环链表 + 链表插入删除增加效率很高,时间复杂度为O(1),查找节点的时间复杂度为O(n) + +跳表: + 跳表是有序的,只有有序的才可以使用跳表 + 插入/删除/搜索都是O(logn),跳表是通过增加索引优化,操作难度偏高,空间复杂度O(n),升维+空间换时间 From 3dfbbf33a91dd2d9d6de1af68d523b8851503834 Mon Sep 17 00:00:00 2001 From: gaowei Date: Fri, 19 Jun 2020 18:44:31 +0800 Subject: [PATCH 3/4] ad --- .DS_Store | Bin 0 -> 6148 bytes Week02/NOTE 2.md | 1 + Week02/NOTE.md | 24 +++++++++++++++++++++++- Week02/Valid Anagram.ts | 35 +++++++++++++++++++++++++++++++++++ Week02/k-pre-num.js | 15 +++++++++++++++ Week02/letter-async.js | 17 +++++++++++++++++ Week02/n-tree-layer.js | 30 ++++++++++++++++++++++++++++++ Week02/pre-tree.js | 33 +++++++++++++++++++++++++++++++++ Week02/two-tree-middle.js | 21 +++++++++++++++++++++ Week02/two-tree-pre.js | 25 +++++++++++++++++++++++++ Week02/twoSum.ts | 37 +++++++++++++++++++++++++++++++++++++ Week02/ugliy-num.js | 25 +++++++++++++++++++++++++ 12 files changed, 262 insertions(+), 1 deletion(-) create mode 100644 .DS_Store create mode 100644 Week02/NOTE 2.md create mode 100644 Week02/Valid Anagram.ts create mode 100644 Week02/k-pre-num.js create mode 100644 Week02/letter-async.js create mode 100644 Week02/n-tree-layer.js create mode 100644 Week02/pre-tree.js create mode 100644 Week02/two-tree-middle.js create mode 100644 Week02/two-tree-pre.js create mode 100644 Week02/twoSum.ts create mode 100644 Week02/ugliy-num.js diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..d1772e627527e728d6815ca802addfa826493d92 GIT binary patch literal 6148 zcmeHKPixyS6qk~$b&`w6R#@pp*Fp|%x4Ku)>yCqfOG0T&W@EF689T&I!w3PN_c8Xj zA7rez-uG2@>U)w>8qywD+VP%v`aM0pC)qz4$zqIge-iI7W;4btP{djjs#^s6QCFm9 zJcwM4;jrCkT8yGJk5;0|@emonwOeC-mO%N)me=oZG)iSrI?f+nt2dskty|4j8;kw4 zbs7xwX;6mcr0~P>v1-XUiHzM1%i+R4%=7fx4x^q-rUU!=n>;H+nI+>JsV9Az#rf3F zCs|ya1bV!_jLYG`-sp~#Gf3@*S<;h*jASayPgWDlz}}qAc3ju(h}}JR-Vw9?eWxQ{ zy>#bu`_i(uw)bAYI~vVDfBE|D`;VXJzkchb;PBNdd1i0{7ho*0au$rUG|xVOf046@ zB_sxj0b=0sF<^EwtNr*Mot~H&AO`+11Gqm3P(;sQrBQ7iP~q<*_BRkwz{a}-qA=(g ztTaLo2-m5AI+dF%2G{A}7bea#SZUPhjH{Vp95XXFHx#aB2ft9^jC&fXCI*OsWd`cH zY2*3-_U`-tauT(O0b=04Vt_Xef&(93$(*e#FNbHX0DTXNf^n6`>l855Q4Fzo6u$sf Z0)Bx8pl7hs2p$mn5l}QxMGV|416S{QWLW?J literal 0 HcmV?d00001 diff --git a/Week02/NOTE 2.md b/Week02/NOTE 2.md new file mode 100644 index 000000000..50de30414 --- /dev/null +++ b/Week02/NOTE 2.md @@ -0,0 +1 @@ +学习笔记 \ No newline at end of file diff --git a/Week02/NOTE.md b/Week02/NOTE.md index 50de30414..506f173ab 100644 --- a/Week02/NOTE.md +++ b/Week02/NOTE.md @@ -1 +1,23 @@ -学习笔记 \ No newline at end of file +学习笔记 + +二叉树遍历 + 前序: 根左右 + 中序: 左根右 + 后续: 左右根 + + 查找遍历O(n) + +二叉搜索树 + 有序的树-二叉排序树,有序二叉树、排序二叉树 + 左子树所有节点值均小于它的根节点的值 + 右子树所有节点值均大于它的根节点的值 + + 中序排序: 升值排序 + + 时间复杂度O(logn) + +堆: + 可以迅速找到一堆数中最大与最小的数据结构 大顶堆和小顶堆 + +图: + 有点有边 \ No newline at end of file diff --git a/Week02/Valid Anagram.ts b/Week02/Valid Anagram.ts new file mode 100644 index 000000000..0faee7d8c --- /dev/null +++ b/Week02/Valid Anagram.ts @@ -0,0 +1,35 @@ +// answer +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isAnagram = function(s: string, t:string): boolean { + let tempS = s.toLowerCase().split('').sort().join(''); + let tempT = t.toLowerCase().split('').sort().join(''); + return tempS === tempT; +} + +// answer2 思路 创建一个map存储一个字符串的变量 判断是否有key,有则计数+1 +// 另一个字符串通过i对比 存储-1 +// 最后计算 想加为0 +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isAnagram2 = function(s: string, t:string): boolean { + if(s.length !== t.length) { + return false; + } + let map = new Map(); + s.split('').map((item, i) => { + map.set(item, map.has(item) ? map.get(item) + 1 : 1); + map.set(t[i], map.has(t[i]) ? map.get(t[i]) - 1 : -1); + }); + + return Array.from(map.values()).reduce((cur,pre) => { + cur && pre === 0; + }, true); +} + diff --git a/Week02/k-pre-num.js b/Week02/k-pre-num.js new file mode 100644 index 000000000..b4827dc4d --- /dev/null +++ b/Week02/k-pre-num.js @@ -0,0 +1,15 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ +var topKFrequent = function(nums, k) { + let map = new Map(), + arr = [...new Set(nums)] + nums.map((num) => { + if (map.has(num)) map.set(num, map.get(num) + 1) + else map.set(num, 1) + }) + + return arr.sort((a, b) => map.get(b) - map.get(a)).slice(0, k); +}; \ No newline at end of file diff --git a/Week02/letter-async.js b/Week02/letter-async.js new file mode 100644 index 000000000..84350332c --- /dev/null +++ b/Week02/letter-async.js @@ -0,0 +1,17 @@ +// @lc code=start +/** + * @param {string[]} strs + * @return {string[][]} + */ +var groupAnagrams = function(strs) { + const map = new Map(); + for (let i = 0; i < strs.length; i++) { + let temp = strs[i].split('').sort(); + if (map.has(temp)) { + map.set(temp, map.get(temp).push(strs[i])); + } else { + map.set(temp, [strs[i]]); + } + } + return map.values(); +}; \ No newline at end of file diff --git a/Week02/n-tree-layer.js b/Week02/n-tree-layer.js new file mode 100644 index 000000000..d0a80452b --- /dev/null +++ b/Week02/n-tree-layer.js @@ -0,0 +1,30 @@ +/** + * // Definition for a Node. + * function Node(val,children) { + * this.val = val; + * this.children = children; + * }; + */ + +/** + * @param {Node} root + * @return {number[][]} + */ +var levelOrder = function(root) { + if (!root) return []; + let queue = [root]; + let ans = []; + while (queue.length) { + let level = []; + let len = queue.length; + for (let i = 0; i < len; i++) { + let current = queue.shift(); + level.push(current.val); + if (current.children && current.children.length > 0) { + queue.push(...current.children); + } + } + ans.push(level); + } + return ans; +}; \ No newline at end of file diff --git a/Week02/pre-tree.js b/Week02/pre-tree.js new file mode 100644 index 000000000..4f41b4e5a --- /dev/null +++ b/Week02/pre-tree.js @@ -0,0 +1,33 @@ +/** + * // Definition for a Node. + * function Node(val, children) { + * this.val = val; + * this.children = children; + * }; + */ +function Node(val, children) { + this.val = val; + this.children = children; +}; +/** + * @param {Node} root + * @return {number[]} + */ +var preorder = function(root) { + if (!root) return []; + return Array.prototype.concat.apply([root.val], root.children.map(child => preorder(child))); +}; + +// 栈 - 先进后出 reverse 先进先出 +var preorder = function(root) { + if (!root) return []; + let res = [], + arr = [root]; + while (arr.length) { + let current = arr.pop(); + if (!current) continue; + res.push(current.val); + arr.push(...current.children.reverse()); + } + return res; +}; \ No newline at end of file diff --git a/Week02/two-tree-middle.js b/Week02/two-tree-middle.js new file mode 100644 index 000000000..a8e91f7db --- /dev/null +++ b/Week02/two-tree-middle.js @@ -0,0 +1,21 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number[]} + */ +var inorderTraversal = function(root) { + let nums = []; + let fun = (root) => { + root.left && fun(root.left); + nums.push(root.val); + root.right && fun(root.right); + } + root && fun(root); + return nums; +} \ No newline at end of file diff --git a/Week02/two-tree-pre.js b/Week02/two-tree-pre.js new file mode 100644 index 000000000..5c3a48fe0 --- /dev/null +++ b/Week02/two-tree-pre.js @@ -0,0 +1,25 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number[]} + */ +var preorderTraversal = function(root) { + let arr = [], + res = [] + root && arr.push(root) + + while (arr.length > 0) { + let cur = arr.pop() + res.push(cur.val) + + cur.right && arr.push(cur.right) + cur.left && arr.push(cur.left) + } + return res +}; \ No newline at end of file diff --git a/Week02/twoSum.ts b/Week02/twoSum.ts new file mode 100644 index 000000000..8dcc25aa6 --- /dev/null +++ b/Week02/twoSum.ts @@ -0,0 +1,37 @@ +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +var twoSum = function(nums: Array, target: number): Array { + let index = 0; + let first = nums[index]; + let arr = []; + for (let i = 1; i < nums.length; i++) { + let num = target - nums[i]; + if (first == num) { + arr.push(index, i); + } + if (i == nums.length - 1) { + i = index + 1; + index++; + first = nums[index]; + } + } + return arr; +}; + + +var twoSum2 = function(nums: Array, target: number): Array { + const map = new Map(); + + for (let i = 0; i < nums.length; i++) { + let subtractionValue = target - nums[i]; + let toValue = map.get(subtractionValue); + if (toValue !== undefined) { + return [i, toValue] + } + map.set(nums[i], i); + } +}; + diff --git a/Week02/ugliy-num.js b/Week02/ugliy-num.js new file mode 100644 index 000000000..8cb598aa5 --- /dev/null +++ b/Week02/ugliy-num.js @@ -0,0 +1,25 @@ +/** + * @param {number} n + * @return {number} + */ +var nthUglyNumber = function(n) { + let result = [1]; + let i2 = 0; + let i3 = 0; + let i5 = 0; + for (let i = 1; i < n; i++) { + let target = Math.min(2 * result[i2], 3 * result[i3], 5 * result[i5]); + result.push(target); + if (target === 2 * result[i2]) { + ++i2; + } + if (target === 3 * result[i3]) { + ++i3; + } + if (target === 5 * result[i5]) { + ++i5; + } + } + return result[n - 1]; + +}; \ No newline at end of file From 396d321ab1a31e1ddcc36d57d20eb9b8f9eb4966 Mon Sep 17 00:00:00 2001 From: gaowei Date: Sun, 5 Jul 2020 23:53:01 +0800 Subject: [PATCH 4/4] d --- .DS_Store | Bin 6148 -> 6148 bytes Week03/defTemplate.js | 19 +++++++++++++++++++ Week04/.DS_Store | Bin 0 -> 6148 bytes Week04/NOTE.md | 6 +++++- Week04/findContentChildren.js | 21 +++++++++++++++++++++ Week04/findMin.js | 17 +++++++++++++++++ Week04/lemonJuice.js | 26 ++++++++++++++++++++++++++ Week04/maxProfit.js | 15 +++++++++++++++ Week04/numIslands.js | 30 ++++++++++++++++++++++++++++++ 9 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 Week03/defTemplate.js create mode 100644 Week04/.DS_Store create mode 100644 Week04/findContentChildren.js create mode 100644 Week04/findMin.js create mode 100644 Week04/lemonJuice.js create mode 100644 Week04/maxProfit.js create mode 100644 Week04/numIslands.js diff --git a/.DS_Store b/.DS_Store index d1772e627527e728d6815ca802addfa826493d92..4ce4b9e13c588e42119302b449ce3f8d9f6e980e 100644 GIT binary patch delta 205 zcmZoMXfc=|#>B!ku~2NHo+2a5#(>?7ivyUM7+E&+Fqtw+vN41+qyk|!g8_p{Qh9Mf zQcivn0|Ud)$r?o{JeCaIOE2`8H}6RIruq%j@|6Y W{GE9+zlb9T&?qL5_RSF@YnTDZGcCFR delta 69 zcmZoMXfc=|#>B)qu~2NHo+2aD#(>?7lMO^zHuJL>F>V&&5N4U!uz52(2R{c;(Plx8 X@640=MI1R8fPj&Kfn{@q$QotY5S`H=0STnE+{VT(rQH(F)+TA}gq*~}kQAmAw#%>Zm-z?$1=hZ|GYY4t zjUZ8GVE4`L%RPB4ZI5q7aozkFdMfbl}bdpmL1Mm#g!;rryD!$?v9H!m({Aq5?CrPq&F!D!XCJ>=tSJ}>27-ZL z;Aafr&ZZ2H4WkYQf`MS*odKQ?35A#)i(x%FP}LLw$Y*pF=*%S;lN_^SF@y!877Dab z^%O%b9R11tvSTr{a8XY_)MtK6UbN1R@l$md&4y721HnL-fkSJKdH-MFm+4LNyCKmE z27-Zq#()l*Su@3^{BHfUJ>Inm literal 0 HcmV?d00001 diff --git a/Week04/NOTE.md b/Week04/NOTE.md index 50de30414..74e93d3ac 100644 --- a/Week04/NOTE.md +++ b/Week04/NOTE.md @@ -1 +1,5 @@ -学习笔记 \ No newline at end of file +学习笔记 + +-- Binary Search +取n/2的位置 比较左右数据是否为有序 +以左边、右边数据继续查找 直到找到不是有序的数据终止 \ No newline at end of file diff --git a/Week04/findContentChildren.js b/Week04/findContentChildren.js new file mode 100644 index 000000000..1a12dc254 --- /dev/null +++ b/Week04/findContentChildren.js @@ -0,0 +1,21 @@ + +/** + * @param {number[]} g + * @param {number[]} s + * @return {number} + */ +var findContentChildren = function(g, s) { + g = g.sort((a, b) => a - b); + s = s.sort((a, b) => a - b); + var maxNum = 0, gIndex = 0, sIndex = 0; + while(gIndex < g.length && sIndex < s.length) { + if (s[sIndex] > g[gIndex]) { + maxNum++; + sIndex++; + gIndex++ + } else { + sIndex++ + } + } + return maxNum; +} \ No newline at end of file diff --git a/Week04/findMin.js b/Week04/findMin.js new file mode 100644 index 000000000..0afe412ed --- /dev/null +++ b/Week04/findMin.js @@ -0,0 +1,17 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var findMin = function(nums) { + var left = 0; + var right = nums.length - 1; + while (left < right) { + var mid = (left + right) >> 1; + if (nums[mid] > nums[right]) { + left = mid + 1; + } else { + right = mid; + } + } + return nums[left]; +}; \ No newline at end of file diff --git a/Week04/lemonJuice.js b/Week04/lemonJuice.js new file mode 100644 index 000000000..7e735d8bb --- /dev/null +++ b/Week04/lemonJuice.js @@ -0,0 +1,26 @@ +var lemonadeChange = function(bills) { + var five = 0, ten = 0; + + for(let i = 0; i < bills.length; i++) { + if (bills[i] === 5) { + five++; + } else if (bills[i] === 10) { + if ( five === 0) { + five--; + } + ten++; + five--; + } else if (bills[i] === 20) { + if (ten > 0 && five > 0) { + ten--; + five--; + } else if (five > 3) { + five -= 3; + } else { + return false; + } + } + } + + return true; +}; diff --git a/Week04/maxProfit.js b/Week04/maxProfit.js new file mode 100644 index 000000000..3ae98449d --- /dev/null +++ b/Week04/maxProfit.js @@ -0,0 +1,15 @@ +// 后一天比前一天大为收益,小则跳过 + +/** + * @param {number[]} prices + * @return {number} + */ +var maxProfit = function(prices) { + let income = 0; + for (let i = 0; i < prices.length; i++) { + if (prices[i+1] > prices[i]) { + income += prices[i+1] - prices[i]; + } + } + return income; +}; \ No newline at end of file diff --git a/Week04/numIslands.js b/Week04/numIslands.js new file mode 100644 index 000000000..3f1ad1237 --- /dev/null +++ b/Week04/numIslands.js @@ -0,0 +1,30 @@ +/** + * @param {character[][]} grid + * @return {number} + */ +var numIslands = function(grid) { + if(!grid.length){ + return 0; + } + let num = 0; + let row = grid.length; + let col = grid[0].length; + let loop = (x, y) => { + if(grid[x][y] == 1){ + grid[x][y] = 0; + x > 0 && loop(x - 1, y); + x < row - 1 && loop(x + 1, y); + y > 0 && loop(x, y - 1); + y < col - 1 && loop(x, y + 1); + } + } + for(let i = 0; i < row; i++){ + for(let j = 0; j < col; j++){ + if(grid[i][j] == 1){ + num++; + loop(i, j); + } + } + } + return num; +} \ No newline at end of file