I have a table a
ID | Name | City
1 |Jack | Null
2 |Tom | Null
And table b
ID | Name | City
1 |Jack | Dever
2 |Tom | Dallas
I need to write a query to join these two tables by id, name and city if
they are not null in table a. But any of these three column could be null
for each row.
I wrote one below but the performance is bad when data grows
Select * from a, b
Where (a.id is not null and a.id=b.id or a.id is null) and
(a.name is not null and a.name=b.name or a.name is null) and
(a.ci... 阅读全帖
adding a null to the end of each level can be helpful. following is printing
out each level. making link list is just same, creating a new list each
time a null is Dequeued. Tail null is taken care automatically.
public void PrintOutLevel(TreeNode node)
{
// null,check.....
Queue q = new Queue();
q.Enqueue(node);
q.Enqueue(null); // This is the tail of the first levle.
while (!(q.Count == 0))
... 阅读全帖
网上搜到的都没有考虑negative的情况,比如一个tree只有一个node,这个node的值
是negative。
下面是我自己的答案,已经pass OJ:
class LocalResult
{
int localMaxPathSum; // local max path sum
int localSubPathSum; // either root, or root+left, or root+right
public LocalResult(int maxPathSum, int subPathSum)
{
this.localMaxPathSum = maxPathSum;
this.localSubPathSum = subPathSum;
}
}
public class binaryTreeMaxPathSum {
/**
* @param args
*/
public static void main(String[] args) {
/... 阅读全帖
/**
* Using {@link AppleExercise.Problem1.SampleCache} as a starting point,
answer questions 1-3 by
* creating 3 new inner classes within {@link AppleExercise.Problem1}
below.
*/
public static class Problem1 {
public interface Cache {
public void put(Object key, Object value);
public Object get(Object key);
}
public static class SampleCache implements Cache {
private Map map;
public SampleCache() {
... 阅读全帖
Complete code:
import java.util.*;
import java.lang.Math.*;
// Customized Tree for sorting ending times and count how many ending times
are earlier than inserted one to compute the racer's score.
// Insertion is O(nlgn)
class node{
int val;
int below;
node left;
node right;
node(int val){this.val = val; left = right = null; below = 0;}
}
class Tree{
node root;
Tree(){root = null;}
int insert(int val){
node N = root;
if(N == null){
root ... 阅读全帖
贴代码,如有错误,请私信。
/**
* Implement insert and delete in a trinary tree.
*
*/
public class TrinaryTree {
/**
* define a trinary tree's node
*
*/
static class TreeNode{
public int val; // node's value
public TreeNode left, right, mid; // represent left, right, middle
node
public TreeNode(int v){
this.val = v;
left = null;
right = null;
mid = null;
}
}
TreeNode root;
/*
* Sort a linked list in O(n log n) time using constant space
complexity.
*/
public ListNode sortList(ListNode head) {
if(head == null || head.next == null)
return head;
//get the length of the liklist
int count = 0;
ListNode node = head;
while(node!=null){
count++;
node = node.next;
}
//break up to two list
int middle = count / 2;
... 阅读全帖
/**
* Definition for singly-linked list with a random pointer.
* class RandomListNode {
* int label;
* RandomListNode next, random;
* RandomListNode(int x) { this.label = x; }
* };
*/
public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
if(head == null)
return null;
if(head.next == null){
RandomListNode result = new RandomListNode(head.label);
result.next = null;
if(head.random != null)
re... 阅读全帖
这个不奇怪啊。
你可以看一看那个procedure, 你看里面有几个对temp table 的改变, 这些数据的改
变都会触发
recompilation。
我这个版本是2008 SP2的。
create procedure sys.sp_replmonitorrefreshagentdata
as
begin
set nocount on
declare @retcode int
,@agent_id int
,@agent_id2 int
,@publisher_id int
,@xact_seqno varbinary(16)
,@logreader_latency int
,@publisher_db sysname
,@publication sysname
hiberate的管理在lazy模式等下弄起来挺麻烦,自己管理倒是很方便,但可能存在连接
没有释放的隐患。。。。
import java.io.Serializable;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Component;
//@Component("hibernateSessionUtil")
public class HibernateSessionUtil implements Serializable {
//Create a thread local variable tLocalsess, for operating session
public static final ThreadLocal tLocalsess = new Thr... 阅读全帖
Give each Predictor object its own thread, and apply producer/consumer
pattern twice: one is from user thread to predictor thread for dropping off
prediction input, and the other one is form predictor thread to user thread
for picking up computed prediction.
public class Predictor implements Runnable {
private String input, prediction;
private boolean stop;
public Predictor () {
new Thread (this).start ();
}
public synchronized boolean isIdle () {
return input... 阅读全帖
你解释的和上面storm说的是一样的。
关于paul的链表,我没看仔细,下面是我自己写的。
没想到要写这么久。真的面的时候应该就废了。
很长,有耐心就慢慢看吧。。不知道对了对,意思应该在那了。
早知道应该写成C,可以跑试试看。
这么长,只是为了比begin-end那个少扫一遍。。。
defrecord NodeType
ch char
pos int
prePos *NodeType
nxtPos *NodeType
preSameChar *NodeType
nxtSameChar *NodeType
// S2 char to first tracked S2 NodeType (pointing to the start of
// a linked list of same char NodeType by nxtSameChar)
nodesFoundFirst hashmap
// S2 char to last tracked S2 NodeType (pointing to the end of
// a linked list of ... 阅读全帖
Update: fix some boundary condition checks
recursively call a function Node* cc(Node* root) to find out where to insert an extra node, where cc checks left subtree first and then right subtree to find a non-full subtree. It returns NULL if both subtrees are full.
Node* cc(Node* root)
{ // make sure that leaf node return NULL
if (root == NULL)
return NULL;
// first check if root has both children, if not return root
if (root->left != NULL && root->right == NULL)
return root;... 阅读全帖
各位大牛,请看看下面这个算法, 这个是不用heap的算法。
我觉得complexity 是 O(k*n), k是list 的个数, n 是list的平均长度。
那是不是这个应该比用heap的更好点啊?
public static ListNode mergeKLists(ArrayList kLists) {
if (kLists==null)
return null;
if (kLists.size()==0)
return null;
// if there is only one list in kLists
if (kLists.size()==1)
return kLists.get(0);
int length = kLists.size();
// initialize the curList with the first item in the list
ListNo... 阅读全帖