Showing posts with label Data structure. Show all posts
Showing posts with label Data structure. Show all posts

Sunday, August 30, 2015

MJ [32] Iterate a nested array (Deep Iterator)

Question:
Write an iterator to iterate a nested array. For example, for given array:
                                          [1,2,[3,[4,5],[6,7],8],9,10] 
call iterator.next() 10 times should return 1,2,3,4,5,6,7,8,9,10.



Dark nodes are of TYPE_NESTED. Numbered nodes are of TYPE_ITEM.
Ref

Thursday, August 6, 2015

MJ [1]

Note

  • implement a data structure to get a value of a key at a specific time

Ref
[1] http://www.mitbbs.com/article_t/JobHunting/33021795.html
[2] https://gist.github.com/Winslett/6a674ae858ab92f13038

Wednesday, August 5, 2015

LeetCode [251] Flatten 2D Vector

Design and implement an iterator to flatten a 2d vector. It should support the following operations: next and hasNext.

Example:
Vector2D iterator = new Vector2D([[1,2],[3],[4]]);

iterator.next(); // return 1
iterator.next(); // return 2
iterator.next(); // return 3
iterator.hasNext(); // return true
iterator.hasNext(); // return true
iterator.next(); // return 4
iterator.hasNext(); // return false

Notes:
  1. Please remember to RESET your class variables declared in Vector2D, as static/class variables are persisted across multiple test cases. Please see herefor more details.
  2. You may assume that next() call will always be valid, that is, there will be at least a next element in the 2d vector when next() is called.

Follow up:
As an added challenge, try to code it using only iterators in C++ or iterators in Java.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Vector2D
{
    vector<vector<int>>::iterator row_it, row_end;
    vector<int>::iterator col_it;

  public:
    Vector2D(vector<vector<int>> &v)
    {
        row_it = v.begin(), row_end = v.end();
        while (row_it != row_end && row_it->empty())
            row_it++;
        if (row_it != row_end)
            col_it = row_it->begin();
    }

    int next()
    {
        int v = *col_it;
        col_it++;
        if (col_it == row_it->end())
        {
            row_it++;
            while (row_it != row_end && row_it->empty())
                row_it++;
            if (row_it != row_end)
                col_it = row_it->begin();
        }
        return v;
    }

    bool hasNext()
    {
        return row_it != row_end;
    }
};

/**
 * Your Vector2D object will be instantiated and called as such:
 * Vector2D* obj = new Vector2D(v);
 * int param_1 = obj->next();
 * bool param_2 = obj->hasNext();
 */

Tuesday, August 4, 2015

LeetCode [244] Shortest Word Distance II

Ref
[1] https://leetcode.com/problems/shortest-word-distance-ii/
OJ

Wednesday, May 13, 2015

LeetCode [208] Implement Trie (Prefix Tree)

 208. Implement Trie (Prefix Tree)

Medium

Trie (we pronounce "try") or prefix tree is a tree data structure used to retrieve a key in a strings dataset. There are various applications of this very efficient data structure, such as autocomplete and spellchecker.

Implement the Trie class:

  • Trie() initializes the trie object.
  • void insert(String word) inserts the string word to the trie.
  • boolean search(String word) returns true if the string word is in the trie (i.e., was inserted before), and false otherwise.
  • boolean startsWith(String prefix) returns true if there is a previously inserted string word that has the prefix prefix, and false otherwise.

 

Example 1:

Input
["Trie", "insert", "search", "search", "startsWith", "insert", "search"]
[[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
Output
[null, null, true, false, true, null, true]

Explanation
Trie trie = new Trie();
trie.insert("apple");
trie.search("apple");   // return True
trie.search("app");     // return False
trie.startsWith("app"); // return True
trie.insert("app");
trie.search("app");     // return True

 

Constraints:

  • 1 <= word.length, prefix.length <= 2000
  • word and prefix consist of lowercase English letters.
  • At most 3 * 104 calls will be made to insert, search, and startsWith.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Trie {
    class TrieNode{
        boolean isEnd;
        TrieNode[] children;
        TrieNode(){
            isEnd = false;
            children = new TrieNode[26];
        }
    }
    TrieNode root;

    /** Initialize your data structure here. */
    public Trie() {
        root = new TrieNode();
    }
    
    /** Inserts a word into the trie. */
    public void insert(String word) {
        TrieNode p = root;
        for(char c : word.toCharArray()){
            if(p.children[c-'a']==null){
                p.children[c-'a'] = new TrieNode();
            }
            p = p.children[c-'a'] ;
        }
        p.isEnd = true;
    }
    
    /** Returns if the word is in the trie. */
    public boolean search(String word) {
        TrieNode p = root;
        for(char c : word.toCharArray()){
            if(p.children[c-'a']==null){
                return false;
            }
            p = p.children[c-'a'] ;
        }
        return p.isEnd == true;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    public boolean startsWith(String prefix) {
        TrieNode p = root;
        for(char c : prefix.toCharArray()){
            if(p.children[c-'a']==null){
                return false;
            }
            p = p.children[c-'a'] ;
        }
        return true;
    }
}

/**
 * Your Trie object will be instantiated and called as such:
 * Trie obj = new Trie();
 * obj.insert(word);
 * boolean param_2 = obj.search(word);
 * boolean param_3 = obj.startsWith(prefix);
 */

Tuesday, February 3, 2015

LeetCode [170] Two Sum III - Data structure design

Ref
[1] https://leetcode.com/problems/two-sum-iii-data-structure-design/
OJ