leetcode382
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution { ListNode head;
Random random;
/** @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 Solution(ListNode head)
{
this.head = head;
random = new Random();
} /** Returns a random node's value. */
public int GetRandom()
{
ListNode c = this.head;
int r = c.val;
for (int i = ; c.next != null; i++)
{
c = c.next;
if (random.Next(i + ) == i)
{
r = c.val;
}
} return r;
}
} /**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(head);
* int param_1 = obj.GetRandom();
*/
https://leetcode.com/problems/linked-list-random-node/#/description
leetcode382的更多相关文章
- [Swift]LeetCode382. 链表随机节点 | Linked List Random Node
Given a singly linked list, return a random node's value from the linked list. Each node must have t ...
随机推荐
- hiredis处理zscan和hscan的reply
zscan的返回值可以看做是一个二维数组,第一维包含两个元素:string类型的游标cursor和集合元素数组:第二维即集合元素数组,这个数组交替存放着集合元素和score,元素和score也都是st ...
- java sundry tips
1.关于Arrays 记得binarySearch方法返回的int 类型的数值的含义. If the array contains multiple elements with the spec ...
- HDU - 1142:A Walk Through the Forest (拓扑排序)
Jimmy experiences a lot of stress at work these days, especially since his accident made working dif ...
- Java 输出文件通过 BufferedWriter.newline() 方法换行
最近项目中需要导出文件,其实导出文件是一个挺简单的事情.但是却遇到了很奇怪的问题. 首先导出到文件需要用到 BufferedWriter.而换行则是通过 bw.newline() 方法,问题将出在 n ...
- Postman 上传文件
一.选择post请求方式,输入请求地址 二.填写Headers Key:Content-Type Value:multipart/form-data 三.填写body 选择form-data 然后选择 ...
- iOS中scrollview自动滚动的实现
http://bbs.csdn.net/topics/390347330 原问题是,我要展现给用户的内容放在scrollview中,让内容从上到底自动滚动,我最开始用的是DDAutoscrollvie ...
- MySQL排序_20160926
在工作中对数据进行排序也是最常用的,比如根据用户的下单金额降序 或者对销售业绩进行降序排序 在考核员工KPI时候也经常用到 一.order by 函数 order by 函数默认根据后面字段升序,使 ...
- 【oracle】Oracle中as关键字
在Oracle中as关键字不能用于指定表的别名 在Oracle中指定表的别名时只需在原有表名和表的别名之间用空格分隔即可 但as关键字可以用于指定列的别名 但在存储过程中如果列的别名与原有列名相同,在 ...
- openresty 几个插件使用
1. jwt opm get SkyLothar/lua-resty-jwt 2. cookie opm get p0pr0ck5/lua-resty-cookie 3. http ...
- 【Oracle】OGG单向复制配置
实验环境: 源端: Ip:192.168.40.10 DataBase:Oracle 11.2.0.1.0 ORCL OS:OEL5.6 OGG:fbo_ggs_Linux_x86_ora11g_32 ...