由买买提看人间百态

topics

全部话题 - 话题: null
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
k*o
发帖数: 2
1
求牛人帮忙找bug。
题目是leetcode上的"Recover Binary Search Tree"。
思路是中序遍历,用递归做。
总是过不了OJ,在自己电脑上可以跑。
求大牛帮忙看看怎么回事。跪谢了!!!
我的代码如下:
public class Solution {
TreeNode node1 = null;
TreeNode node2 = null;
TreeNode last = null;
TreeNode current = null;
public void dfs(TreeNode root) {
if(root == null) {
return;
}
TreeNode left = root.left;
TreeNode right = root.right;
dfs(left);
last = current;
current = root;
if(last!=nul... 阅读全帖
S********0
发帖数: 29
2
来自主题: JobHunting版 - Leetcode新题 Copy List with Random Pointer
同求问题,本地是过的,runtime error, 上代码。
public class Solution {
public static RandomListNode copyRandomList(RandomListNode head) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
if(head == null) return null;
RandomListNode pointer, res_pointer;
pointer = head;
while(pointer != null){
RandomListNode temp = new RandomListNode(pointer.label);
temp.random = null;
temp.next = pointer.next;
... 阅读全帖
S********0
发帖数: 29
3
话不多说直接上代码,在本地测了没发现问题。。
谢谢.....
public static RandomListNode copyRandomList(RandomListNode head) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
if(head == null) return null;
RandomListNode pointer, res_pointer;
pointer = head;
while(pointer != null){
RandomListNode temp = new RandomListNode(pointer.label);
temp.random = null;
temp.next = pointer.next;
RandomListNode n... 阅读全帖
g*********e
发帖数: 14401
4
来自主题: JobHunting版 - leetcode Sort List
我的也很长,但不用每个recursion都寻找中间node的位置。
class Solution {
public:
ListNode *merge(ListNode *a, ListNode *b, ListNode *&last) {
ListNode *res=NULL;
ListNode *prev=NULL;
while(a && b) {
if(res==NULL)
res=(a->val < b->val) ? a:b;
if(a->val < b->val) {
if(prev)
prev->next=a;
prev=a;
a=a->next;
} else {
if(prev)
prev->next=b;... 阅读全帖
l*****a
发帖数: 14598
5
冗余的代码看着舒服吗?
函数压栈多一个参数不多费时间吗?
public TreeNode sortedListToBST(ListNode head) {
if(head==null) return null;
ListNode fast=head;
ListNode slow=head;
ListNode pre=null;
while(fast!=null && fast.next!=null) {
fast=fast.next.next;
pre=slow;
slow=slow.next;
}
TreeNode root=new TreeNode(slow.val);
if(pre==null) {
root.left=null;
} else {
pre.next=null;
root.left... 阅读全帖
k****r
发帖数: 807
6
这道题当时c++时简单的就实现了,但是类似的,我用java就不对,具体代码是这样的
public class Solution {
public int sumNumbers(TreeNode root) {
int result = 0;
int tmp = 0;
sumHelp(root, tmp, result);
return result;
}
private void sumHelp(TreeNode root, int tmp, int sum){
if (root == null) return;
if (root.left == null && root.right == null){
int newone = tmp + root.val;
sum = sum + newone;
}
if (root.right != null) sumHelp(root.right,... 阅读全帖
j**7
发帖数: 143
7
来自主题: JobHunting版 - fb家面试题讨论
O(N) time, O(1) extra space
public TreeNode[] convertBST(TreeNode root) {
TreeNode head = null;
TreeNode tail = null;
TreeNode current = root;
while (current != null) {
if (current.left != null && current.left.value < current.value) {
TreeNode left = current.left;
while (left.right != null && left.right != current) {
left = left.right;
}
if (left.right == null) {
... 阅读全帖
j**7
发帖数: 143
8
来自主题: JobHunting版 - fb家面试题讨论
O(N) time, O(1) extra space
public TreeNode[] convertBST(TreeNode root) {
TreeNode head = null;
TreeNode tail = null;
TreeNode current = root;
while (current != null) {
if (current.left != null && current.left.value < current.value) {
TreeNode left = current.left;
while (left.right != null && left.right != current) {
left = left.right;
}
if (left.right == null) {
... 阅读全帖
o*******y
发帖数: 362
9
Stack stack = new Stack();
List list = new ArrayList();
TreeNode cur = root, pre = null;
stack.push(cur);
while(!stack.isEmpty()){
cur = stack.peek();
if((cur.left == null && cur.right == null) || (pre != null && (
cur.left == pre || cur.right == pre))){
cur = stack.pop();
if(cur.left == null && cur.right == null){
list.add(cur);
... 阅读全帖
o******y
发帖数: 45
w*r
发帖数: 2421
11
来自主题: Tennessee版 - 包子请教query (转载)
【 以下文字转载自 Database 讨论区 】
发信人: MMinhouston2 (MMinhouston2), 信区: Database
标 题: 包子请教query
发信站: BBS 未名空间站 (Wed Sep 3 11:38:21 2008)
请教一下这个query应该怎样写哈
有call history table如下,一个ID可能被call多次:
ID callresult calldate
1 null null
1 R sep 2
2 S sep 1
3 null null
3 null null
……
我想找出callresult从来都是null的ID,曾经有过null但是还是有别的result的不包括
在内。
这个应该怎样写?
谢谢啦!
g**u
发帖数: 504
12
我把完整代码贴出来吧,这个是BSTree.h文件如下:
#include
#include
using namespace std;
template
class BinarySearchTree
{
public:
struct tree_node
{
tree_node* left;
tree_node* right;
T data;
};
public:
BinarySearchTree(){ root = NULL; }
bool isEmpty() const { return root==NULL; }
void insert(T);
void print_search(T);
private:
tree_node* search(tree_node*,T);
private:
tree_node* root;
};
template
void BinarySearchTree<... 阅读全帖
m**********2
发帖数: 2252
13
来自主题: Database版 - 包子请教query
请教一下这个query应该怎样写哈
有call history table如下,一个ID可能被call多次:
ID callresult calldate
1 null null
1 R sep 2
2 S sep 1
3 null null
3 null null
……
我想找出callresult从来都是null的ID,曾经有过null但是还是有别的result的不包括
在内。
这个应该怎样写?
谢谢啦!
l*******y
发帖数: 11
14
我的数据情况如下:
每个ID有多组重复测量的值,但是有的没有测量值
ID field1 field2 filed3
1
1 z
1 x y Null
2 a
2 Null b c
.
.
.
n
n e f
n d Null Null
想通过query得到每个field最后一个非空的数值,即:
ID field1 field2 filed3
1 x y z
2 a b c
...
n d e f
用total,last 得出的只能得到最后一组值:
ID field1 field2 filed3
1 x y Null
2 Null b c
n d Null Null
谢谢大家啦!
u*********e
发帖数: 9616
15
that was what I did. I created a table variable and just inserted values.
Later I replaced the table variable with temp table but no luck.
Below is my store procedure.
ALTER PROCEDURE [dbo].[SP_REPORT_GET_STATUS_DATES]
@Statement_Year SMALLINT,
@Statement_Month TINYINT

AS
BEGIN
BEGIN TRY
Declare @Curr_Code nvarchar(10)
Declare @Group_Code nvarchar(10)
declare @Group_member_Code nvarchar(10)
declare @Date_Submitted smalldatetime
declare @SuppDate... 阅读全帖
g***l
发帖数: 18555
16
你把这个TABLE VARIABLE换成TEMP TABLE就可以了。date都用DATETIME,没事不要弄变
量,CURSOR全去掉。不要用SELECT *,有什么COLUMN就写什么COLUMN
declare @Results table (Group_Code nvarchar(10) not null,
Group_Member_Code nvarchar(10) not null,
Stmt_Date_Created nvarchar(10) null,
Stmt_Date_Updated nvarchar(10) null,
Stmt_Date_Approved nvarchar(10) null,
Supp_Date_Entered nvarchar(200) null,
... 阅读全帖
u*********e
发帖数: 9616
17
gejkl,
Thank you very much for helping me answering my question. I was doing
research myself and figuring out the issue. You are right. I rewrite the
query to get rid of cursor and use PATH XML instead.
One interesting thing I observed is that after I updated my sp and run the
report, it gave out an error msg:
"An error occurred during local report processing.Exception has been thrown
by the target of an invocation. String was not recognized as a valid
DateTime.Couldn't store <> in Stmt_Date_Cre... 阅读全帖
B*****g
发帖数: 34098
18
来自主题: Database版 - user_objects 与CAT view
oracle 9i, user_objects should cover cat
CREATE OR REPLACE FORCE VIEW sys.user_objects
(
object_name,
subobject_name,
object_id,
data_object_id,
object_type,
created,
last_ddl_time,
timestamp,
status,
temporary,
generated,
secondary
) AS
SELECT o.name,
o.subname,
o.obj#,
o.dataobj#,
DECODE (o.type#,
0, 'NEXT OBJECT',
1, 'INDEX',
2, 'TABLE',
3, 'CLUST... 阅读全帖
s****y
发帖数: 581
19
来自主题: Database版 - 怎么去除duplicates
if your DB is Oracle, suppose your table name is table_name:
delete from table_name tn_1
where exists(select *
from table_name tn_2
where (
(tn_2.first_name = tn_1.first_name)
or
(tn_2.first_name is null and tn_1.first_name is null)
)
and
(
(tn_2.last_name = tn_1.last_name)
or
(tn_2.l... 阅读全帖
i*****9
发帖数: 293
20
来自主题: Database版 - 一个oracle query, 求问
有下面这四个表和下面这几个要求,怎么写比较好能得到我想要的结果? 多谢
1,先选出USERACC_FOR_DOM_REP的所有的值:username, account_status, target_db.
2,再从上面的结果中除掉所有EXCEPTION_ACC的username(条件是EXCEPTION_ACC里的
username和target_db是USERACC_FOR_DOM_REP的)。
3, 再从上面的结果中除掉所有MAX_USER_LOGIN的username(条件是MAX_USER_LOGIN里的
username和target_db是USERACC_FOR_DOM_REP的,外加LAST_LOGIN < sysdate -90)。
4, 再从上面的结果中除掉所有MYTABLE的username(条件是MYTABLE里的username和
target_db是USERACC_FOR_DOM_REP的
SQL> desc USERACC_FOR_DOM_REP
Name Null? Type
... 阅读全帖
b*********a
发帖数: 723
21
来自主题: Programming版 - 一道多线程的面试题 (转载)
上代码,三个类。可以直接运行
package com.bruce.concurrent;
public class MainApplication {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "I am from China and I was born in Jiangxi province";
Paragraph para = new Paragraph(str);
Thread pt = null;
int threadCount=5;
for (int i = 0; i < threadCount; i++) {
pt = new Thread(new PrinterThread(i, para, threadCount));
pt.start();
}
}
}
package com.bruce.concurrent;
public class PrinterThread implements Runnable {
private ... 阅读全帖
m******r
发帖数: 1033
22
来自主题: Programming版 - 两个简单R代码(坑?)求解[factor]
那你帮忙看看一下两段?(都是从?factor 来的, 我稍作改动)
x <- factor(c(1, 2, NA) , exclude = NULL)
is.na(x)
结果FALSE FALSE FALSE
x <- factor(c(1, 2, NA) , exclude = NA)
is.na(x)
结果FALSE FALSE TRUE
我知道NA 和 NULL的区别。
NA 是missing value indicator, length =1
NULL 是NULL object
我都搞不清楚exclude = NULL是啥意思。
exclude = NULL 可以是说, 所有level允许。
exclude = NULL 也可以说, 要把NULL object 排出, 因为它在exclusion list上面。
(对这个题目, NA应该允许存在)
无论怎样解释, 第一段代码为啥得出FALSE FALSE FALSE的结果??
c*********s
发帖数: 63
23
终于弄好了。把下面的存成.bst文件,和Tex文件放在同一个文件夹就可以了。
%%
%% This is file `cellnew.bst',
%% generated with the docstrip utility, by HJ, 03/03/2013.
%%
%% The original source files were:
%%
%% merlin.mbs (with options: `ay,nat,nm-rvvc,nmlm,x10,x0,m10,m0,mcite,mct-1
,mct-x2,keyxyr,blkyear,dt-beg,yr-par,note-yr,atit-u,jttl-rm,thtit-a,vol-it,
vnum-x,volp-com,pp-last,num-xser,jnm-x,btit-rm,bt-rm,add-pub,pub-par,pre-pub
,edby,edbyy,blk-tit,ppx,ed,abr,ednx,xedn,jabr,url,url-blk,em-it,nfss,')
%% ----------------------... 阅读全帖
c*********s
发帖数: 63
24
终于弄好了。把下面的存成.bst文件,和Tex文件放在同一个文件夹就可以了。
%%
%% This is file `cellnew.bst',
%% generated with the docstrip utility, by HJ, 03/03/2013.
%%
%% The original source files were:
%%
%% merlin.mbs (with options: `ay,nat,nm-rvvc,nmlm,x10,x0,m10,m0,mcite,mct-1
,mct-x2,keyxyr,blkyear,dt-beg,yr-par,note-yr,atit-u,jttl-rm,thtit-a,vol-it,
vnum-x,volp-com,pp-last,num-xser,jnm-x,btit-rm,bt-rm,add-pub,pub-par,pre-pub
,edby,edbyy,blk-tit,ppx,ed,abr,ednx,xedn,jabr,url,url-blk,em-it,nfss,')
%% ----------------------... 阅读全帖
w*******y
发帖数: 60932
25
Kobo 6" eBook Reader with 100 Preloaded Classic Books @ just $59.98
LIMITED PERIOD OFFER
Link to buy:
http://www.borders.com/online/store/TitleDetail?type=0&catalogI 4,parse: 9]&searchData={productId:null,sku:null,type:0,sort:null,currPage:1,resultsPerPage:25,simpleSearch:true,navigation:0,moreValue:null,coverView:false,url:rpp=25&view=2&all_search=3175385&type=0&nav=0&simple=true,terms:{all_search=3175385}}&storeId=13551&sku=817866000101&ddkey=http:SearchResults
#91;search%3A+4%2Cparse%3A+9]&se... 阅读全帖
i**9
发帖数: 351
26
来自主题: JobHunting版 - 新鲜出炉的amazon面经-phone&onsite
写了一个 检查bst对称的
bool symmetrical(Node * root){
if(root==NULL){
return true;
}
return symmetrical(root->left,root->right);
}
bool symmetrical(Node * l,Node * r){
if(l==NULL && r==NULL){
return true;
}
if(l==NULL && r!=NULL){
return false;
}
if(l!=NULL && r==NULL){
return false;
}
if(l->data!=r->data){
return false;
}
return symmetrical(l->left,r->right)&&symmetrical(l->right,r->left);
}
S*******0
发帖数: 198
27
来自主题: JobHunting版 - 贡献邮件面试题(Web Development)
1. Deadlock describes a situation that two or more threads (processes) are
blocked forever, waiting for each other.
Causes: one thread needs to visit the resource that another thread is
possessing and vice versa.
Effects: If deadlock happens, the threads involved will hang there forever
in an undesired status. Deadlock should be avoided.
2.
public ArrayList getToyotas(ArrayList cars)
{
if(cars == null) return null;

ArrayList toyotas = new ArrayList();
for ... 阅读全帖
v***a
发帖数: 365
28
来自主题: JobHunting版 - 最近没有什么新题

发现之前算法还不够优化,有overlap直接删点就是了
struct node {
int x, y;
node * left, * right, * fa;
};
node * insert(node * n, int x, int y) {
while (n && overlap(n, x, y)) {
if (n->x < x) x = n->x; if (n->y > y) y = n->y;
n = removeNode(n); // The hard part
}
if (n == NULL) {
n = new node;
n->x = x; n->y = y; n->left = NULL; n->right = NULL; n->fa = NULL;
if (root == NULL) root = n;
return n;
}
node * ret;
if (n->x < x) {
r... 阅读全帖
S**I
发帖数: 15689
29
来自主题: JobHunting版 - [合集] 问个C的基本问题
☆─────────────────────────────────────☆
honeydream (pretty) 于 (Fri May 6 00:30:21 2011, 美东) 提到:
下面这个程序为啥会segmentation fault呢?
int main(void)
{
char *p1, *p2;
*p1 = 'a';
*p2 = 'b';
printf("%c %c\n", *p1, *p2);
return 0;
}
如果我不用指针,把p1,p2的星号都去掉,就可以输出正确结果。
☆─────────────────────────────────────☆
chenpp (chenpp) 于 (Fri May 6 00:31:43 2011, 美东) 提到:
p1和p2浮空,指向未定义的地址空间。
对未定义的地址空间进行读写操作会导致未定义的行为,包括段错。

☆─────────────────────────────────────☆
mercuriusl (Mercurius) 于 (Fri May 6 ... 阅读全帖
h****n
发帖数: 1093
30
来自主题: JobHunting版 - Time complexity
斗胆写了一个,请指教
复杂度:
T(n)= 2T(n/2)+ 1 复杂度为O(n)
TreeNode FindLowCommonNode(TreeNode root, TreeNode p, TreeNode q)
{
if(root==null || p == null || q == null)
return null;
if(root==p || root==q)
{
return root;
}
TreeNoe left = FindLowCommonNode(root.Left, p, q);
TreeNoe right = FindLowCommonNode(root.Right, p, q);
if(left!=null && right!=null)
{
return root;
}
return left==null? right:left;
}
S**I
发帖数: 15689
31
来自主题: JobHunting版 - [合集] 微软面试的一道题
☆─────────────────────────────────────☆
sugarbear (sugarbear) 于 (Thu Apr 7 00:42:48 2011, 美东) 提到:
找 二叉树 两个最大的相同子树
没答上来。
见了四个,被拒了。 第二个是manager,后来主动写信跟我联系,说把我推荐给industry recruiting team,不知道是不是有转机? 觉得industry recruiting应该更难吧? 求祝福!
☆─────────────────────────────────────☆
boohockey (Pursuit of Dreams!) 于 (Thu Apr 7 10:27:03 2011, 美东) 提到:
bless
这道题有没有正解

industry recruiting team,不知道是不是有转机? 觉得industry recruiting应该更
难吧? 求祝福!
☆─────────────────────────────────────☆
grass (美丽人生) 于 (Thu Apr... 阅读全帖
d********w
发帖数: 363
32
来自主题: JobHunting版 - M家 onsite 悲剧,同胞们弄死烙印吧
我来写一个java的
public ListNode swapPairs(ListNode head) {
// Start typing your Java solution below
// DO NOT write main() function
if (head == null || head.next == null)
return head;

ListNode newNode = head.next;
ListNode p;
ListNode cur = head;
while(head != null && head.next != null) {
p = head.next.next;

head.next.next = head;
if (p != null)
head.next = p.next;
... 阅读全帖
d********w
发帖数: 363
33
来自主题: JobHunting版 - M家 onsite 悲剧,同胞们弄死烙印吧
我来写一个java的
public ListNode swapPairs(ListNode head) {
// Start typing your Java solution below
// DO NOT write main() function
if (head == null || head.next == null)
return head;

ListNode newNode = head.next;
ListNode p;
ListNode cur = head;
while(head != null && head.next != null) {
p = head.next.next;

head.next.next = head;
if (p != null)
head.next = p.next;
... 阅读全帖
l*********8
发帖数: 4642
34
来自主题: JobHunting版 - leetcode上wild match
贴一下我的程序,通过了leetcode judge.
感觉还是有些繁琐.
class Solution {
public:
bool isMatch(const char *s, const char *p) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(!s || !p)
return false;
if(p[0] == '\0' && s[0] != '\0')
return false;
const char * star = NULL;
const char * nextStar = NULL;
if (p[0] == '*') {
star = p;
p++;
}
while (1) {
n... 阅读全帖
Z*****Z
发帖数: 723
35
来自主题: JobHunting版 - FB面试题:binary tree inorder successor
响应大侠号召。写了个直白板的,求拍。
static >Node next(Node tree, Node node) {
if(tree == null){
return null;
}
if(node.right != null){
Node tmp = node.right;
while(tmp.left != null){
tmp = tmp.left;
}
return tmp;
}
Stack> stk = new Stack>();
if(!searchNode(tree, node, stk)){
return null;
... 阅读全帖
w****x
发帖数: 2483
36
来自主题: JobHunting版 - 问个reverse linked list

以后面试因该严格静止用脚本语言面试,太坯了
NODE* Reverse(NODE* pHead, int nStart, int n)
{
if (NULL == pHead || nStart < 1 || n < 1)
return pHead;
NODE* pX = NULL;
NODE* pIter = pHead;
for (int i = 1; i < nStart && pIter != NULL; i++)
{
pX = pIter;
pIter = pIter->pNext;
}
if (pIter == NULL) return pHead;
NODE* pEnd = pIter;
NODE* pPrev = NULL;
for (int i = 0; i < n && pIter != NULL; i++)
{
NODE* pTmp = pIter->pNext;
pIter->pNext = p... 阅读全帖
z*****5
发帖数: 1871
37
新手,为改变命运每天挑灯夜练,望指教,谢谢!
public static Node add(Node n1, Node n2) {
if (n1==null || n2==null) return null;

int carry = 0;
Node result = new Node(0);
Node p = result;

while (n1!=null || n2!=null) {
p.data = (n1==null ? n2.data : n2==null ? n1.data : (n1.data+n2.
data)%10) + carry; //此处报NullPointerException异常;
carry = p.data<10 ? 0 : 1;
n1 = n1.next;
n2 = n2.next;
p = p.next;... 阅读全帖
g*******n
发帖数: 214
38
来自主题: JobHunting版 - G电面一题
如果不用recursion的话代码太长是不是面试的时候不能用?
public ArrayList numString2Char(String num) {
//map to store all the previous possibilities
HashMap> map = new HashMap ArrayList>();
int[] numbers = new int[num.length()];
for (int i = 0; i < num.length(); i++) {
numbers[i] = Integer.parseInt(num.charAt(i) + "");
}
if(numbers[0]==0) return null;
ArrayList tempList;
... 阅读全帖
h****n
发帖数: 1093
39
不需要vector吧,假设binary tree是complete binary tree
已通过online judge测试
/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution {
public:
void connect(TreeLinkNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(root==NULL) return;
TreeLinkNode * cur = root;
TreeLinkNode ... 阅读全帖
h****n
发帖数: 1093
40
不需要vector吧,假设binary tree是complete binary tree
已通过online judge测试
/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution {
public:
void connect(TreeLinkNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(root==NULL) return;
TreeLinkNode * cur = root;
TreeLinkNode ... 阅读全帖
d******i
发帖数: 76
41
来自主题: JobHunting版 - Uni_value subtree problem
谢谢@wwwyhx大牛贡献的题。看了帖子里面的回复后,有了些思路,虽然对于牛人们,
这道题很简单,对于还是菜鸟的我还是很难的,发个帖子,供大家讨论,希望最
终得出正确的答案吧。
“我面的时候有一简单题一定要O(n), 找树的unival个数(就是这个节点开始所有子树都
一样的节点数) ”
题的意思是,找到这样的子树,满足子树中所有节点的值相等。计算满足此条件的子树
中节点的总个数。(希望没有理解错。)
例子1:
1
结果为 1
例子2:
1
1 1
结果为 3
例子3:
1
2 3
结果为2(2, 3)
例子4:
1
2 3
2
结果为:3 ({2,2}, {3})
例子 5:
1
2 3
2
4
结果为: 2 (4,3);
下面是C++代码,大家指正
---------------更新:不需要看我写的了,直接看楼下的解法吧,哈哈。-----------
----
#include
#include
using namespace std;
struct TreeN... 阅读全帖
e****e
发帖数: 418
42
我来个Recursion code,欢迎指正:
public void print(TreeNode n1, TreeNode n2) {
if ( n1 == null && n2 == null )
return;
else if ( n1 == null && n2 != null )
print(n2);
else if ( n1 != null && n2 == null )
print(n1);
else {
print(n1.left, n2.left)
if( n1.val > n2.val) {
println(n2.val);
println(n1.val);
} else{
println(n1.val);
println(n2.val);
}
print(n1.right, n2.right)
}
}
private void print(TreeNode n) {
if ( n == null )
return... 阅读全帖
c*****a
发帖数: 808
43
来自主题: JobHunting版 - 看不懂这题
写出来了
public static ListNode copyLink(ListNode node){
ListNode original = node, head = null, newOne=null,next;
//insert node inbetween, so abc becomes aabbcc
while(original!=null){
newOne = new ListNode(original.val);
next = original.next;
original.next = newOne;
newOne.next =next;
original = next;
}
//new list
head = node.next;
//copying random link
original = node;
while(original!=null){
original.next.datamember... 阅读全帖
e***s
发帖数: 799
44
来自主题: JobHunting版 - 今天面的太惨了....
刚写的,思想是,node的总数等于left subtree + right subtree + 1;
如果left subtree perfect 直接算 2^h -1; 如果不,递归进去
同理right subtree
public static int nodesInCompleteBT(TreeNode root){
if(root == null)
return 0;
if(root.left == null) // one node tree
return 1;
int height = 1;
TreeNode node = root.left;
while(node != null)
{
node = node.left;
height++;
}
return nodesInCompleteBTHelper(root, height);
}
... 阅读全帖
i********m
发帖数: 332
45
来自主题: JobHunting版 - 弱问题,连反转链表都看不懂了
练习了
public ListNode reverseBetween(ListNode head, int m, int n) {
// Start typing your Java solution below
// DO NOT write main() function

int diff = n-m;
if (diff < 0 || m <= 0)
return null;
boolean flag = false;
ListNode cur = head;
ListNode prev = head;

while (m-- > 1) {
if (cur == head) {
cur = cur.next;
}
else {
cur = cur.next;
... 阅读全帖
W********e
发帖数: 45
46
看不太出这个错在哪了,当input 是{},{0} 时候run time error了。
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode *h1=l1,*h2=l2;
ListNode *newHead,*dummy=newHead;
if(l1==NULL&&l2==NULL)
return NULL;
while(h1!=NULL&&h2!=NULL)
{
if(h1->val>=h2->val)
{
dummy->next=h2;
dummy=h2;
h2=h2->next;
}
else
... 阅读全帖
x*****0
发帖数: 452
47
class Solution {
private:
TreeNode *pre;
public:
Solution() : pre(NULL) {}
void flatten(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
//static TreeNode* pre = NULL;
if (root != NULL) {
flatten(root->right);
flatten(root->left);
root->right = pre;
root->left = NULL;
pre = root;
}
}
};
各位帮我看看这段代码有什么问题?我自己测试leetcode给出的数据,没有问题。但是
就是过不了。
如果改... 阅读全帖
a**********2
发帖数: 340
48
修修补补好几次才过了测试,写的又臭又长,谁能帮我改一下啊?
class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if( m > n || !head) return NULL;

ListNode* tail1 = (m>1?head:NULL);
ListNode* head2 = NULL;
ListNode* tail2 = NULL;
int i;
for( i = 1; i < m-1; i++)
{
if(!tail1)return NULL;
tail1=tail1->next;
}
ListN... 阅读全帖
J*****a
发帖数: 4262
49
这是linkedin第一轮电面的一位国人大哥给我的“warm-up”题,首先是序列化
我写成了tree类的成员函数,所以没有把root当成方法的参数
public void serialize(){
File file = new File("test.txt");
if(root == null) return;
FileWriter fw = null;
try{
fw = new FileWriter(file);
BufferedWriter bf = new BufferedWriter(fw);
Queue q = new LinkedList();
q.add(root);
while(!q.isEmpty()){
Node cur = q.poll();
if(cur == null) bf.write("# ");
else{
bf.write(cur.data + " ");
... 阅读全帖
A******g
发帖数: 612
50
来自主题: JobHunting版 - Leetcode新题 Copy List with Random Pointer
过不了,请大牛看看, 我在本地测试过好像没问题啊
----------------------------------------------
public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {

if (head==null) return null;
RandomListNode p = head;
while (p != null) {
RandomListNode newNode = new RandomListNode(p.label);
newNode.next = p.next;
p.next = newNode;
p = newNode.next;
}

p = head;
RandomListNode p2 = head.next;
... 阅读全帖
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)