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 ...
随机推荐
- 每天一个linux命令:【转载】cat命令
cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. 1.命令格式: cat [选项] [文件] ...
- python当前路径
os.getcwd()获取系统路径 sys.path [0]获取当前路径
- web 优化原则
1. 减少http 请求 2. 使用CDN 3. 添加expires 头 4. gzip 压缩 5. 样式表放在头部 6. 脚本放底部 7. 避免css 表达式 8. 使用 ...
- pthread中读写锁
读写锁很像一个互斥量,他阻止多个线程同时修改共享数据的另一种方法,区分不同互斥量的是他是分读数据和写数据,一个读写锁允许同时多个线程读数据,只要他们不修改数据. 只要没有写模式下的加锁,任意线程都可以 ...
- spring容器启动
1 主要类 ContextLoaderListener:注册在web.xml中,web应用启动时,会创建它,并回调它的initWebApplicationContext()方法,从而创建并启动spri ...
- sql的一些事件处理
select getdate() select Convert(varchar(10),getdate(),120) yyyy-mm-ddselect Convert(varchar(20),getd ...
- Tomcat(64位)免安装版的环境安装与配置
本篇博客主要介绍Tomcat(64位)免安装版的环境安装与配置,该篇文章同样适合于32位Tomcat免安装版的环境安装与配置. 该篇博客中的大部分内容同百度经验中的<出现unable to op ...
- Java格式化时间为String类型
SimpleDateFormat ormater = new SimpleDateFormat("yyyy-MM-dd"); Date date=new Date(); Strin ...
- Jenkins的项目管理
新建Item 使用Jenkins最重要的是能够创建一些工作流,除了部署,还能做很多流程上的事情.同样,一条条项目建起来需要做一定的管理,在Jenkins首页Jenkins->新建可以按自己的需要 ...
- nginx statistics in multi-workers
1. 从网上下载多进程统计补丁.https://github.com/arut/nginx-patches 2. Copy补丁文件per-worker-listener到nginx ...