leetcode 382. Linked List Random Node

257 篇文章 17 订阅

Given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen.

Follow up:
What if the linked list is extremely large and its length is unknown to you? Could you solve this efficiently without using extra space?

Example:

// Init a singly linked list [1,2,3].
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
Solution solution = new Solution(head);

// getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.
solution.getRandom();

这一题我的解法很没水准。

package leetcode;

import java.util.ArrayList;

public class Linked_List_Random_Node_382 {
	ArrayList<Integer> arrayList=new ArrayList<Integer>();

	/**
	 * @param head
	 *            The linked list's head. Note that the head is guaranteed to be
	 *            not null, so it contains at least one node.
	 */
	public Linked_List_Random_Node_382(ListNode head) {
		while(head!=null){
			arrayList.add(head.val);
			head=head.next;
		}
	}

	/** Returns a random node's value. */
	public int getRandom() {
		int randomIndex=(int)Math.floor(Math.random()*arrayList.size());
		return arrayList.get(randomIndex);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub		
		ListNode head=new ListNode(1);
		head.next=new ListNode(2);
		head.next.next=new ListNode(3);
		Linked_List_Random_Node_382 l=new Linked_List_Random_Node_382(head);
		System.out.println(l.getRandom());
	}

}
这其实是一道思维题。使用大数据中的蓄水池抽样算法。 http://blog.jobbole.com/42550/ 
另外,Random.nextint() 和Math.random()的区别:前者生成的随机数效率高于后者。
public class Solution {

    /** @param head The linked list's head. Note that the head is guanranteed to be not null, so it contains at least one node. */
    ListNode head = null;
    Random randomGenerator = null;
    public Solution(ListNode head) {
        this.head = head;
        this.randomGenerator = new Random();

    }
    
    /** Returns a random node's value. */
    public int getRandom() {
        ListNode result = null;
        ListNode current = head;
        
        for(int n = 1; current!=null; n++) {
            if (randomGenerator.nextInt(n) == 0) {//如nextInt(101)随机产生一个大于等于0,小于101的整形数。
                result = current;
            }
            current = current.next;
        }
        
        return result.val;
        
    }
}
用例子来说明这个算法:假设数组是[1,2,3]。
若结果是1被选中,那么过程中:
        n=1时,current=1,被选中(使得nextInt(1)为0,就是0中选0)概率1/1; 
        n=2时,current=2,不被选中(使得nextInt(2)不为0,就是0,1中选1)的概率是1/2;
        n=3时,current=3,不被选中(使得nextInt(3)不为0,就是0,1,2中选1或2)的概率是2/3;
        概率:(1/1)*(1/2)*(2/3)=1/3
若结果是2被选中,那么过程中:
        n=1时,不需再看,因为这个情况是2被选中,说明下一个循环肯定有result=2,那么n=1无论怎样都无妨,概率1/1
        n=2时,current=2,被选中(使得nextInt(2)为0,就是0,1中选0)的概率是1/2;
        n=3时,current=3,不被选中(使得nextInt(3)不为0,就是0,1,2中选1或2)的概率是2/3;
        概率: (1/1)*(1/2)*(2/3)=1/3
若结果是3被选中,那么过程中:
         n=1时,n=2时,不需再看,因为这个情况是3被选中,说明下一个循环肯定有result=3,那么n=1和n=2时无论选什么都无妨,概率1/1
        n=3时,current=3,被选中(使得nextInt(3)为0,就是0,1,2中选0)的概率是1/3;
        概率: (1/1)*(1/3)=1/3


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值