由买买提看人间百态

topics

全部话题 - 话题: val
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
j*****y
发帖数: 1071
1
来自主题: JobHunting版 - google面试全过程(简装版)
你跑这个程序试试?
#include
using namespace std;
struct Node
{
int val;
Node * next;
Node(int value):val(value), next(0){}
};
void deleteNode(Node*& node){
if(!node) return;
if(!node->next){
delete node;
node = NULL;
}else{
node->val = node->next->val;
node->next = node->next->next;
delete node->next;
}
}
int main()
{
Node *head = new Node(1);
head->next = new Node(2);
Node *p = head->next;
... 阅读全帖
y**i
发帖数: 1112
2
来自主题: JobHunting版 - 白板代码,支持O(1)时间GetMin的stack
贴一个我的,正好前几天也写了一个:
template
class Stack
{
T* val;
T* min;
int max_size;
int top;
public:
class Underflow { };
class Overflow { };
Stack() : max_size(1000), top(0) { val = new int[max_size]; min = new
int[max_size]; }
Stack(int s) : max_size(s), top(0) { val = new int[max_size]; min = new
int[max_size]; }
~Stack() { delete[] val; delete[] min; }
void Push(T t);
T Pop();
T GetMin();
};
template void Stack::Push(T t)
{
if (top
f*******r
发帖数: 1086
3
来自主题: JobHunting版 - ms面试题
刚刚写了一个,欢迎大家拍砖!
struct DLNode
{
int val;
DLNode* prev;
DLNode* next;
DLNode()
{
int val = -1;
prev = NULL;
next = NULL;
}
};
DLNode* ConvertBSTToDLL(BST_Node* root, bool bRight)
{
if(root==NULL) return NULL;
DLNode* newNode = new DLNode();
newNode->val = root->val;
DLNode* pPrevNode = ConvertBSTToDLL(root->left, false);
if(pPrevNode!=NULL)
{
c*******n
发帖数: 63
4
来自主题: JobHunting版 - 问个G家面经题
我觉得design一个data structure的话,就不必拘泥于一般的stack需求
比如实现一个0和1的序列stack
pop就是返回最低有效位,并右移一位等

pop()
{
int ret = val & 1;
val >>= 1;
return ret;
}
反之
push(int i) // i=1 or i=0
{
val <<= 1;
val |= i;
}
b**********r
发帖数: 91
5
来自主题: JobHunting版 - 问道amazon的面试题
Refined the search logic by calculate the lower bound more precisely
Now even this example can be done around 1 second
0, 10, 10, 5, 7, 3, 4, 2, 9, 6, 1, 7, 10, 2, 2, 2, 2, 4, 8
1, 2, 2, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8,
8, 8, 9, 9, 10, 10, 10, 10, 10, 11, 12, 12, 12, 12, 14, 14, 14, 14, 15,
15, 15, 15, 16, 16, 16, 16, 17, 17, 18, 18, 18, 18, 18, 19, 19, 20, 20,
20, 21, 21, 21, 22, 22, 22, 22, 23, 23, 24, 24, 24, 25, 25, 25, 25, 25,
25, 26, 26, 28, 29, 29, 29, 30, 30... 阅读全帖
b**********r
发帖数: 91
6
来自主题: JobHunting版 - 问道amazon的面试题
Refined the search logic by calculate the lower bound more precisely
Now even this example can be done around 1 second
0, 10, 10, 5, 7, 3, 4, 2, 9, 6, 1, 7, 10, 2, 2, 2, 2, 4, 8
1, 2, 2, 2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8,
8, 8, 9, 9, 10, 10, 10, 10, 10, 11, 12, 12, 12, 12, 14, 14, 14, 14, 15,
15, 15, 15, 16, 16, 16, 16, 17, 17, 18, 18, 18, 18, 18, 19, 19, 20, 20,
20, 21, 21, 21, 22, 22, 22, 22, 23, 23, 24, 24, 24, 25, 25, 25, 25, 25,
25, 26, 26, 28, 29, 29, 29, 30, 30... 阅读全帖
a**********2
发帖数: 340
7
来自主题: JobHunting版 - G面经
int reverseInteger(int val)
{
int result =0 ;
while(val)
{
result *=10;
result += val%10;
val /= 10;
}
return result;
}
a**********2
发帖数: 340
8
来自主题: JobHunting版 - G面经
int reverseInteger(int val)
{
int result =0 ;
while(val)
{
result *=10;
result += val%10;
val /= 10;
}
return result;
}
g*********e
发帖数: 14401
9
来自主题: JobHunting版 - google这是什么意思?

我答的是这样的,请大家指正。
我谈了做实习时给图做优化,sign extension的问题。eda的东西,他听了似乎不是太
有兴趣。
hashtable O(1) access time, no order,通过key来map
bst O(logn) time, but can retain the order of stored item
我一开始说bst是balanced tree. 他指出不是。又问bst不balance会怎么样,worst
time analysis. 以及有啥方法balance. 我跟他说avl tree 或者红黑树,但没要求写
具体代码。
接着还追问了hash collision怎么处理,我说可以弄个list append上去,或者probing
(然后稍微解释了下probing)
我首先想到的是debug statement的副作用,可能里面执行了什么函数。其他我说想不
出来。他说加了一行code会改变什么?我说executable大小会改变,load到内存位置也
会不一样。我说可能是内存某一块坏了,刚好load到了那块。接着他问还会改变什么?
我说可能... 阅读全帖
c*********t
发帖数: 2921
10
来自主题: JobHunting版 - 计算组合数C(m,n)
Here is the best way to compute C(n, m),
O(m)
int combin(int n, int m)
{
int val = 1;
int i;
//every iteration is like to compute C(n ,i)
for (i = 1; i <= m; i++) {
val *= n - (i - 1);
val /= i;
}
return val;
}
h****n
发帖数: 1093
11
来自主题: JobHunting版 - M家 onsite 悲剧,同胞们弄死烙印吧
我的递归和非递归解法,贴在下面 OJ通过 ,还有每K个reverse那个,不过那个只有递
归版本
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *swapPairs(ListNode *head) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
/*
//递归做法很简单,但是要切记前面几行的顺序,别放错了
ListNode* cur = head;
if(cur==NULL) return NULL;
ListNode* n... 阅读全帖
h****n
发帖数: 1093
12
来自主题: JobHunting版 - M家 onsite 悲剧,同胞们弄死烙印吧
我的递归和非递归解法,贴在下面 OJ通过 ,还有每K个reverse那个,不过那个只有递
归版本
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *swapPairs(ListNode *head) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
/*
//递归做法很简单,但是要切记前面几行的顺序,别放错了
ListNode* cur = head;
if(cur==NULL) return NULL;
ListNode* n... 阅读全帖
k*****y
发帖数: 744
13
来自主题: JobHunting版 - 请教一道单链表问题
ListNode *deleteDuplicate(ListNode *head){
ListNode *newHead = NULL, *newLast = NULL;
ListNode *next;
while(head){
next = head->next;
while(next && next->val == head->val) next = next->next;
if(next == head->next){
if( newLast ){
newLast->next = new ListNode(head->val);
newLast = newLast->next;
}
else
newHead = newLast = new ListNode(head->val);
}
head = next;
... 阅读全帖
k*******2
发帖数: 84
14
来自主题: JobHunting版 - 请教一道单链表问题
Solution with Dummy node at the bottom. And how will recursion work here,
please?
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(head == NULL)
return head;

ListNode *dummy = new ListNode(0);
dummy->next = head;

ListNode *p1 = dummy;
ListNode *p2 = dummy->next;


while(p2!= NULL && p2->... 阅读全帖
p*****2
发帖数: 21240
15
来自主题: JobHunting版 - 亚麻onsite面经
最后一题是反着clone BT吧?
将一个bt,mirror一下返回一个新的数,要求不改变原来的树。我跟他说
,我不想做了,头一天坐了6个小时的飞机,他就送我下楼。
class Node:
def __init__(self,val):
self.val=val;
left=None;
right=None;
def clone(root):
if root==None:
return None;
Copy=Node(root.val)
Copy.left=clone(root.right)
Copy.right=clone(root.left)
return Copy
c********r
发帖数: 286
16
来自主题: JobHunting版 - 问一道题
public class FindDuplicatedInt {
public static int FindDup(int[] arr)
{
if(arr.length > 1002 || arr == null) return -1;
int checker = 0;
for(int i = 0;i int val = arr[i];
if((checker & (1<0){
return val;
}
checker |= (1< }
return -1;
}
public static void main(String[] args) {
int [] a = new int[]{1,2,3,4,5,6,7,8,9,10,2};
System.out.pri... 阅读全帖
p*****2
发帖数: 21240
17
来自主题: JobHunting版 - leetcode过的一代工程师
我写了一个10行以内的。
ListNode sub(ListNode node, ListNode prev)
{
if(node==null)
return null;
ListNode ret=sub(node.next, node);
if(prev!=null && prev.val==node.val || node.next!=null && node.val==
node.next.val)
return ret;
else
{
node.next=ret;
return node;
}
}

public ListNode deleteDuplicates(ListNode head)
{
return sub(head,null);
}

return
p********s
发帖数: 37
18
来自主题: JobHunting版 - leetcode过的一代工程师
凑个热闹
ListNode* deleteDuplicates(ListNode *head) {
for(ListNode *cur = head; cur && cur->next; cur = cur->next)
while (cur->next && cur->val == cur->next->val) {
ListNode *tmp = cur->next;
cur->next = tmp->next;
delete tmp;
}
return head;
}
如果再猥琐点的话:
ListNode* deleteDuplicates(ListNode *head) {
for(ListNode *cur = head; cur && cur->next; cur = cur->next)
while (cur->next && cur->val == cur->next->val)
cur->next = cur->next->next;
return head;
}
p*****2
发帖数: 21240
19
来自主题: JobHunting版 - leetcode过的一代工程师

都差不多吧。
public ListNode deleteDuplicates(ListNode head)
{
ListNode curr=head;
ListNode tail=head=null;
for(ListNode prev=null;curr!=null;prev=curr,curr=curr.next)
{
if((prev==null || prev.val!=curr.val) && (curr.next==null ||
curr.val!=curr.next.val))
{
if(tail==null)
{
tail=curr;
head=curr;
}
else
{
... 阅读全帖
i**********e
发帖数: 1145
20
来自主题: JobHunting版 - 一道msft的题
这题最坏情况要O(n)吧,必须检查每一个节点。
基本思路就是 inorder traversal 和保存前一个节点,但不用额外的空间。
TreeNode *first, *second, *prev;
void recoverHelper(TreeNode *p) {
if (!p) return;
recoverHelper(p->left);
if (prev && prev->val > p->val) {
if (!first)
first = prev;
second = p;
}
prev = p;
recoverHelper(p->right);
}
void recover(TreeNode *root) {
first = second = prev = NULL;
recoverHelper(root);
swap(first->val, second->val);
}
s******e
发帖数: 108
21
来自主题: JobHunting版 - Get first Greater in a array
Implement an array on ints which supports
1) void lookup(int idx) : value stored at idx, in amortized O(1) time.
2) void append(int value) : adds a value at the end of the array, in
amortized
O(1) time.
3) void set(int idx, int val) : sets the value at idx to val. if idx >
length
, throws an error. Complexity amortized O(1) time.
4) int GetFirstMaxIndex(int val): Gets the first index such that the element
at the idx is > val. Complexity O(logn).
ex [2, 10,5,6,80]
input : 6 output : 10
input :20 ... 阅读全帖
g*********e
发帖数: 14401
22
来自主题: JobHunting版 - 上一道我以前喜欢出的题目吧
int getNum(char **s){
if(**s == '\0')
return 0;
int val=0;
while(**s != '.' && **s != '\0'){
val = val*10 + (**s)-'0';
(*s)++;
}
return val;
};
int compareVersion( char * s1, char * s2){
if(*s1 == '\0' && *s2 == '\0')
return 0;
if(*s1 == '.')
s1++;
if(*s2 == '.')
s2++;
int a = getNum(&s1);
int b = getNum(&s2);
printf("comparing %d and %d\n", a, b);
if(a > b)
return 1;
else if(a < b)
... 阅读全帖
g*********e
发帖数: 14401
23
来自主题: JobHunting版 - 上一道我以前喜欢出的题目吧
fix after the leading zero issue
int getNum(char **s, int &leadZeros){
if(**s == '\0')
return 0;
int val=0;
bool lead=true;
leadZeros=0;
while(**s != '.' && **s != '\0'){
if(lead && **s == '0')
leadZeros++;
else
lead = false;
val = val*10 + (**s)-'0';
(*s)++;
}
return val;
};
int compareVersion( char * s1, char * s2){
if(*s1 == '\0' && *s2 == '\0')
return 0;
if(*s1 == '.')
s1++;
... 阅读全帖
l*********8
发帖数: 4642
24
来自主题: JobHunting版 - 请教这么一个题:BST maximum sum path
赞! 思路和代码都很简洁。
不过我觉得有个bug:
g = root->val + max(leftG, rightG);
应该为:
g = max(0, root->val + max(leftG, rightG));
test case:
1
\
5
\
-7
答案应该是6,本程序返回5.
另外, if (root->left)也可以去掉。
试在你的基础上修改如下:
int solve(TreeNode *root, int &g) {
if (!root) {
g = 0;
return 0;
}
int leftG = 0, rightG = 0;
int f = max(solve(root->left, leftG),solve(root->right, rightG);
f = max(f, leftG + rightG + root->val);
g = max(0, root->val + max(leftG, rightG));
return f;
}
int so... 阅读全帖
l********t
发帖数: 878
25
另外谢谢 longway2008,确实是算单线最长的时候没考虑周到。贴个改过后work的
class Solution {
public:
int maxValue;
int maxPathSum(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int singleDown;
maxValue = -9999;
return maxPathOverload(root, singleDown);
}
int maxPathOverload(TreeNode* root, int& singleLine){
if (!root){
singleLine = 0;
return 0;
}
int leftSingleLine = 0;
... 阅读全帖
h****n
发帖数: 1093
26
和值有关系么,理解错了,以为是所有子树一样的节点,不过道理都一样
代码如下
vector GetAllUnivalNodes(TreeNode * root)
{
vector res;
helper(root, res);
return res;
}
bool helper(TreeNode * root, vector & res)
{
if(root==NULL)
return true;
bool leftRes = helper(root->left, res);
bool rightRes = helper(root->left, res);
if(!leftRes||!rightRes)
return false;
if(root->left&&root->val!=root->left->val)
return false;
if(root->right&&root->val!=r... 阅读全帖
p*****2
发帖数: 21240
27
来自主题: JobHunting版 - Uni_value subtree problem

int ans=0;
boolean dfs(Node root)
{
if(root==null)
return true;
boolean l=dfs(root.left);
boolean r=dfs(root.right);
if(l && r && (root.left==null || root.left.val==root.val) && (root.
right==null || root.right.val==root.val))
{
ans++;
return true;
}

return false;
}
t*********n
发帖数: 89
28
来自主题: JobHunting版 - Binary Tree Maximum Path Sum
贴个我写的代码,将就看了。
int maxPathSum(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int max = INT_MIN;
maxPath(root,max);
return max;
}
int maxPath(TreeNode *root,int &max){
if (!root)
return 0;
int l = maxPath(root->left,max);
int r = maxPath(root->right,max);
if ((l+r+root->val)>max){
max = l+r+root->val;
}
int big = l>r?(l+root->val):(r+root->val);
return big>0?big:0;
}
e****e
发帖数: 418
29
来自主题: JobHunting版 - recover binary search tree 常数空间
Inorder traverse, keep the prev value and prev tree node,
Find first misplaced node by
if ( current.val < prev.val )
Node first = prev;
Find second by
if ( current.val < prev.val )
Node second = current;
After traversal, swap the values of first and second node.
Only need two pointers, prev and current node, and two variables, first and
second node. O(1) space.
y***u
发帖数: 174
30
来自主题: JobHunting版 - 明天A家onsite
写了一下next successor: O(1) space, O(lgN) time。
Node getNextSuccessor(Node root, Node node){
if(root==null || node == null){
return null;
}
Node pre = null;
while(root!=null && root!=node){
if(root.val > node.val){
// go left
pre = root;
root = root.left;
}else if(root.val < node.val){
// right
root = root.right;
}else{
//found
if(node.right!=null){
... 阅读全帖
c*****a
发帖数: 808
31
这个时灵时不灵,有时候能过,有时候不能过.....
Run Status: Accepted!
Program Runtime: 840 milli secs
public class Solution {
public int maximalRectangle(char[][] matrix) {
// Start typing your Java solution below
// DO NOT write main() function
if(matrix.length <=0 )
return 0;
int col=matrix[0].length;
int[] ar = new int[col];
int max=0;
for(int i=0; i for(int j=0; j if(matr... 阅读全帖
P*******U
发帖数: 203
32
不用转的,可以直接搞
public class Solution {
public TreeNode sortedListToBST(ListNode head) {
// Start typing your Java solution below
// DO NOT write main() function
if(head == null)
return null;

if(head.next == null)
return new TreeNode(head.val);

ListNode head1, head2;
head1 = head;
head2 = head;

while(head2!=null && head2.next!=null){
head1 = head1.next;
head2... 阅读全帖
l*******b
发帖数: 2586
33
来自主题: JobHunting版 - 两道题目
试一下scala写的第一个程序。。。
val list = Array[Int](1,3,4,5,7,9)
def powerSet(l: Array[Int]) = {
def select(i: Int, n:Int) =
for(j <- 0 to n if((i & (1 << j)) != 0)) yield l(j)
val n = l.length - 1
val p = (1 << (n+1)) - 1
val power = for(i <- 0 to p) yield select(i,n).mkString(" ")
power.mkString("\n")
}
o***d
发帖数: 313
34
今天发生两次了,c++, 同样的test case,我用VS2010和cygwin都测试没问题,leetcode就
是fail........
比如这个:
/**
* 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 isValidBST(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
// Start typing your C/C++ solution below
// DO NOT write int main() ... 阅读全帖
o***d
发帖数: 313
35
来自主题: JobHunting版 - 狗店面,求BLESS
大牛,我仔细重新看了一下第一题,不明白到底题意是什么,能再解释一下么?
我猜的Node:
//each node can have at most two parents
struct Node
{
Node* left;
Node* right;
Node* leftParent;
Node* rightParent;
};
//另一种
struct Node
{
Node* left;
Node* right;
vector parents;
};
问题1:================================================
从父亲到子节点是可以双向访问的?换句话说,如果一个子节点有一个父节点,那么从父
节点也可以访问到资节点?
如果是这样,我觉得单纯用parents就可以搞定这题,不需要加标号阿.你后面回帖的反例
里面,一个节点有3个父节点.那么你在查找这个子节点时,仍然能发现一个树的父节点有
3个,另一个只有两个阿.
我找不到反例。
//P-code
//return false if any check f... 阅读全帖
w****x
发帖数: 2483
36
来自主题: JobHunting版 - 请教各位大牛一个K-way merge 的问题
不用堆得版本:
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode *mergeKLists(vector &lists) {
ListNode* pHead = NULL;
ListNode* pIter = NULL;
while (true)
{
int p = -1;
for (int i = 0; i < lists.size(); i++)
{
if (lists[i] != NULL)
{
if (p < 0 || lists[i]->val < lists[p]->val)
p = i;
}
}
if (p < 0) break;
... 阅读全帖
c*****a
发帖数: 808
37
来自主题: JobHunting版 - 请教各位大牛一个K-way merge 的问题
我的用堆
public ListNode mergeKLists(ArrayList lists) {
Comparator mycomp = new Comparator(){
@Override
public int compare(ListNode a, ListNode b){
if(a.val else if(a.val==b.val) return 0;
else return 1;
}
};
if(lists.size()==0) return null;
PriorityQueue heap = new PriorityQueue(lists.
size(), mycomp);
for... 阅读全帖
p*****2
发帖数: 21240
38
来自主题: JobHunting版 - FP感受
刚做了一个小题。不知道第二种写法面试会不会悲剧?
object test2 extends App {
val n=readLine.toInt
val a=readLine.split(" ").map(_.toInt)

var max=0
for(i<-0 until n)
{
var value=a(i)
max=math.max(max,a(i))
for(j<-i+1 until n)
{
value^=a(j)
max=math.max(max,value)
}
}
println(max)
}
object test2 extends App {
val n=readLine.toInt
val a=readLine.split(" ").map(_.toInt)
println((for(i<-0 until n) yield for(j<-i+1 to n... 阅读全帖
p*****2
发帖数: 21240
39
来自主题: JobHunting版 - g电面 详细面经
val m=5
val n=5
val board=Array(".BB..","B.WB.","B.WB.",".BWB.","..B..")
val visited=Array.ofDim[Boolean](m,n)

println(dfs(1,2))

def dfs(i:Int, j:Int):Boolean={
if(i<0 || i>=n || j<0 || j>=n) return true
if(board(i)(j)=='B' || visited(i)(j)) return false
visited(i)(j)=true
if(dfs(i-1,j)) return true
if(dfs(i+1,j)) return true
if(dfs(i,j-1)) return true
if(dfs(i,j+1)) return true

false
}
c*u
发帖数: 22
40
来自主题: JobHunting版 - 请假大家一道BB的题
这样可以么
String str = "lacyrkaplcknb";
char[] cr = str.toCharArray();
LinkedHashSet set = new LinkedHashSet();
for (char val : cr) {
if (set.contains( val ))
set.remove( val );
else
set.add( val );
}
System.out.println( set.iterator().next() );

node
p*****2
发帖数: 21240
41
来自主题: JobHunting版 - 关于尾递归

尾递归的实现,不过Scala不支持这种递归的优化。
def H(value:Double, n:Int):Double={
H_helper(Array(value),n)(0)
}

def V(value:Double, n:Int):Double={
V_helper(Array(value),n)(0)
}

def H_helper(arr:Array[Double], n:Int):Array[Double]=n match{
case 0 => caclV(arr)
case _ => {
val ar=new Array[Double](arr.length*2)
for(i<- 0 until arr.length) {
ar(2*i)=arr(i)-1
ar(2*i+1)=arr(i)+1
}
... 阅读全帖
p*****2
发帖数: 21240
42
来自主题: JobHunting版 - 比较久之前T家的面试
def solve(n:Int):Int={
val isPow=(n:Int)=>{var m=n;while(m%2==0) m/=2;m==1}
val visited=new Array[Boolean](n+1)
val queue=new Queue[Int]
queue+=n
visited(n)=true
var count=1
var distance=0

while(!queue.isEmpty){
while(count>0){
val curr=queue.dequeue
if(curr==0) return distance
for(i<-0 until n if !visited(i) && isPow((curr-i).abs)){
queue+=i... 阅读全帖
p*****2
发帖数: 21240
43
来自主题: JobHunting版 - G 家面经

是我刚才的程序有个bug。你看看这个对不对。
object test6 extends App {
val mat=Array(Array(4,4,0), Array(5,5,5), Array(0, 4, 4))
val m=3
val n=3
val dp=Array.ofDim[Int](m,n,m,n)

for(i<-0 until m; j<-0 until n; x<-0 until m; y<-0 until n if i+j==x+y){
if(i>0 && x>0) dp(i)(j)(x)(y)=math.max(dp(i)(j)(x)(y), dp(i-1)(j)(x-
1)(y))
if(i>0 && y>0) dp(i)(j)(x)(y)=math.max(dp(i)(j)(x)(y), dp(i-1)(j)(x)
(y-1))
if(j>0 && x>0) dp(i)(j)(x)(y)=math.max(dp(i)(j)(x)(y), dp(i)(j-1)(x-
... 阅读全帖
p*****2
发帖数: 21240
44
来自主题: JobHunting版 - G 家面经

是我刚才的程序有个bug。你看看这个对不对。
object test6 extends App {
val mat=Array(Array(4,4,0), Array(5,5,5), Array(0, 4, 4))
val m=3
val n=3
val dp=Array.ofDim[Int](m,n,m,n)

for(i<-0 until m; j<-0 until n; x<-0 until m; y<-0 until n if i+j==x+y){
if(i>0 && x>0) dp(i)(j)(x)(y)=math.max(dp(i)(j)(x)(y), dp(i-1)(j)(x-
1)(y))
if(i>0 && y>0) dp(i)(j)(x)(y)=math.max(dp(i)(j)(x)(y), dp(i-1)(j)(x)
(y-1))
if(j>0 && x>0) dp(i)(j)(x)(y)=math.max(dp(i)(j)(x)(y), dp(i)(j-1)(x-
... 阅读全帖
c***s
发帖数: 192
45
用两个边界来判断的思路是对的,不过你的codes写得复杂了点。
下面是我写的:
public class ValidateBinarySearchTree {
public boolean isValidBST(TreeNode root, int min, int max) {
if(root == null) return(true);
if(root.val <= min || root.val >= max) return(false);
return(isValidBST(root.left, min, root.val) &&
isValidBST(root.right, root.val, max));
}
public boolean isValidBST(TreeNode root) {
return(isValidBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE));
}
}

:
O(
tree
b*******n
发帖数: 847
46
谢谢!
经过你提醒我改了我的code,这回过了,发现用array的index来referecen简单多了
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector > levelOrderBottom(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector > traversal;
vector path;
vector阅读全帖
p*****2
发帖数: 21240
47
来自主题: JobHunting版 - 菜鸟问个题
val str="abcabcbcc"
val arr=Array.tabulate(26)(i=>Array(i,0))
str.foreach{c=>arr(c-'a')(1)+=1}
val sorted=arr.sortBy(-_(1))
val ss=for(i<-0 until 26 if (sorted(i)(1)>0)) yield{
(sorted(i)(0)+'a').toChar+" "+sorted(i)(1)
}
println(ss.mkString(","))
m*****n
发帖数: 204
48
来自主题: JobHunting版 - Java编程讨论:LinkedIn的H2O

public class ElementsImpl implements IElements {

private final int batchSize;
private final AtomicInteger counter = new AtomicInteger();

ElementsImpl(int i) {
batchSize = i;
}

public int getBatchSize() {
return batchSize;
}
@Override
public void inc() {
counter.incrementAndGet();
}
@Override
public boolean reserve() {
int val = co... 阅读全帖
m*****n
发帖数: 204
49
来自主题: JobHunting版 - Java编程讨论:LinkedIn的H2O

public class ElementsImpl implements IElements {

private final int batchSize;
private final AtomicInteger counter = new AtomicInteger();

ElementsImpl(int i) {
batchSize = i;
}

public int getBatchSize() {
return batchSize;
}
@Override
public void inc() {
counter.incrementAndGet();
}
@Override
public boolean reserve() {
int val = co... 阅读全帖
n*p
发帖数: 298
50
in order traversal
要用一个变量来存前一个Node的值。但发现有时候值没有被更新,结果是错的,是因为
Java的passing by value的原因吗?代码如下:
public boolean isValidBST(TreeNode root) {
// Start typing your Java solution below
// DO NOT write main() function
if (root == null) return true;
//if (root.left == null && root.right == null) return true;

Integer preVal = Integer.MIN_VALUE;
return inOrder (root, preVal);

}

public boolean inOrder(TreeNode root, Integer preVal... 阅读全帖
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)