由买买提看人间百态

topics

全部话题 - 话题: null
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
x******l
发帖数: 39
1
来自主题: Statistics版 - Oracle query 求助
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... 阅读全帖
w*********g
发帖数: 30882
2
万恶之源!高房价已经沦为中国阶级固化的根基!
中国城市中心<更多内容2017-02-28 15:20:43
今天就谈一个现实问题,那就是阶层固化!
根据瑞信研究院的财富报告, 超过约515万人民币净资产的人,已经属于全球最富裕的
人,据说这一类人只占全球的1%。我曾经读过一篇文章名字叫《我只过百分之1的生活
》,那么现在我想问,谁有500万?
毫无疑问,那些在北上广深任何一个城市有套房子的人,都已经站在了全球财富金字塔
的顶端。
null
这里,我们来个故事。
一个故事
我的同事老朱给我讲过一件真实的事。
十几年前,老朱在深圳富士康打工,跟他一块在流水线上工作的有一个男孩,这个男孩
叫小赵。
那个时候,大家下班后都是窝在宿舍里睡觉,只有小赵下班了还去夜市摆地摊,即便休
息也去打零工,他花了大概有两年多的时间,攒够了首付的钱,在深圳买了套房。
10年过去了,现在小赵身家起码有3000万了。
我悠悠地对老朱说,如果当初你也在深圳买套房就好了,那你就不用和傻逼的我做同事
了,老朱叹息了一声笑了。
转了个笑脸,我在心里想,房子真的能给人带来翻天覆地的变化,对,是翻天覆地……
讲到这里,我和老朱... 阅读全帖
p*****2
发帖数: 21240
3
来自主题: JobHunting版 - 请教一道单链表问题
import java.io.*;
import java.util.*;
public class test3
{
public static void main(String[] args) throws Exception
{
new test3().run();
}
PrintWriter out = null;
void print(Node head)
{
while (head != null)
{
out.print(head.value + " ");
head = head.next;
}
out.println();
}
Node remove(Node head)
{
Node prev = head;
head = null;
Node pp = null;
while (prev != null)
... 阅读全帖
w****x
发帖数: 2483
4
来自主题: JobHunting版 - MS on-site 面经&求分析(口头offer)
不用队列实现next连接
void LinkRightFromParent(NODE* pNode, NODE* pParent)
{
if (pNode == NULL) return;
if (pParent != NULL && pParent->pLft == pNode && pParent->pRgt
!= NULL)
pNode->pSibling = pParent->pRgt;
else
{
NODE* pIter = pParent == NULL ? NULL : pParent-
>pSibling;
NODE* pRgtCon = NULL;
while (pIter != NULL && pRgtCon == NULL)
{
if (pIter->pLft != NULL)
pRgtCon = pIter->pLft;
else if (pIter->pRgt != NULL)
pRgtCon = pIter->pRgt;
pIter = pIter->pSibling;
}
pNode->pSibling = pRgtCon;
}
LinkRightFro... 阅读全帖
v***d
发帖数: 51
5
来自主题: JobHunting版 - 生成树
试着做了一下
Node* findAllBSTs(int beginIndex, int endIndex, Node* origHead, Node*
curParent) {
if ( endIndex - beginIndex < 0 )
return NULL;
else if ( endIndex - beginIndex == 0 ) {
Node* newNode = new Node(values[beginIndex], NULL, NULL);
return newNode;
}
else {
for ( int i = beginIndex; i <= endIndex; i++ ) {
curParent->value = values[i];

if ( beginIndex <= i-1 )
curParent->leftChild = new Node(0, N... 阅读全帖
w****x
发帖数: 2483
6
来自主题: JobHunting版 - 刚才重新回顾了一下那题
struct NODE
{
vector vecGUID;
NODE* nodes[256];

NODE() { memset(nodes, 0, sizeof(nodes)); }
};
void _inner_get_guid(NODE* pNode, const char* str, int k, vector& vec)
{
if (NULL == pNode)
return;
if (k <= 0)
{
vec = pNode->vecGUID;
return;
}
_inner_get_guid(pNode->nodes[*str], str+1, k-1, vec);
}
vector getGUID(const char* str, NODE* pRoot, int k)
{
vector vecRet;
if (NULL == pRoot || NULL == str || *str == 0 ... 阅读全帖
t********y
发帖数: 14
7
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))
... 阅读全帖
a****x
发帖数: 89
8
网上搜到的都没有考虑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) {
/... 阅读全帖
p*****2
发帖数: 21240
9
来自主题: JobHunting版 - A家面经求Offer

def secondLargest(root:TreeNode):TreeNode={
if(root==null || root.left==null && root.right==null) return null
var node=root
while(node.right!=null && node.right.right!=null) node=node.right
if(node.right==null || node.right.left!=null){
node=if(node.right==null) node.left else node.right.left
while(node.right!=null) node=node.right
}
node
}
这个行吗?还没好好过test case
r******r
发帖数: 700
10
来自主题: JobHunting版 - Apple Siri 组 Java 测试题
/**
* 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() {
... 阅读全帖
s******n
发帖数: 226
11
来自主题: JobHunting版 - 一道rf的面试题
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 ... 阅读全帖
j********x
发帖数: 2330
12
我觉得是有个bug,我还没想出来怎么构造反例,先不说具体的bug了
能过small和large case
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
stack lhs;
for (TreeNode* l_root = root; l_root != NULL; l_root = l_root->left)
{
lhs.push(l_root);
}

stack rhs;
for (Tre... 阅读全帖
d**e
发帖数: 6098
13
☆─────────────────────────────────────☆
heigen (Bacon Lettuce and Tomat Sandwich) 于 (Mon Sep 10 20:28:04 2012, 美东) 提到:
等了2个月零8天了,这日子数得我好心焦啊。
☆─────────────────────────────────────☆
AlbanyNY (Alb) 于 (Tue Sep 11 16:47:13 2012, 美东) 提到:
More than 4 months, still pending
☆─────────────────────────────────────☆
ADSP (^_^) 于 (Tue Sep 11 21:23:14 2012, 美东) 提到:
2 months now, pending..
Good luck to all of us.

☆─────────────────────────────────────☆
imserious (我是希尔瑞斯) 于 (Wed Sep 12 22:4... 阅读全帖
d*****1
发帖数: 263
14
问题虽然解决了。但是,还是没有彻底明白。
问题是:Sum Root to Leaf Numbers (见链接)
http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/
下面的3个方案,都是用了递归.
方案1: 利用了一个static 变量存储结果。
public class Solution{
public static int totalSum = 0;
public int sumNumbers(TreeNode root) {
if (root != null) {
getSum(root, 0);
}
return totalSum;
}
private void getSum(TreeNode node, int upperSum) {
if (node.left == null && node.right == null) {// find leaf
totalSum +... 阅读全帖
f**********3
发帖数: 295
15
来自主题: JobHunting版 - leetcode上的Sort List那道题
public class Solution {
public ListNode sortList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode tail = head.next;
while (tail.next != null) {tail = tail.next;}
HeadTail ret = sort(head, tail);
return ret.head;
}

class HeadTail {
ListNode head;
ListNode tail;
public HeadTail(ListNode head, ListNode tail) {
this.head = head;
this.tail = tail;
}
}

... 阅读全帖
N******t
发帖数: 43
16
贴代码,如有错误,请私信。
/**
* 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;

public TrinaryTre... 阅读全帖
c*******7
发帖数: 438
17
来自主题: JobHunting版 - 请教iterative merge sort list的代码
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode sortList(ListNode head) {

if(head == null) {
return null;
}
ListNode[] heads = new ListNode[100];
int[] counts = new int[100];
heads[0] = head;
counts[0] = 1;

ListNode next = head.next;
i... 阅读全帖
w****r
发帖数: 15252
18
来自主题: JobHunting版 - [ 每日一课] Sort List
/*
* 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;
... 阅读全帖
a**********0
发帖数: 422
19
来自主题: JobHunting版 - copy list with random pointer 老出错
/**
* 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... 阅读全帖
j**7
发帖数: 143
20
来自主题: JobHunting版 - 版上看到的几道F家的题目
5.多层链表压扁及还原
// 没有测试过。
public static Node flatten(Node head, Node tail) {
Node start = head;
while (head != tail.next) {
Node next = head.next;
if (head.down != null) {
Node leftMost = leftMost(head.down);
Node rightMost = rightMost(head.down);
head.down.up = null;
head.down = null;
head.next = leftMost;
leftMost.prev = head;
rightMost.next = next... 阅读全帖
b**********5
发帖数: 7881
21
来自主题: JobHunting版 - Facebook面经
求 LCA 两种情况,有parent结点跟没有parent的结点的情况都要回答。
TreeNode LCA (TreeNode a, TreeNode b) {
TreeNode aCur = a; TreeNode bCur = b;
HashSet s = new HashSet<>();
while (aCur != null || bCur != null) {
if (aCur != null) {
if (s.contains(aCur)) return aCur;
else s.add(aCur);
aCur = aCur.parent;
}
if (bCur != null) {
if (s.contains(bCur)) return bCur;
else s.add(bCur);
bCur = bCur.parent;
}
}
return nu... 阅读全帖
a***e
发帖数: 413
22
Symmetric Tree一开始又把题理解错了。后来弄了个recursion之后,写了如下这个冗
长的 iterative version. 后来看了别人写的发现用一个stack要简洁好多好多。估计
是same tree那道题偷了懒,没认真看iterative的做法。
请问怎么样才能想到比较简洁的答案呢?是不是还是得多练?
从2月中就断断续续的刷,151道做了77道,希望年底能搞完一遍。N年前的今天来到美
国,雄心壮志要当教授,开公司,现在备受打击后,只是很实际的希望找个喜欢的工作
,能够在不爽的时候快点离开!
bool isSymmetric(TreeNode *root) {
if (root==NULL||(root->left==NULL &&root->right==NULL)) return true;

if ((root->left==NULL && root->right!=NULL)||(root->left!=NULL&&root
->right==NULL))
{
re... 阅读全帖
f*y
发帖数: 876
23
来自主题: JobHunting版 - 请问一道关于binary tree的题
post-order travesal,return the path to the current max in the subtree.

public static void maxPath(TreeNode root) {
if (root == null) return;
ArrayList path = get(root);
System.out.println(path);
}
public static ArrayList get(TreeNode root) {
if (root == null) return null;
ArrayList left = get(root.left);
ArrayList right = get(root.right);
ArrayList list;
if (left == null && ri... 阅读全帖
w**w
发帖数: 5391
24
来自主题: Tennis版 - [合集] 巅峰站
☆─────────────────────────────────────☆
zhanglaosan (张老三) 于 (Mon Oct 29 05:32:53 2012, 美东) 提到:
竟然体力不支,回家就闷头睡了,结果睡到半夜睡不动了,上BBS一看,老肯已经发来
编辑好的录像了,可惜第二个set只拍了几分钟就给关机了,所以主要是第一个set
体力不支主要原因不是跟老肯的比赛,就这场比赛而言本身还好,主要原因是
1,天气超级炎热
2. 赛前刚打了一场league联赛。我本来想,也就一双打,队长临时找不到人拉人问我
,我一想,正好热身啊,不错啊
没想到,这种超炎热天气连着打两场比赛对体力要求还是很高的
当然,这个不是啥借口,老肯号称更不擅长热天比赛,且,出门旅行,肯定热得也够呛。
球,输了,输的挺惨,感觉老肯老在网上经常“自贬“,还是很迷惑人的,老肯打得很
好,非常稳定,网前volley一般,但应付我足够,我volley更烂。进网小球老肯处理得
很老道,相反我这种球全部丢分。
发球,我现在依然双误频频,这个是我近期的新气象,以前是著名的不双误男。赛前练
发球老肯就在对面说,... 阅读全帖
z***y
发帖数: 7151
25
这个不奇怪啊。
你可以看一看那个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

-- security check
-- Has to b... 阅读全帖
l********0
发帖数: 283
26
来自主题: Database版 - 问一个表结构设计的问题
比如有这样一个表
f1 f2 f3
A1 100 100 100
A2 100 100 100
...
A10000 100 100 100
B1 100 100 null
B2 100 100 null
...
B10000 100 100 null
C1 100 null null
C2 100 null null
...
C10000 100 null null
这个表有4列,后三列是数据,100代表可能的数据,null代表无数据
一部分记录A1-A10000拥有这三列数据
一部分记录B1-B10000拥有前两列数据
一部分记录C1-C10000拥有前一列数据
办公室在讨论这样的表是否可以分解为三个表,但是分解还有两种方法
分解方法1:
一个表只有A记录,有三列
一个表只有B记录,有两列
一个表只有C记录,有一列
分解方法2:
一个表只有一列,... 阅读全帖
y***m
发帖数: 7027
27
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... 阅读全帖
o**2
发帖数: 168
28
来自主题: Java版 - 工作中遇到的并行处理问题
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... 阅读全帖
p*****2
发帖数: 21240
29
来自主题: Programming版 - 请教一个编程问题
看看这个可不可以
class MapIterator{
Iterator it=null;
Iterator sub=null;
Object next=null;

public MapIterator(Map m){
it=m.values().iterator();
}

boolean processSub(){
while(sub!=null && sub.hasNext()){
next=sub.next();
if(next!=null) return true;
}
return false;
}

boolean hasNext(){
if(next!=n... 阅读全帖
g**1
发帖数: 10330
30
http://wemedia.ifeng.com/9430023/wemedia.shtml
杜少按
「三十而立」,这句古训让现代人很紧张。
假设22岁大学毕业,你我只有8年时间成家立业。
然而在今天的中国,恋爱、家庭、教育都要以一张房产证为前提。因此,凡是在一线城
市打拼的年轻人,大多先立业,后成家。
三十而立,把这句古训翻译成现代汉语:毕业后,你有8年时间打拼,买套房,然后才
能拥有一个幸福人生。
然而这场8年抗战,需要你用尽每一丝力气打拼。
你还剩多长时间?距离首付,还差几万?足够努力吗?
有没有想过:这样的努力,真的有用吗?
这是杜少的第18篇真实故事,你将看到一个怀揣梦想的青年,燃烧青春努力10年之后,
将得到什么结果。
null
每当看到斯嘉丽・约翰逊的华为P9手机广告,老周都会感到一阵恶心。
去年,这款手机是华为力推的产品,好莱坞一姐都给中国手机拍了广告。
身为华为的老员工,老周却感觉不到骄傲。斯嘉丽・约翰逊甜美的微笑,只能让
老周想起每天十几个小时的繁重工作,紧接着嘴里泛起一股怪味。
老周最怕在地铁里想起工作,光是地铁环境就让他想吐——
前面... 阅读全帖
c**s
发帖数: 43
31
来自主题: JobHunting版 - 这题谁知道答案?
你解释的和上面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 ... 阅读全帖
s****n
发帖数: 48
32
来自主题: JobHunting版 - A家,link all node in the same lev
我写了个练手的,可以处理non-full binary tree。几个要点:
1.把parent node传给recursive function
2.每次recursive call只负责连接当前root。因为parent node作为参数传进来了,我
可以access parent's next link。并且这个时候所有上层的next links都已经连接好
了。
3.左右子树分别递归处理。先处理右子树,再处理左子树。
public void AddNextLink(Tree tree)
{
if (tree.Root == null)
{
return;
}
tree.Root.Next = null;
this.AddNextLink(tree.RightTree, tree, false);
this.AddNextLink(tree.LeftTree, tree, t... 阅读全帖
c**********6
发帖数: 105
33
面完就发上来了
第一次面大公司啊 好鸡冻 T____T
1. project
2. 上题:
i> 如何用一个方法返回多个值
ii> 如何check一个二叉树的节点的children互为镜像
简单吧
求各种推荐啊 Google, Facebook, Microsoft, Yahoo, Linkedin, Twitter
我答的是
1. i> 新建一个class,封装多个变量;
ii> 利用java参数传递是传递引用,可以直接修改变量值(这点和c++类似),而且同时还可以返回一个值;
iii> 利用java类库中的数据结构,比如说ArrayList。
2. i> recursive算法比较简单
boolean check(TreeNode root) {
//case 1: root == null
if(root == null) return true;
//case 2: left == right == null;
if(root.left == null && root.right == null) r... 阅读全帖
n*******w
发帖数: 687
34
来自主题: JobHunting版 - google 电面面经
1. binary search tree
first() 从左子树一直往下,到左子树为null,返回node。
next() 没有parent指针,找中序遍历successor挺麻烦。写个递归都感觉繁琐。
思路是,先top-down,找到当前node,再bottom-up,找到successor。
下面用Java写的。假设没有duplicate keys。不然会更麻烦。
tmp = null;//tmp存储结果辅助用的。cur是当前node。
Node next(Node cur, Node root){
//find cur node, and check its right child is null or not
//if null, return null
if(root == cur){
if(root.right != null)
return first(root.right);
else
return null;
}
//tmp stores the result, if tmp is null, root... 阅读全帖
p*****2
发帖数: 21240
35
来自主题: JobHunting版 - [google面试]iterator访问
大概这个样子吧。
class flat implements Iterator
{
Iterator> it1=null;
Iterator it2=null;

void Getit2()
{
it2=null;
if(it1!=null)
{
while(it1.hasNext())
{
Vector v=it1.next();
if(v!=null && v.size()!=0)
{
it2=v.iterator();
return;
};
}
}
}
public flat(Vector> a)
{
if(a!=null)
{
it1=a.iterator();
Getit2();
}
... 阅读全帖
a***y
发帖数: 50
36
俺写了一个, 没有测试过只有1, 2, 或者0 的情况...
请高手指正...
思路就是维护了三个指针p0, p1, p2, 分别指向了数组index最小的0, 1, 2值所在位置,
然后遍历的过程中,不断swap(值和指针同时swap), 复杂度为O(n), 常数空间.
请高手们多指正了!
#include
#include
using namespace std;
class MITbbSolver
{
public:
MITbbSolver() {}
virtual ~MITbbSolver() {}
public:
void swap_pv(int* &p1, int* &p2) {
int tmp = *p1;
*p1 = *p2;
*p2 = tmp;
int* p = p1;
p1 = p2;
p2 = p;
return;
}
void sort_012_array(int* ... 阅读全帖
w****x
发帖数: 2483
37
来自主题: JobHunting版 - binary tree的in-order iterator怎么写?
这题作的不下5遍了, 说实话, 第一次做还挺不容易的:
============带parent===========================
void InOrderPrint(NODE* pRoot)
{
assert(pRoot);
NODE* pCur = pRoot;
while (pCur != NULL)
{
while (pCur->pLft != NULL)
{
cout<nVal< pCur = pCur->pLft;
}
cout<nVal< //The ending condition is tricky but simple
while (pCur != NULL)//use "pCur != NULL" rather than "pCur->pParent
!= NULL"
... 阅读全帖
I*******l
发帖数: 203
38
来自主题: JobHunting版 - 插入节点到complete binary tree的末尾
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;... 阅读全帖
p*****2
发帖数: 21240
39
我写了一个,有点繁琐,还没测试。
void connectSibling(Node root)
{
assert(root!=null);

Node[] layer = new Node[2];
layer[0]=root;
int i=0;

while(layer[i%2]!=null)
{
Node prev=null;
Node curr=layer[i%2];

while(curr!=null)
{
if(curr.left!=null)
{
if(prev==null)
{
prev=curr.left;
... 阅读全帖
p*****2
发帖数: 21240
40
写了一个DFS的
Node findNext(Node node)
{
if(node==null)
return null;

while(node!=null)
{
if(node.left!=null)
return node.left;
if(node.right!=null)
return node.right;

node=node.sibling;
}

return null;
}

void dfs(Node node, Node parent)
{
if(node==null)
return;

if(node==parent.left && parent.right... 阅读全帖
w***o
发帖数: 109
41
来一个java的。space是O(1),复杂度应该是O(n)。
两个指针,一个current level的cur,一个是下一层link的头-nexthead:
void linkSibling(Node root)
Node cur = root;
Node nexthead = null;
while(cur != null) {
nexthead = null;
while(cur != null) {
Node runner = null;
if(cur.left != null) {
if(nexthead == null) {
nexthead = cur.left;
runner = nexthead;
} else {
runner.sibling = cur.left;
runner = runner.sibling;
}
... 阅读全帖
p*****2
发帖数: 21240
42
我写了一个,有点繁琐,还没测试。
void connectSibling(Node root)
{
assert(root!=null);

Node curr=root;

while(curr!=null)
{
Node prev=null;
Node next=null;

while(curr!=null)
{
if(curr.left!=null)
{
if(prev==null)
{
prev=curr.left;
next=prev;
}
... 阅读全帖
p*****2
发帖数: 21240
43
写了一个DFS的
Node findNext(Node node)
{
if(node==null)
return null;

while(node!=null)
{
if(node.left!=null)
return node.left;
if(node.right!=null)
return node.right;

node=node.sibling;
}

return null;
}

void dfs(Node node, Node parent)
{
if(node==null)
return;

if(node==parent.left && parent.right... 阅读全帖
w***o
发帖数: 109
44
来一个java的。space是O(1),复杂度应该是O(n)。
两个指针,一个current level的cur,一个是下一层link的头-nexthead:
void linkSibling(Node root)
Node cur = root;
Node nexthead = null;
while(cur != null) {
nexthead = null;
while(cur != null) {
Node runner = null;
if(cur.left != null) {
if(nexthead == null) {
nexthead = cur.left;
runner = nexthead;
} else {
runner.sibling = cur.left;
runner = runner.sibling;
}
... 阅读全帖
h****n
发帖数: 1093
45
我画个图你就明白了
比如:
head---->node--->node----->node----->node----->NULL
| |
node--->node-->NULL node--->NULL
|
node->NULL
第一次append之后变成:
head---->node--->node----->node----->node--->node--->node---NULL
| |
node--->NULL node-->NULL
第二次append之后变成
head-->node-->node-->node-->node-->node-->node-->node--->NULL
... 阅读全帖
y***u
发帖数: 174
46
这个行么?recursive的。
我需要暂时把左子树或者右子树变成null,不然太麻烦。
void Traverse(ArrayList output, Node A, Node B){
if(A==null && B==null)
return;
else if(A==null){
output.add(B.val);
return;
}else if(B==null){
output.add(A.val);
return;
}else{
Node t1 = null, t2=null;
if(B.val>A.val){
Trav(output, A, B);
}else if(B.val < A.val){
Trav(output, B, A);
}else{
output.add(A.val);
ou... 阅读全帖
x******0
发帖数: 1025
47
来自主题: JobHunting版 - delete a node in linked list
这个和我上学期考试题目一样啊
//recursively remove ALL nodes with value
PINTLISTNODE RemoveAll1(PINTLISTNODE pHead, int iRemoveValue)
{
if (pHead == NULL)
{
return NULL;
}
if (pHead->iValue == iRemoveValue)
{
PINTLISTNODE pToDelete = pHead;
pHead = pHead->pNext;
delete pToDelete;
return RemoveAll1(pHead, iRemoveValue);
}
else
{
pHead->pNext = RemoveAll1(pHead->pNext, iRemoveValue);
return pHead;
}
}
//use a dummyHead
PI... 阅读全帖
c****7
发帖数: 13
48
来自主题: JobHunting版 - 请教各位大牛一个K-way merge 的问题
各位大牛,请看看下面这个算法, 这个是不用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... 阅读全帖
d**********x
发帖数: 4083
49
来自主题: JobHunting版 - LinkedIn 面经
H2O那个我写了个C的。可以改吧改吧加上对各种原子的计数。
编译的时候别忘了 -lpthread !
#include
#include
#include
#include
#include
#include
sem_t availH;
sem_t availO;
sem_t usedH;
sem_t usedO;
void* H(void*) {
sem_post(&availH);
sem_wait(&usedH);
fprintf(stderr, "H consumed.\n");
pthread_detach(pthread_self());
return NULL;
}
void* O(void*) {
sem_post(&availO);
sem_wait(&usedO);
fprintf(stderr, "O consumed.\n");
pthread_detach(... 阅读全帖
d**********x
发帖数: 4083
50
来自主题: JobHunting版 - LinkedIn 面经
H2O那个我写了个C的。可以改吧改吧加上对各种原子的计数。
编译的时候别忘了 -lpthread !
#include
#include
#include
#include
#include
#include
sem_t availH;
sem_t availO;
sem_t usedH;
sem_t usedO;
void* H(void*) {
sem_post(&availH);
sem_wait(&usedH);
fprintf(stderr, "H consumed.\n");
pthread_detach(pthread_self());
return NULL;
}
void* O(void*) {
sem_post(&availO);
sem_wait(&usedO);
fprintf(stderr, "O consumed.\n");
pthread_detach(... 阅读全帖
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)