全部评论
单调栈
https://blog.csdn.net/Broken_Wave/article/details/82390882 import java.util.Stack;
public class FindRightMax {
public static void main(String[] arg) {
int array[]=new int[] {1,5,3,6,4,8,9,10};
int res[]=findMax(array);
for(int num:res) {
System.out.println(num);
}
}
public static int[] findMax(int[] array) {
int len =array.length;
Stack<Integer> st = new Stack<Integer>();
int res[]=new int[len];
int i=0;
while(i<len) {
if(st.isEmpty()||array[i]<=array[st.peek()]) {
st.push(i);
i++;
}else {
res[st.pop()]=array[i];
}
}
while(!st.isEmpty()) {
res[st.pop()]=-1;
}
return res;
}
}
我若干个月前也是这道题………
function firstLargeNum(num) { let i = 0; let ans = [], stack = []; for (i = num.length - 1; i >= 0; i--) { while (stack.length > 0) { let top = stack[stack.length - 1]; if (top > num[i]) { ans.unshift(top); break; } else { stack.pop(); } } if (stack.length === 0) { ans.unshift(-1); } stack.push(num[i]); } return ans; }
相关推荐
11-10 22:06
上海震旦职业学院 前端工程师 点赞 评论 收藏
分享