File tree Expand file tree Collapse file tree 3 files changed +24
-25
lines changed Expand file tree Collapse file tree 3 files changed +24
-25
lines changed Original file line number Diff line number Diff line change @@ -132,30 +132,33 @@ public:
132
132
Java:
133
133
```java
134
134
class Solution {
135
+ /**
136
+ 分两个阶段
137
+ 1、起点下标1 从左往右,只要 右边 比 左边 大,右边的糖果=左边 + 1
138
+ 2、起点下标 ratings.length - 2 从右往左, 只要左边 比 右边 大,此时 左边的糖果应该 取本身的糖果数(符合比它左边大) 和 右边糖果数 + 1 二者的最大值,这样才符合 它比它左边的大,也比它右边大
139
+ */
135
140
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;
141
143
for (int i = 1; i < ratings.length; i++) {
142
144
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;
144
148
}
145
149
}
146
150
147
151
for (int i = ratings.length - 2; i >= 0; i--) {
148
152
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);
150
154
}
151
155
}
152
156
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 ;
156
160
}
157
-
158
- return count;
161
+ return ans;
159
162
}
160
163
}
161
164
```
Original file line number Diff line number Diff line change @@ -88,15 +88,15 @@ Java:
88
88
class Solution {
89
89
public List<Integer> partitionLabels(String S) {
90
90
List<Integer> list = new LinkedList<>();
91
- int[] edge = new int[123 ];
91
+ int[] edge = new int[26 ];
92
92
char[] chars = S.toCharArray();
93
93
for (int i = 0; i < chars.length; i++) {
94
- edge[chars[i] - 0 ] = i;
94
+ edge[chars[i] - 'a' ] = i;
95
95
}
96
96
int idx = 0;
97
97
int last = -1;
98
98
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' ]);
100
100
if (i == idx) {
101
101
list.add(i - last);
102
102
last = i;
Original file line number Diff line number Diff line change @@ -224,32 +224,28 @@ javaScript
224
224
var commonChars = function (words ) {
225
225
let res = []
226
226
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 数组
231
228
232
229
let a = " a" .charCodeAt ()
233
230
let firstWord = words[0 ]
234
231
for (let i = 0 ; i < firstWord .length ; i++ ) { // 第 0 个单词的统计
235
232
let idx = firstWord[i].charCodeAt ()
236
233
firstHash[idx - a] += 1
237
234
}
238
-
235
+
236
+ let otherHash = new Array (size).fill (0 ) // 初始化 hash 数组
239
237
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
-
245
238
for (let j = 0 ; j < words[i].length ; j++ ) {
246
239
let idx = words[i][j].charCodeAt ()
247
240
otherHash[idx - a] += 1
248
241
}
242
+
249
243
for (let i = 0 ; i < size; i++ ) {
250
244
firstHash[i] = Math .min (firstHash[i], otherHash[i])
251
245
}
246
+ otherHash .fill (0 )
252
247
}
248
+
253
249
for (let i = 0 ; i < size; i++ ) {
254
250
while (firstHash[i] > 0 ) {
255
251
res .push (String .fromCharCode (i + a))
You can’t perform that action at this time.
0 commit comments