zoukankan      html  css  js  c++  java
  • 1030.链表中下一个最大的节点


    这道题就是找自己大的那个数就好 简单的解法

    class Solution {
    	public int[] nextLargerNodes(ListNode head) {
    		int arr[] = new int[100001];
    		int flag = 0;
    		while (head != null) {
    			arr[flag++] = head.val;
    			head = head.next;
    		}
    		int res[] = new int[flag];
    		for (int i = 0; i < flag; i++) {
    			for (int k = i + 1; k < flag; k++) {
    				if (arr[k] > arr[i]) {
    					res[i] = arr[k];
    					break;
    				}
    			}
    		}
    		return res;
    	}
    }
    

    后面增加用栈解的方法

    class Solution {
    	public int[] nextLargerNodes(ListNode head) {
    		ArrayList<Integer> al = new ArrayList<Integer>();
    		while(head!=null)
            {al.add(head.val);
    			head = head.next;
    		}
    		int res[] = new int [al.size()];
    		Stack<Integer> s = new Stack<Integer>();
    		for (int i = 0; i < al.size(); ++i) {
                while (!s.isEmpty() && al.get(s.peek()) < al.get(i))
                    res[s.pop()] = al.get(i);
                s.push(i);
            }
    		return res;
    	}
    }
    
  • 相关阅读:
    android 源码编译 问题 列表
    springboot总结
    设计模式学习笔记
    JWT入门1
    oauth2入门github
    mybatis面试题
    shiro入门
    knife4j swagger API文档
    pahole安装(编译)
    goMySql的逆向工程
  • 原文地址:https://www.cnblogs.com/cznczai/p/11150371.html
Copyright © 2011-2022 走看看