From f08002ed2a01cc6b9a8717cef46221a4b95fe18b Mon Sep 17 00:00:00 2001 From: YangQi <70502828+YangFong@users.noreply.github.com> Date: Wed, 22 Dec 2021 16:57:36 +0800 Subject: [PATCH 1/5] refactor: optimizing JavaScript examples --- basic/sorting/BubbleSort/README.md | 36 ++++++++++++++++-------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/basic/sorting/BubbleSort/README.md b/basic/sorting/BubbleSort/README.md index b28e7ae4e4ec3..95fff1d733a8d 100644 --- a/basic/sorting/BubbleSort/README.md +++ b/basic/sorting/BubbleSort/README.md @@ -45,25 +45,27 @@ public class BubbleSort { ```js function bubbleSort(inputArr) { - let len = inputArr.length; - let swapped = false; - for (let i = 1; i <= len - 1; i++) { - swapped = false; - for (let j = 0; j < len - 1; j++) { - if (inputArr[j] > inputArr[j + 1]) { - let temp = inputArr[j]; - inputArr[j] = inputArr[j + 1]; - inputArr[j + 1] = temp; - swapped = true - } - } - if (swapped === false) break; - } - return (inputArr) + for (let i = inputArr.length - 1; i > 0; i--) { + let hasChange = false; + for (let j = 0; j < i; j++) { + if (inputArr[j] > inputArr[j + 1]) { + const temp = inputArr[j]; + inputArr[j] = inputArr[j + 1]; + inputArr[j + 1] = temp; + hasChange = true; + } + } + + if (!hasChange) { + break; + } + } + + return inputArr; } -let arr = [6, 3, 2, 1, 5]; -console.log(bubbleSort(arr)) +const arr = [6, 3, 2, 1, 5]; +console.log(bubbleSort(arr)); ``` ### **Go** From 21b6cd3b2e38189d885c4a7a1d03f1656b822013 Mon Sep 17 00:00:00 2001 From: YangQi <70502828+YangFong@users.noreply.github.com> Date: Wed, 22 Dec 2021 17:46:58 +0800 Subject: [PATCH 2/5] style: adjust code indentation --- basic/sorting/BubbleSort/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basic/sorting/BubbleSort/README.md b/basic/sorting/BubbleSort/README.md index 95fff1d733a8d..94101fe44eb82 100644 --- a/basic/sorting/BubbleSort/README.md +++ b/basic/sorting/BubbleSort/README.md @@ -60,7 +60,7 @@ function bubbleSort(inputArr) { break; } } - + return inputArr; } From 6628ba95f15441c04a45a2fe9370211246264a4f Mon Sep 17 00:00:00 2001 From: YangQi <70502828+YangFong@users.noreply.github.com> Date: Wed, 22 Dec 2021 18:09:30 +0800 Subject: [PATCH 3/5] style: adjust code indentation --- basic/sorting/BubbleSort/README.md | 34 +++++++++++++++--------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/basic/sorting/BubbleSort/README.md b/basic/sorting/BubbleSort/README.md index 94101fe44eb82..03d25c487c5d9 100644 --- a/basic/sorting/BubbleSort/README.md +++ b/basic/sorting/BubbleSort/README.md @@ -45,23 +45,23 @@ public class BubbleSort { ```js function bubbleSort(inputArr) { - for (let i = inputArr.length - 1; i > 0; i--) { - let hasChange = false; - for (let j = 0; j < i; j++) { - if (inputArr[j] > inputArr[j + 1]) { - const temp = inputArr[j]; - inputArr[j] = inputArr[j + 1]; - inputArr[j + 1] = temp; - hasChange = true; - } - } - - if (!hasChange) { - break; - } - } - - return inputArr; + for (let i = inputArr.length - 1; i > 0; i--) { + let hasChange = false; + for (let j = 0; j < i; j++) { + if (inputArr[j] > inputArr[j + 1]) { + const temp = inputArr[j]; + inputArr[j] = inputArr[j + 1]; + inputArr[j + 1] = temp; + hasChange = true; + } + } + + if (!hasChange) { + break; + } + } + + return inputArr; } const arr = [6, 3, 2, 1, 5]; From c87242e46f21d27ebba2a763a40298ecb8e8581d Mon Sep 17 00:00:00 2001 From: YangQi <70502828+YangFong@users.noreply.github.com> Date: Wed, 22 Dec 2021 21:31:15 +0800 Subject: [PATCH 4/5] docs: Add a description of the solution --- .../README.md" | 5 +++++ 1 file changed, 5 insertions(+) diff --git "a/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" index 318f4499a872b..cf7a787c60fc0 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" @@ -29,6 +29,11 @@ ## 解法 +- 两个栈,一个负责输入,一个负责输出 +- 入队的元素放入输入栈当中 +- 当出队时,将输入栈当中的所有元素出栈,放入输出栈当中,让栈底的元素跑到最上面来 +- 只有输出栈中没有元素时才进行倒放,不必每一次出队前都进行进行倒放 + ### **Python3** From c67f1011c74e547f2fa51f4118d9114b06a0d144 Mon Sep 17 00:00:00 2001 From: YangQi <70502828+YangFong@users.noreply.github.com> Date: Wed, 22 Dec 2021 21:40:25 +0800 Subject: [PATCH 5/5] refactor: optimizing Java examples --- .../README.md" | 56 +++++++++---------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git "a/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" index cf7a787c60fc0..1adc61378b1bd 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" @@ -70,33 +70,29 @@ class CQueue: ### **Java** ```java +import java.util.Stack; + class CQueue { + private Stack stack_1; + private Stack stack_2; - private Deque s1; - private Deque s2; public CQueue() { - s1 = new ArrayDeque<>(); - s2 = new ArrayDeque<>(); + stack_1 = new Stack<>(); + stack_2 = new Stack<>(); } public void appendTail(int value) { - s1.push(value); - if (s2.isEmpty()) { - move(); - } + stack_1.push(value); } public int deleteHead() { - if (s2.isEmpty()) { - move(); + if (stack_2.empty()) { + while (!stack_1.empty()) { + stack_2.push(stack_1.pop()); + } } - return s2.isEmpty() ? -1 : s2.pop(); - } - private void move() { - while (!s1.isEmpty()) { - s2.push(s1.pop()); - } + return stack_2.empty() ? -1 : stack_2.pop(); } } @@ -112,32 +108,32 @@ class CQueue { ```js var CQueue = function () { - this.data = []; - this.helper = []; + this.data = []; + this.helper = []; }; /** * @param {number} value * @return {void} */ CQueue.prototype.appendTail = function (value) { - this.data.push(value); + this.data.push(value); }; /** * @return {number} */ CQueue.prototype.deleteHead = function () { - if (this.data.length) { - while (this.data.length > 1) { - this.helper.push(this.data.pop()); - } - let res = this.data.pop(); - while (this.helper.length) { - this.data.push(this.helper.pop()); + if (this.data.length) { + while (this.data.length > 1) { + this.helper.push(this.data.pop()); + } + let res = this.data.pop(); + while (this.helper.length) { + this.data.push(this.helper.pop()); + } + return res; + } else { + return -1; } - return res; - } else { - return -1; - } }; ```