g***9 发帖数: 159 | 1 第二题公共前缀并不是leetcode原题吧...
请教大牛 rolling hash 的解法 .. ?
自己写了一个Trie的python版本:
import sys
CHAR_COUNT = 26
class Entry(object):
def __init__(self, count=0, next=None):
self.cnt = count
self.nxt = next
#def
#class
class TrieNode(object):
def __init__(self):
self.entrylist = []
for i in xrange(CHAR_COUNT):
self.entrylist.append(Entry())
#for
#def
#class
root = TrieNode()
def InsertTrieNodes(root, curstr):
n = len(curstr)
prefix = []
curnode = root
valid = Tru... 阅读全帖 |
|
a*********8 发帖数: 140 | 2 用recursion和post order的stack,做第一题:
class TreeNode2 {
char val;
TreeNode2 left;
TreeNode2 right;
TreeNode2(char val){
this.val = val;
}
}
public class Exercise2 {
public List pathRootToLeaf(TreeNode2 root){
List list = new ArrayList();
if (root == null){
return list;
}
List left = pathRootToLeaf(root.left);
List right = pathRootToLeaf(root.right);
i... 阅读全帖 |
|
l******s 发帖数: 3045 | 3 两个Queue也可以吧,Queue 和 Queue,保持两个Queue同步就行了
。
It's accepted by leetcode.
private IList allPath(TreeNode root){
Queue queue = new Queue();
Queue qStr = new Queue();
IList result = new List();
if(root == null) return result;
queue.Enqueue(root); qStr.Enqueue(root.val.ToString());
while(queue.Count != 0){
TreeNode cur = queue.Dequeue();
string curStr = qStr.Dequeue();
if(cur.le... 阅读全帖 |
|
z*****1 发帖数: 1 | 4 用的dfs 写起来也没有那么复杂
String getCurString(String pat, int patIndex)
{
char curChar = pat.charAt(patIndex);
boolean positive = pat.charAt(patIndex + 1) == '+';
StringBuilder sb = new StringBuilder();
if (positive)
{
sb.append(curChar);
sb.append(curChar);
}
else
{
sb.append(curChar);
sb.append(curChar);
sb.append(curChar);
sb.append(curChar);
}
return sb.toString();
}
public int dfs(String txt... 阅读全帖 |
|
d****o 发帖数: 1055 | 5 /*
curInt
curStr
flag=1
*/
Answer; time. O(n) space O(1)
bool findInput(string in, int target){
string s = intToString(target);
int i=0;
int flag=1;
int curInt=0;
while(i
if(in[i]!=' '&&flag==1){
curInt = curInt*10+(in[i]-'0');
} else if(in[i]!=' '&&flag==0){
curInt= in[i]-'0';
i++;
} else if(in[i]==' '&&flag==1){
if(curInt==target) return true;
... 阅读全帖 |
|