Saturday, October 5, 2013

Copy List with Random Pointers

Copy List with Random Pointer

 
A linked list is given such that each node contains an additional random pointer which 
could point to any node in the list or null.
Return a deep copy of the list.

Gas Station

Gas Station

 
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from 
station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
Return the starting gas station's index if you can travel around the circuit once, otherwise return -1.
Note:
The solution is guaranteed to be unique.

Clone Graph

Clone Graph

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.
OJ's undirected graph serialization:
Nodes are labeled uniquely.
We use # as a separator for each node, and , as a separator for node label and each
neighbor of the node.
As an example, consider the serialized graph {0,1,2#1,2#2,2}.
The graph has a total of three nodes, and therefore contains three parts as separated by #.
  1. First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
  2. Second node is labeled as 1. Connect node 1 to node 2.
  3. Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a  self-cycle.

Friday, October 4, 2013

Single Number

Single Number

Given an array of integers, every element appears twice except for one. 
Find that single one.
Note:
Your algorithm should have a linear runtime complexity. 
Could you implement it without using extra memory?

Single Number II


Single Number II

Given an array of integers, every element appears three times except for one. 
Find that single one.
Note:
Your algorithm should have a linear runtime complexity.
Could you implement it without using extra memory?