random-pick-index

本文介绍了一种基于LeetCode的题目实现方案,通过构造一个类`Solution`来完成对数组中特定目标值的随机选取。该实现使用了哈希映射来存储每个数值对应的索引列表,并利用随机数生成器从列表中选择一个索引来返回。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://leetcode.com/problems/random-pick-index/

public class Solution {

    private Map mp;
    private Random rand;
    
    public Solution(int[] nums) {
        mp = new HashMap();
        for (int i=0; i<nums.length; i++) {
            List lt = (ArrayList)mp.remove(nums[i]);
            if (lt == null) {
                lt = new ArrayList();
            }
            lt.add(i);
            mp.put(nums[i], lt);
        }
        rand = new Random();
    }
    
    public int pick(int target) {
        List lt = (ArrayList)mp.get(target);
        return (int)lt.get(rand.nextInt(lt.size()));
    }
}

/**
 * Your Solution object will be instantiated and called as such:
 * Solution obj = new Solution(nums);
 * int param_1 = obj.pick(target);
 */

 

def spatially_regular_gen(): # Generator loop for i in range(num_per_epoch): # Choose the cloud with the lowest probability cloud_idx = int(np.argmin(self.min_possibility[split])) # choose the point with the minimum of possibility in the cloud as query point point_ind = np.argmin(self.possibility[split][cloud_idx]) # Get all points within the cloud from tree structure points = np.array(self.input_trees[split][cloud_idx].data, copy=False) # Center point of input region center_point = points[point_ind, :].reshape(1, -1) # Add noise to the center point noise = np.random.normal(scale=cfg.noise_init / 10, size=center_point.shape) pick_point = center_point + noise.astype(center_point.dtype) # Check if the number of points in the selected cloud is less than the predefined num_points if len(points) < cfg.num_points: # Query all points within the cloud queried_idx = self.input_trees[split][cloud_idx].query(pick_point, k=len(points))[1][0] else: # Query the predefined number of points queried_idx = self.input_trees[split][cloud_idx].query(pick_point, k=cfg.num_points)[1][0] # Shuffle index queried_idx = DP.shuffle_idx(queried_idx) # Get corresponding points and colors based on the index queried_pc_xyz = points[queried_idx] queried_pc_xyz = queried_pc_xyz - pick_point queried_pc_colors = self.input_colors[split][cloud_idx][queried_idx] queried_pc_labels = self.input_labels[split][cloud_idx][queried_idx] # Update the possibility of the selected points dists = np.sum(np.square((points[queried_idx] - pick_po
04-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值