From ebe4ea6db537c74ef03d10b6d1bda283c6cf522d Mon Sep 17 00:00:00 2001 From: KailokFung Date: Sat, 16 Oct 2021 22:28:53 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix(0135):=20=E5=8A=A0=E4=BA=86=E6=B3=A8?= =?UTF-8?q?=E9=87=8A=EF=BC=8C=E6=AF=94=E5=8E=9F=E5=85=88=E5=B0=91=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=E9=81=8D=E5=8E=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0135.分发糖果.md | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/problems/0135.分发糖果.md b/problems/0135.分发糖果.md index ace6bc7d..f3c00536 100644 --- a/problems/0135.分发糖果.md +++ b/problems/0135.分发糖果.md @@ -132,30 +132,33 @@ public: Java: ```java class Solution { + /** + 分两个阶段 + 1、起点下标1 从左往右,只要 右边 比 左边 大,右边的糖果=左边 + 1 + 2、起点下标 ratings.length - 2 从右往左, 只要左边 比 右边 大,此时 左边的糖果应该 取本身的糖果数(符合比它左边大) 和 右边糖果数 + 1 二者的最大值,这样才符合 它比它左边的大,也比它右边大 + */ public int candy(int[] ratings) { - int[] candy = new int[ratings.length]; - for (int i = 0; i < candy.length; i++) { - candy[i] = 1; - } - + int[] candyVec = new int[ratings.length]; + candyVec[0] = 1; for (int i = 1; i < ratings.length; i++) { if (ratings[i] > ratings[i - 1]) { - candy[i] = candy[i - 1] + 1; + candyVec[i] = candyVec[i - 1] + 1; + } else { + candyVec[i] = 1; } } for (int i = ratings.length - 2; i >= 0; i--) { if (ratings[i] > ratings[i + 1]) { - candy[i] = Math.max(candy[i],candy[i + 1] + 1); + candyVec[i] = Math.max(candyVec[i], candyVec[i + 1] + 1); } } - int count = 0; - for (int i = 0; i < candy.length; i++) { - count += candy[i]; + int ans = 0; + for (int s : candyVec) { + ans += s; } - - return count; + return ans; } } ``` From d92d7207cb8ec4d8628c41e8aaeca6145ca76823 Mon Sep 17 00:00:00 2001 From: Luo <82520819+Jerry-306@users.noreply.github.com> Date: Sun, 17 Oct 2021 10:20:19 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E7=AE=80=E5=8C=96=201002=20=E6=9F=A5?= =?UTF-8?q?=E6=89=BE=E7=9B=B8=E5=90=8C=E5=AD=97=E7=AC=A6=20JavaScript?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 对两个数组的初值写法进行简化 --- problems/1002.查找常用字符.md | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/problems/1002.查找常用字符.md b/problems/1002.查找常用字符.md index 09e49c4f..44a02ceb 100644 --- a/problems/1002.查找常用字符.md +++ b/problems/1002.查找常用字符.md @@ -224,10 +224,7 @@ javaScript var commonChars = function (words) { let res = [] let size = 26 - let firstHash = new Array(size) - for (let i = 0; i < size; i++) { // 初始化 hash 数组 - firstHash[i] = 0 - } + let firstHash = new Array(size).fill(0) // 初始化 hash 数组 let a = "a".charCodeAt() let firstWord = words[0] @@ -235,21 +232,20 @@ var commonChars = function (words) { let idx = firstWord[i].charCodeAt() firstHash[idx - a] += 1 } - + + let otherHash = new Array(size).fill(0) // 初始化 hash 数组 for (let i = 1; i < words.length; i++) { // 1-n 个单词统计 - let otherHash = new Array(size) - for (let i = 0; i < size; i++) { // 初始化 hash 数组 - otherHash[i] = 0 - } - for (let j = 0; j < words[i].length; j++) { let idx = words[i][j].charCodeAt() otherHash[idx - a] += 1 } + for (let i = 0; i < size; i++) { firstHash[i] = Math.min(firstHash[i], otherHash[i]) } + otherHash.fill(0) } + for (let i = 0; i < size; i++) { while (firstHash[i] > 0) { res.push(String.fromCharCode(i + a)) From cb9a717a7ccdbb6063837a231b7a1a1d45258772 Mon Sep 17 00:00:00 2001 From: KailokFung Date: Mon, 18 Oct 2021 11:01:38 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix(0763):=20=E4=B8=8B=E6=A0=87=E5=8F=96=20?= =?UTF-8?q?=E5=AD=97=E6=AF=8D=20-=20'a'=20=E7=9A=84=E5=80=BC=EF=BC=8C?= =?UTF-8?q?=E5=B0=91=E7=82=B9=E7=A9=BA=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0763.划分字母区间.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/problems/0763.划分字母区间.md b/problems/0763.划分字母区间.md index 863b569d..c64ff3c8 100644 --- a/problems/0763.划分字母区间.md +++ b/problems/0763.划分字母区间.md @@ -88,15 +88,15 @@ Java: class Solution { public List partitionLabels(String S) { List list = new LinkedList<>(); - int[] edge = new int[123]; + int[] edge = new int[26]; char[] chars = S.toCharArray(); for (int i = 0; i < chars.length; i++) { - edge[chars[i] - 0] = i; + edge[chars[i] - 'a'] = i; } int idx = 0; int last = -1; for (int i = 0; i < chars.length; i++) { - idx = Math.max(idx,edge[chars[i] - 0]); + idx = Math.max(idx,edge[chars[i] - 'a']); if (i == idx) { list.add(i - last); last = i;