Data Structure
共 11 篇文章
Implement a Stack
Implement Stack in Java. public class MyStack<E> { private static class Node<E> { E value; Node<E> next; public Node(E v, Node<E> n){ value = v; next = n; } } private Node<E> head; private int size = 0; public void push(E e) { head = new Node<E>(e, head); size++; } public E pop() throws Exception {…
2015-07-30 11:56 · Technical Interviews · Data Structure
Implementing HashSet in Java
Implementing HashSet in Java. public class HashSet<K> { public static class Node<K> { final K key; Node next; public Node(K key, Node next) { this.key = key; this.next = next; } } private int size = 0; private int capacity = 16; private Node[] tab; public HashSet(int capacity) { this.capacity = cap…
2015-07-22 07:59 · Data Structure
HashMap
1,链表法。 public class HashMap<K,V> { public static class Node<K,V> { K key; V value; Node next; public Node(K key, V value, Node<K, V> next) { this.key = key; this.value = value; this.next = next; } } private Node<K,V> tab = new Node<K,V>[16]; private ReentrantLock[] locks = new ReentrantLock[16]; pu…
2015-07-01 05:37 · Data Structure
LeetCode 212 - Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once in a word. Fo…
2015-06-08 23:53 · LeetCode · DFS · Data Structure
LeetCode 211 - Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or . . A . means it can represent any one letter. For example: addWord("bad") addWord("dad") add…
2015-05-23 08:39 · LeetCode · Data Structure
Heap
/** * An implementation of a heap. Elements stored in the heap must implement the * Comparable interface. * */ public class Heap { private Comparable[] heap; private int size; private int capacity; private int capacityIncrement; /** * Construct a heap with initial capacity 10 and capacity increment…
2015-04-24 06:38 · Technical Interviews · Data Structure · Heap
Median of Integer Stream
Given a stream of unsorted integers, find the median element in sorted order at any given time. So, we will be receiving a continuous stream of numbers in some random order and we don’t know the stream length in advance. Write a function that finds the median of the already received numbers efficie…
2015-03-21 01:20 · Technical Interviews · Data Structure
介绍后缀树(Suffix Tree)
The following is an attempt to describe the Ukkonen algorithm by first showing what it does when the string is simple (i.e. does not contain any repeated characters), and then extending it to the full algorithm. First, a few preliminary statements. What we are building, is basically like a search t…
2015-03-18 12:55 · Data Structure
Trie
Trie. import java.util.*; public class Trie { private static final int R = 26; private static class TrieNode { boolean isLeaf; //是否为单词的结尾 int count; //以此节点为prefix的字符串的个数, 此属性不是必须的 TrieNode[] childs = new TrieNode[R]; } private TrieNode root = new TrieNode(); private int size; public int size() { re…
2015-03-18 02:44 · Data Structure
Data structure: insert, remove, contains, get random element, all at O(1)
From: http://stackoverflow.com/questions/5682218/data-structure-insert-remove-contains-get-random-element-all-at-o1 Design a data structure that offers the following operations in O(1) time: insert remove contains get random element Solution: Consider a data structure composed of a hashtable H and …
2015-03-15 00:07 · Data Structure · Google · Linkedin
Suffix Tree
A Suffix Tree is a data-structure that allows many problems on strings (sequences of characters) to be solved quickly. If txt=t1t2...ti...tn is a string, then Ti=titi+1...tn is the suffix of txt that starts at position i, e.g. The suffix tree for ` txt ' is a Trie -like or PATRICIA -like data struc…
2015-02-26 05:48 · Technical Interviews · Data Structure