You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class Solution {
public int longestSubarray(int[] nums) {
int max = 0;
List<Integer> list = new ArrayList<>();
for (int i = 0; i < nums.length; ++i) {
if (nums[i] > max) {
list.clear();
list.add(i);
max = nums[i];
} else if (nums[i] == max) {
list.add(i);
}
}
int result = 1;
int len = 0;
int prev = -1;
for (Integer integer : list) {
if (prev == -1 || integer == prev + 1) {
++len;
result = Math.max(result, len);
} else {
len = 1;
}
prev = integer;
}
return result;
}
}
这道题中,一开始看到subArray想到利用滑动窗口的方法,但问题是:
所以要冷静,逻辑思考。
The text was updated successfully, but these errors were encountered: