leetcode398】的更多相关文章

Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array. Note:The array size can be very large. Solution that uses too much extra spa…
public class Solution { int[] nums; Random rnd; public Solution(int[] nums) { this.nums = nums; this.rnd = new Random(); } public int pick(int target) { ; ; ; i < nums.length; i++) { if (nums[i] != target) continue; ) result = i; } return result; } }…
382. 链表随机节点 给定一个单链表,随机选择链表的一个节点,并返回相应的节点值.保证每个节点被选的概率一样. 进阶:如果链表十分大且长度未知,如何解决这个问题?你能否使用常数级空间复杂度实现? 示例: // 初始化一个单链表 [1,2,3]. ListNode head = new ListNode(1); head.next = new ListNode(2); head.next.next = new ListNode(3); Solution solution = new Soluti…