java O(n^2) solution beat 89%


  • 0
    J
            Map<Integer,Integer> m = new HashMap<Integer,Integer>();
            for(int i=0;i<nums.length;++i){
                boolean find = false;
                for(int j=i;j<nums.length;++j){
                    if(nums[i] < nums[j])
                    {
                        m.put(nums[i],nums[j]);
                        find = true;
                        break;
                    }
                }
                if(!find)
                    m.put(nums[i],-1);
            }
            int[] res = new int[findNums.length];
            for(int i=0;i<findNums.length;++i)
                res[i] = m.get(findNums[i]);
            return res;
    

Log in to reply
 

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