Java use ArrayList


  • 0
    G

    Here is my code,just use ArrayList to get the index.

    public class Solution {
        public int[] nextGreaterElement(int[] findNums, int[] nums) {
        int[] ret=new int[findNums.length];
        ArrayList<Integer> al=new ArrayList<Integer>();
        for(int n:nums) al.add(n);
        for(int i=0;i<findNums.length;i++){
            int currInd=al.indexOf(findNums[i])+1;
            for(int j=currInd;j<=nums.length;j++){
            if(j==nums.length){ret[i]=-1;}    
            else if(nums[j]>findNums[i]){ret[i]=nums[j];break;}
            else{ret[i]=-1;}
        }}
        return ret;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.