From b852482f79b52d578582bec2dbb08f4da32cf9e8 Mon Sep 17 00:00:00 2001 From: zhaocchen Date: Sat, 1 May 2021 16:29:44 +0800 Subject: [PATCH 1/6] feat: add javascript solution to lcci problem: No.02.01.Remove Duplicate Node --- lcci/02.01.Remove Duplicate Node/README.md | 32 +++++++++++++++++++ lcci/02.01.Remove Duplicate Node/README_EN.md | 32 +++++++++++++++++++ lcci/02.01.Remove Duplicate Node/Solution.js | 27 ++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 lcci/02.01.Remove Duplicate Node/Solution.js diff --git a/lcci/02.01.Remove Duplicate Node/README.md b/lcci/02.01.Remove Duplicate Node/README.md index 159a271a25dd2..5a3a60855184e 100644 --- a/lcci/02.01.Remove Duplicate Node/README.md +++ b/lcci/02.01.Remove Duplicate Node/README.md @@ -102,6 +102,38 @@ class Solution { } ``` +### **JavaScript** + +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ + var removeDuplicateNodes = function(head) { + if (head == null || head.next == null) return head; + const cache = new Set([]); + cache.add(head.val); + let cur = head, fast = head.next; + while (fast !== null) { + if (!cache.has(fast.val)) { + cur.next = fast; + cur = cur.next; + cache.add(fast.val); + } + fast = fast.next; + } + cur.next = null; + return head; +}; +``` + ### **...** ``` diff --git a/lcci/02.01.Remove Duplicate Node/README_EN.md b/lcci/02.01.Remove Duplicate Node/README_EN.md index 1e86e25aa4ce8..d618886cf6087 100644 --- a/lcci/02.01.Remove Duplicate Node/README_EN.md +++ b/lcci/02.01.Remove Duplicate Node/README_EN.md @@ -103,6 +103,38 @@ class Solution { } ``` +### **JavaScript** + +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ + var removeDuplicateNodes = function(head) { + if (head == null || head.next == null) return head; + const cache = new Set([]); + cache.add(head.val); + let cur = head, fast = head.next; + while (fast !== null) { + if (!cache.has(fast.val)) { + cur.next = fast; + cur = cur.next; + cache.add(fast.val); + } + fast = fast.next; + } + cur.next = null; + return head; +}; +``` + ### **...** ``` diff --git a/lcci/02.01.Remove Duplicate Node/Solution.js b/lcci/02.01.Remove Duplicate Node/Solution.js new file mode 100644 index 0000000000000..294718cf23288 --- /dev/null +++ b/lcci/02.01.Remove Duplicate Node/Solution.js @@ -0,0 +1,27 @@ +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ + var removeDuplicateNodes = function(head) { + if (head == null || head.next == null) return head; + const cache = new Set([]); + cache.add(head.val); + let cur = head, fast = head.next; + while (fast !== null) { + if (!cache.has(fast.val)) { + cur.next = fast; + cur = cur.next; + cache.add(fast.val); + } + fast = fast.next; + } + cur.next = null; + return head; +}; \ No newline at end of file From 98053e2dfb3c7bad0c0ea603d46771b88fe75539 Mon Sep 17 00:00:00 2001 From: zhaocchen Date: Sat, 1 May 2021 22:21:31 +0800 Subject: [PATCH 2/6] feat: add javascript solution to lcci problem: No.02.02.Kth Node From End of List --- lcci/02.01.Remove Duplicate Node/README.md | 2 +- lcci/02.01.Remove Duplicate Node/README_EN.md | 2 +- .../02.02.Kth Node From End of List/README.md | 28 +++++++++++++++++++ .../README_EN.md | 28 +++++++++++++++++++ .../Solution.js | 23 +++++++++++++++ 5 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 lcci/02.02.Kth Node From End of List/Solution.js diff --git a/lcci/02.01.Remove Duplicate Node/README.md b/lcci/02.01.Remove Duplicate Node/README.md index 5a3a60855184e..b61141778b974 100644 --- a/lcci/02.01.Remove Duplicate Node/README.md +++ b/lcci/02.01.Remove Duplicate Node/README.md @@ -104,7 +104,7 @@ class Solution { ### **JavaScript** -```javascript +```js /** * Definition for singly-linked list. * function ListNode(val) { diff --git a/lcci/02.01.Remove Duplicate Node/README_EN.md b/lcci/02.01.Remove Duplicate Node/README_EN.md index d618886cf6087..95c927c1291ff 100644 --- a/lcci/02.01.Remove Duplicate Node/README_EN.md +++ b/lcci/02.01.Remove Duplicate Node/README_EN.md @@ -105,7 +105,7 @@ class Solution { ### **JavaScript** -```javascript +```js /** * Definition for singly-linked list. * function ListNode(val) { diff --git a/lcci/02.02.Kth Node From End of List/README.md b/lcci/02.02.Kth Node From End of List/README.md index abc4722c9aba6..dd414758a0531 100644 --- a/lcci/02.02.Kth Node From End of List/README.md +++ b/lcci/02.02.Kth Node From End of List/README.md @@ -77,6 +77,34 @@ class Solution { } ``` +### **JavaScript** + +```js +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @param {number} k + * @return {number} + */ +var kthToLast = function(head, k) { + let fast = head, slow = head; + for (let i = 0; i < k; i++) { + fast = fast.next; + } + while (fast != null) { + fast = fast.next; + slow = slow.next; + } + return slow.val; +}; +``` + ### **...** ``` diff --git a/lcci/02.02.Kth Node From End of List/README_EN.md b/lcci/02.02.Kth Node From End of List/README_EN.md index 464e16741d508..fcb95d747eff5 100644 --- a/lcci/02.02.Kth Node From End of List/README_EN.md +++ b/lcci/02.02.Kth Node From End of List/README_EN.md @@ -69,6 +69,34 @@ class Solution { } ``` +### **JavaScript** + +```js +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @param {number} k + * @return {number} + */ +var kthToLast = function(head, k) { + let fast = head, slow = head; + for (let i = 0; i < k; i++) { + fast = fast.next; + } + while (fast != null) { + fast = fast.next; + slow = slow.next; + } + return slow.val; +}; +``` + ### **...** ``` diff --git a/lcci/02.02.Kth Node From End of List/Solution.js b/lcci/02.02.Kth Node From End of List/Solution.js new file mode 100644 index 0000000000000..3fa82d9feaf83 --- /dev/null +++ b/lcci/02.02.Kth Node From End of List/Solution.js @@ -0,0 +1,23 @@ +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @param {number} k + * @return {number} + */ + var kthToLast = function(head, k) { + let fast = head, slow = head; + for (let i = 0; i < k; i++) { + fast = fast.next; + } + while (fast != null) { + fast = fast.next; + slow = slow.next; + } + return slow.val; +}; \ No newline at end of file From 413ccab4a4c1bd3eb32e46e2e5e88d0a17ac7130 Mon Sep 17 00:00:00 2001 From: zhaocchen Date: Sat, 1 May 2021 23:10:45 +0800 Subject: [PATCH 3/6] feat: add javascript solution to lcci problem: No.02.03.Delete Middle Node --- lcci/02.03.Delete Middle Node/README.md | 20 ++++++++++++++++++++ lcci/02.03.Delete Middle Node/README_EN.md | 20 ++++++++++++++++++++ lcci/02.03.Delete Middle Node/Solution.js | 15 +++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 lcci/02.03.Delete Middle Node/Solution.js diff --git a/lcci/02.03.Delete Middle Node/README.md b/lcci/02.03.Delete Middle Node/README.md index 55c35c3498679..428d984c60b8b 100644 --- a/lcci/02.03.Delete Middle Node/README.md +++ b/lcci/02.03.Delete Middle Node/README.md @@ -66,6 +66,26 @@ class Solution { } ``` +### **JavaScript** + +```js +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} node + * @return {void} Do not return anything, modify node in-place instead. + */ + var deleteNode = function(node) { + node.val = node.next.val + node.next = node.next.next +}; +``` + ### **...** ``` diff --git a/lcci/02.03.Delete Middle Node/README_EN.md b/lcci/02.03.Delete Middle Node/README_EN.md index 5b34c7d6518d8..600112b3890f0 100644 --- a/lcci/02.03.Delete Middle Node/README_EN.md +++ b/lcci/02.03.Delete Middle Node/README_EN.md @@ -60,6 +60,26 @@ class Solution { } ``` +### **JavaScript** + +```js +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} node + * @return {void} Do not return anything, modify node in-place instead. + */ + var deleteNode = function(node) { + node.val = node.next.val + node.next = node.next.next +}; +``` + ### **...** ``` diff --git a/lcci/02.03.Delete Middle Node/Solution.js b/lcci/02.03.Delete Middle Node/Solution.js new file mode 100644 index 0000000000000..47ee0fe28e02c --- /dev/null +++ b/lcci/02.03.Delete Middle Node/Solution.js @@ -0,0 +1,15 @@ +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} node + * @return {void} Do not return anything, modify node in-place instead. + */ + var deleteNode = function(node) { + node.val = node.next.val + node.next = node.next.next +}; \ No newline at end of file From a0688b89e28dfbfa5f2b9d8eaf2c70ce2ad07dcf Mon Sep 17 00:00:00 2001 From: zhaocchen Date: Sun, 2 May 2021 10:35:41 +0800 Subject: [PATCH 4/6] feat: add javascript solution to lcci problem: No.17.04.Missing Number --- lcci/17.04.Missing Number/README.md | 16 ++++++++++++++++ lcci/17.04.Missing Number/README_EN.md | 16 ++++++++++++++++ lcci/17.04.Missing Number/Solution.js | 11 +++++++++++ 3 files changed, 43 insertions(+) create mode 100644 lcci/17.04.Missing Number/Solution.js diff --git a/lcci/17.04.Missing Number/README.md b/lcci/17.04.Missing Number/README.md index bb254afa7d962..b138f2d25b27f 100644 --- a/lcci/17.04.Missing Number/README.md +++ b/lcci/17.04.Missing Number/README.md @@ -63,6 +63,22 @@ class Solution { } ``` +### **JavaScript** + +```js +/** + * @param {number[]} nums + * @return {number} + */ +var missingNumber = function(nums) { + let res; + for (let i = 0; i < nums.length; i++) { + res = res ^ nums[i] ^ (i + 1); + } + return res; +}; +``` + ### **...** ``` diff --git a/lcci/17.04.Missing Number/README_EN.md b/lcci/17.04.Missing Number/README_EN.md index 8d38e6e64b912..747e75482c33a 100644 --- a/lcci/17.04.Missing Number/README_EN.md +++ b/lcci/17.04.Missing Number/README_EN.md @@ -61,6 +61,22 @@ class Solution { } ``` +### **JavaScript** + +```js +/** + * @param {number[]} nums + * @return {number} + */ +var missingNumber = function(nums) { + let res; + for (let i = 0; i < nums.length; i++) { + res = res ^ nums[i] ^ (i + 1); + } + return res; +}; +``` + ### **...** ``` diff --git a/lcci/17.04.Missing Number/Solution.js b/lcci/17.04.Missing Number/Solution.js new file mode 100644 index 0000000000000..7d961678f2356 --- /dev/null +++ b/lcci/17.04.Missing Number/Solution.js @@ -0,0 +1,11 @@ +/** + * @param {number[]} nums + * @return {number} + */ + var missingNumber = function(nums) { + let res; + for (let i = 0; i < nums.length; i++) { + res = res ^ nums[i] ^ (i + 1); + } + return res; +}; \ No newline at end of file From bda8450c6777893aaa67203f5e976329327b1e0b Mon Sep 17 00:00:00 2001 From: zhaocchen Date: Thu, 6 May 2021 11:10:38 +0800 Subject: [PATCH 5/6] feat: add javascript solution to lcci problem: No.17.10.Find Majority Element --- lcci/17.10.Find Majority Element/README.md | 27 +++++++++++++++++++ lcci/17.10.Find Majority Element/README_EN.md | 27 +++++++++++++++++++ lcci/17.10.Find Majority Element/Solution.js | 20 ++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 lcci/17.10.Find Majority Element/Solution.js diff --git a/lcci/17.10.Find Majority Element/README.md b/lcci/17.10.Find Majority Element/README.md index ad1805f1708ba..8f7464c2fea1c 100644 --- a/lcci/17.10.Find Majority Element/README.md +++ b/lcci/17.10.Find Majority Element/README.md @@ -35,6 +35,8 @@ +摩尔投票法 + ### **Python3** @@ -53,6 +55,31 @@ ``` +### **JavaScript** + +```js +/** + * @param {number[]} nums + * @return {number} + */ +var majorityElement = function(nums) { + let candidate = 0, count = 0; + for (let num of nums) { + if (count == 0) candidate = num; + if (candidate == num) { + count++; + } else { + count--; + } + } + let n = 0; + for (let num of nums) { + if (candidate == num) n++; + } + return n > (nums.length / 2) ? candidate : -1; +}; +``` + ### **...** ``` diff --git a/lcci/17.10.Find Majority Element/README_EN.md b/lcci/17.10.Find Majority Element/README_EN.md index d5bcac1be8f67..724bc670dc26e 100644 --- a/lcci/17.10.Find Majority Element/README_EN.md +++ b/lcci/17.10.Find Majority Element/README_EN.md @@ -38,6 +38,8 @@ ## Solutions +Boyer–Moore majority vote algorithm + ### **Python3** @@ -52,6 +54,31 @@ ``` +### **JavaScript** + +```js +/** + * @param {number[]} nums + * @return {number} + */ +var majorityElement = function(nums) { + let candidate = 0, count = 0; + for (let num of nums) { + if (count == 0) candidate = num; + if (candidate == num) { + count++; + } else { + count--; + } + } + let n = 0; + for (let num of nums) { + if (candidate == num) n++; + } + return n > (nums.length / 2) ? candidate : -1; +}; +``` + ### **...** ``` diff --git a/lcci/17.10.Find Majority Element/Solution.js b/lcci/17.10.Find Majority Element/Solution.js new file mode 100644 index 0000000000000..0c6247c1dc9de --- /dev/null +++ b/lcci/17.10.Find Majority Element/Solution.js @@ -0,0 +1,20 @@ +/** + * @param {number[]} nums + * @return {number} + */ + var majorityElement = function(nums) { + let candidate = 0, count = 0; + for (let num of nums) { + if (count == 0) candidate = num; + if (candidate == num) { + count++; + } else { + count--; + } + } + let n = 0; + for (let num of nums) { + if (candidate == num) n++; + } + return n > (nums.length / 2) ? candidate : -1; +}; \ No newline at end of file From 0276f82eb6c6df070159f3ca239be46905e4aa86 Mon Sep 17 00:00:00 2001 From: zhaocchen Date: Thu, 6 May 2021 23:50:55 +0800 Subject: [PATCH 6/6] feat: add javascript solution to lcci problem: No.01.08.Zero Matrix --- lcci/01.08.Zero Matrix/README.md | 31 +++++++++++++++++++++++++++++ lcci/01.08.Zero Matrix/README_EN.md | 31 +++++++++++++++++++++++++++++ lcci/01.08.Zero Matrix/Solution.js | 26 ++++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 lcci/01.08.Zero Matrix/Solution.js diff --git a/lcci/01.08.Zero Matrix/README.md b/lcci/01.08.Zero Matrix/README.md index 3cef9fd644bf6..86413b929a638 100644 --- a/lcci/01.08.Zero Matrix/README.md +++ b/lcci/01.08.Zero Matrix/README.md @@ -117,6 +117,37 @@ class Solution { } ``` +### **JavaScript** + +```js +/** + * @param {number[][]} matrix + * @return {void} Do not return anything, modify matrix in-place instead. + */ +var setZeroes = function(matrix) { + let m = matrix.length, n = matrix[0].length; + let rows = new Array(m).fill(false); + let cols = new Array(n).fill(false); + // 标记 + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (matrix[i][j] == 0) { + rows[i] = true; + cols[j] = true; + } + } + } + // 清零 + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (matrix[i][j] != 0 && (rows[i] || cols[j])) { + matrix[i][j] = 0; + } + } + } +}; +``` + ### **...** ``` diff --git a/lcci/01.08.Zero Matrix/README_EN.md b/lcci/01.08.Zero Matrix/README_EN.md index a1585a7f731aa..3d89bcff062a9 100644 --- a/lcci/01.08.Zero Matrix/README_EN.md +++ b/lcci/01.08.Zero Matrix/README_EN.md @@ -132,6 +132,37 @@ class Solution { } ``` +### **JavaScript** + +```js +/** + * @param {number[][]} matrix + * @return {void} Do not return anything, modify matrix in-place instead. + */ +var setZeroes = function(matrix) { + let m = matrix.length, n = matrix[0].length; + let rows = new Array(m).fill(false); + let cols = new Array(n).fill(false); + // 标记 + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (matrix[i][j] == 0) { + rows[i] = true; + cols[j] = true; + } + } + } + // 清零 + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (matrix[i][j] != 0 && (rows[i] || cols[j])) { + matrix[i][j] = 0; + } + } + } +}; +``` + ### **...** ``` diff --git a/lcci/01.08.Zero Matrix/Solution.js b/lcci/01.08.Zero Matrix/Solution.js new file mode 100644 index 0000000000000..8ad1812fc5fec --- /dev/null +++ b/lcci/01.08.Zero Matrix/Solution.js @@ -0,0 +1,26 @@ +/** + * @param {number[][]} matrix + * @return {void} Do not return anything, modify matrix in-place instead. + */ + var setZeroes = function(matrix) { + let m = matrix.length, n = matrix[0].length; + let rows = new Array(m).fill(false); + let cols = new Array(n).fill(false); + // 标记 + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (matrix[i][j] == 0) { + rows[i] = true; + cols[j] = true; + } + } + } + // 清零 + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + if (matrix[i][j] != 0 && (rows[i] || cols[j])) { + matrix[i][j] = 0; + } + } + } +}; \ No newline at end of file