Skip to content

Commit 7f8630a

Browse files
authored
Merge branch 'youngyangyang04:master' into master
2 parents 17a3f7c + 77c1098 commit 7f8630a

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed

problems/0135.分发糖果.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,30 +132,33 @@ public:
132132
Java:
133133
```java
134134
class Solution {
135+
/**
136+
分两个阶段
137+
1、起点下标1 从左往右,只要 右边 比 左边 大,右边的糖果=左边 + 1
138+
2、起点下标 ratings.length - 2 从右往左, 只要左边 比 右边 大,此时 左边的糖果应该 取本身的糖果数(符合比它左边大) 和 右边糖果数 + 1 二者的最大值,这样才符合 它比它左边的大,也比它右边大
139+
*/
135140
public int candy(int[] ratings) {
136-
int[] candy = new int[ratings.length];
137-
for (int i = 0; i < candy.length; i++) {
138-
candy[i] = 1;
139-
}
140-
141+
int[] candyVec = new int[ratings.length];
142+
candyVec[0] = 1;
141143
for (int i = 1; i < ratings.length; i++) {
142144
if (ratings[i] > ratings[i - 1]) {
143-
candy[i] = candy[i - 1] + 1;
145+
candyVec[i] = candyVec[i - 1] + 1;
146+
} else {
147+
candyVec[i] = 1;
144148
}
145149
}
146150
147151
for (int i = ratings.length - 2; i >= 0; i--) {
148152
if (ratings[i] > ratings[i + 1]) {
149-
candy[i] = Math.max(candy[i],candy[i + 1] + 1);
153+
candyVec[i] = Math.max(candyVec[i], candyVec[i + 1] + 1);
150154
}
151155
}
152156
153-
int count = 0;
154-
for (int i = 0; i < candy.length; i++) {
155-
count += candy[i];
157+
int ans = 0;
158+
for (int s : candyVec) {
159+
ans += s;
156160
}
157-
158-
return count;
161+
return ans;
159162
}
160163
}
161164
```

problems/0763.划分字母区间.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,15 @@ Java:
8888
class Solution {
8989
public List<Integer> partitionLabels(String S) {
9090
List<Integer> list = new LinkedList<>();
91-
int[] edge = new int[123];
91+
int[] edge = new int[26];
9292
char[] chars = S.toCharArray();
9393
for (int i = 0; i < chars.length; i++) {
94-
edge[chars[i] - 0] = i;
94+
edge[chars[i] - 'a'] = i;
9595
}
9696
int idx = 0;
9797
int last = -1;
9898
for (int i = 0; i < chars.length; i++) {
99-
idx = Math.max(idx,edge[chars[i] - 0]);
99+
idx = Math.max(idx,edge[chars[i] - 'a']);
100100
if (i == idx) {
101101
list.add(i - last);
102102
last = i;

problems/1002.查找常用字符.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -224,32 +224,28 @@ javaScript
224224
var commonChars = function (words) {
225225
let res = []
226226
let size = 26
227-
let firstHash = new Array(size)
228-
for (let i = 0; i < size; i++) { // 初始化 hash 数组
229-
firstHash[i] = 0
230-
}
227+
let firstHash = new Array(size).fill(0) // 初始化 hash 数组
231228

232229
let a = "a".charCodeAt()
233230
let firstWord = words[0]
234231
for (let i = 0; i < firstWord.length; i++) { // 第 0 个单词的统计
235232
let idx = firstWord[i].charCodeAt()
236233
firstHash[idx - a] += 1
237234
}
238-
235+
236+
let otherHash = new Array(size).fill(0) // 初始化 hash 数组
239237
for (let i = 1; i < words.length; i++) { // 1-n 个单词统计
240-
let otherHash = new Array(size)
241-
for (let i = 0; i < size; i++) { // 初始化 hash 数组
242-
otherHash[i] = 0
243-
}
244-
245238
for (let j = 0; j < words[i].length; j++) {
246239
let idx = words[i][j].charCodeAt()
247240
otherHash[idx - a] += 1
248241
}
242+
249243
for (let i = 0; i < size; i++) {
250244
firstHash[i] = Math.min(firstHash[i], otherHash[i])
251245
}
246+
otherHash.fill(0)
252247
}
248+
253249
for (let i = 0; i < size; i++) {
254250
while (firstHash[i] > 0) {
255251
res.push(String.fromCharCode(i + a))

0 commit comments

Comments
 (0)