Why not do it like 381?


  • 0
    J

    public class Solution {
    HashMap<Integer, LinkedList<Integer>> map;

    public Solution(int[] nums) {
        
        map = new HashMap<Integer, LinkedList<Integer>>();
        //list = new Linkedlist<Integer>();
        
        for (int i = 0; i < nums.length; i++) {
            if (!map.containsKey(nums[i])) {
                map.put(nums[i], new LinkedList<Integer>());
            }
            map.get(nums[i]).add(i);
            
        }
        
    }
    
    public int pick(int target) {
        Random get = new Random();
        LinkedList<Integer> p = map.get(target);
        int read = get.nextInt(p.size());
       // return p.size();
    
        
        return map.get(target).get(read);
    }
    

    }


Log in to reply
 

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