public TreeMap userAccount;
public TreeMap messagePool;
public hw1_BOM()
{
userAccount=new TreeMap();
messagePool=new TreeMap();
}
public class mPool
{
public int mparentID;
public String mUsername;
public String mSubject;
public String mText;
public mPool(int parentid, String username, String subject, String text)
{
mparentID=parentid;
mUsername=username;
mSubject=subject;
mText=text;
}
}
***************what's wrong with this one below?**********
m
in Cassandra, concurrentSkipListMap put() and get() takes the majority of
read access,
actually in this spot we do not need explicit synchronization since the
caller is just one thread. so TreeMap is ok. but TreeMap is not any faster
than concurrentSkipListMap.
so is there a faster sortedMap than TreeMap ?
thanks
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class UberTest {
List data = new ArrayList<>();
public void fillData(){
//Use integer to denote Time here.. you can change it to DateTime
type.
data.add(new TimedKvPair(new KvPair("name1","Bnn"), 1) );
data.add(new TimedKvPair(new KvPair("name2","Dee"), 2) );
data.add(new Time... 阅读全帖
API是有很多interface,同时API也有很多class已经实现了这些interface,这些i
nterface存在的原因一是可以让用户来实现自己的class,二是可以generic的使用
实现同一interface的classes。
ie. interface Map <- TreeMap, HashMap.
you can code as:
1: Map m;
2: m = new TreeMap();
3: use m as a generic Map.
later on if you want to change TreeMap to HashMap.
use only need to change the line 2 to "m = new HashMap();"
TreeMap> map;
iterate each word in book from front to back
if word found in map, append current page number to ArrayList
if word not found in map, put to map, append current page number
TreeMap automatically sort on key, which is String type word
iterate map to generate "word -> page number" pairs
有人知道TreeMap higherKey的复杂度吗?如果是log(n)的话,那 TreeMap查找log(n),
合并就要n(logn)的复杂度了。那还不如ArrayList了。
ArrayList: search logn, insert n
LinkedList: search n, insert n
貌似ArrayList复杂度最好了。当然写起来更费劲些。今天有时间练练。
电面2:
还是一个国人大哥,LeetCode上的Insert Interval,API稍微有点变化,给的是一个链
表节点。
由于还有时间,还考了一个count tweets的设计题。需要实现如下API:
class CountingSvc {
void tweet(long timestamp, int tweetLength);
double avgLength(long begin, long end, long threshold);
};
另外给了一个hint,TreeMap,让自己customize一下object。
可惜我一看是range query就往线段树上想了,于是给出了如下结构,并解释了原理。
class Node {
long totalLen;
long count;
long begin;
long end;
Node left;
Node right;
}
最后追问了下如何去做模糊处理。我还是在线段树上想,就加了一个threshold。
double avgL... 阅读全帖
恭喜.
BTW:sliding window 的那个题目,前面不是有人贴链接了,楼上还有没看懂的? 用线性
结构,单纯的链表或者数组肯定不行,必须用到树形结构.堆可以.问题是这里有两个值,
一个是数组的位置,要保存好更新窗口,一个是数组元素的值,这个好取最大最小.所以就
是一个优先队列了,存的元素是数组元素下标,更新的时候将下标和当前窗口边界比较,
超出的就删除;而优先级用数组元素的值来表示.同时取最大和最小,所以一共需要俩优
先队列,伪码大概是这个样子:
for i from 1 to k 窗口的大小
add i with priority v[i] into PQ1, PQ2
max[i],min[i] 分别从 PQ1, PQ2 根的优先级得到
//上面这步是初始化,当然还要考虑窗口比数组还大的特例
// PQ1, PQ2 一个是按顺序, 一个是按逆序来的, 分别得到最大最小值
for i from k+1 to n 数组长度, 1-based index
add i with priority v[i] into PQ1, PQ2
while PQ1 根的值超出窗... 阅读全帖
对sorted set,logN找左右边界,然后把这个range的数取出来。
TreeSet的实现,你可以去读Java source code. 是通过TreeMap来实现的,API里写的:
“A Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.
This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations. Algorithms are adaptations of those in Cormen, Leiserson, and Rivest's Introduction to... 阅读全帖