由买买提看人间百态

topics

全部话题 - 话题: assertive
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
i**********e
发帖数: 1145
1
来自主题: JobHunting版 - 请问一道很难的面试题
刚想到很精简的 code,不需要用 post-fix stack-based expression,直接储存进一
个 string 数组即可。
效率应该是不错的了,有一些方面可以考虑提高效率,例如用 vector 需要删除元素可
能会慢些,但 vector 里的元素很少,应该影响不大。
还有一点就是每次传递归的时候,都需要把所有的数组重新 copy 一次。这是无可避免
的,因为每次进入下一层递归时必须传 copy,不能在原有数组中更改。
#include
#include
#include
#include
#include
using namespace std;
void generate(vector A, int target, int s, vector expression);
template
void eraseAt(vector &vec, int i) {
typename vector::iterator ... 阅读全帖
z*d
发帖数: 18
2
来自主题: JobHunting版 - 问一道 ama的除法题
#include
#include
int divide (int x, int y) {
int m, n, sign_m, sign_n;
int j, k, l;
if (y == 0) {
printf("Dvided by 0 error! \n");
assert(0);
}
if (x < 0) {
m = -x;
sign_m = -1;
}
else {
m = x;
sign_m = 1;
}
if (y < 0) {
n = -y;
sign_n = -1;
}
else {
n = y;
sign_n = 1;
}
j = 0;
l = m;
while(1) {
k = (j+l)/2;
if (k*n > m)... 阅读全帖
z*d
发帖数: 18
3
来自主题: JobHunting版 - 问一道 ama的除法题
How about this one? Any suggestions or corrections will be highly
appreciated.
#include
#include
int divide (int x, int y) {
long long dividend, divisor, sign_dividend, sign_divisor;
long long low, middle, high;
int quotient;
/* check whether divisor is 0 */
if (y == 0) {
printf("Dvided by 0 error! \n");
assert(0);
}

/* unsign both dividend and divisor and mark the signess */
if (x < 0) {
dividend = -x;
sign... 阅读全帖
i**********e
发帖数: 1145
4
来自主题: JobHunting版 - 问道amazon的面试题
My code found two valid answers for the following input in about 3 secs (
obviously not as good as grass, but the algorithm is easier to code).
Input, A = {2 2 4 6 7 7 8 8 8 9 9 10 11 12 13 14 15 16 16 17 18 20 21 22 23
24 25 26 28 29 30 31 33 34 37 38 39 40 41 45 47 47 49 54 56 }
Answer:
2 7 2 4 8 8 8 10 7
7 10 8 8 8 4 2 7 2
grass, can you explain whether your code generates the solution {7 10 8 8 8
4 2 7 2} ?
Test cases:
A = {7,10,5,2,8,3}
Answer:
2 5 3
3 5 2
A = {1,1,1,2,3,2}
Answer:
1 1 1
A ... 阅读全帖
i**********e
发帖数: 1145
5
来自主题: JobHunting版 - 问道amazon的面试题
My code found two valid answers for the following input in about 3 secs (
obviously not as good as grass, but the algorithm is easier to code).
Input, A = {2 2 4 6 7 7 8 8 8 9 9 10 11 12 13 14 15 16 16 17 18 20 21 22 23
24 25 26 28 29 30 31 33 34 37 38 39 40 41 45 47 47 49 54 56 }
Answer:
2 7 2 4 8 8 8 10 7
7 10 8 8 8 4 2 7 2
grass, can you explain whether your code generates the solution {7 10 8 8 8
4 2 7 2} ?
Test cases:
A = {7,10,5,2,8,3}
Answer:
2 5 3
3 5 2
A = {1,1,1,2,3,2}
Answer:
1 1 1
A ... 阅读全帖
i**********e
发帖数: 1145
6
来自主题: JobHunting版 - 问个google面试题
刚才写的,没有验证过。。。
typedef pair IntNode;
// precondition: binary tree must be complete. (eg, each node must have
either 0 or 2 children)
IntNode maxSumLeafNodes(Node *root, int sumFromRoot, Node *& leaf1, Node *&
leaf2, int &maxSum) {
assert(root);
// base case: leaf node (no children)
if (!root->left && !root->right) return IntNode(root->data, root);
// must have 2 children
assert(root->left && root->right);

IntNode fromLeft = maxSumLeafNodes(root->left, sumFromRoot + roo... 阅读全帖
i**********e
发帖数: 1145
7
来自主题: JobHunting版 - 这么多G的面经,我也想想 ~~
LZ,32 的 super cool 应该是 32 才对吧?
因为 32 = 2^5.
刚写了一个程序,利用 binary search 的思路.
首先选定一个 upper bound,也就是 ceiling(log(n)).
然后再 i=2 到 upper bound 每一个做 binary search.
例如,n=32,upper bound = log(32) = 5.
i=2,从 low = 1 到 high = ceiling(n^(1/2))
i=3, 从 low = 1 到 high = ceiling(n^1/3))
..
以此类推...
binary search 的 invariant 就是比较 target 和 middle and middle's next,看哪
一个比较近。如果是离 middle 比较近,那就 middle's next 和它上边的值都可以抛
掉了。相反,那就 middle 和它下边的值就可以抛掉。
这里,我们确保 middle's next 不会大于 high。这个条件可以保证,因为取的是
lower middle = (L+H)/2.
... 阅读全帖
g**********y
发帖数: 14569
8
来自主题: JobHunting版 - careerup 150的一道经典题
这是书上的答案,它把isSquare()当成O(1)的operation.
1 public static Subsquare findSquare(int[][] matrix){
2 assert(matrix.length > 0);
3 for (int row = 0; row < matrix.length; row++){
4 assert(matrix[row].length == matrix.length);
5 }
6
7 int N = matrix.length;
8
9 int currentMaxSize = 0;
10 Subsquare sq = null;
11 int col = 0;
12
13 // Iterate through each column from left to right
14 while (N - col > currentMaxSize) { // See step 4 above
15 for (int row = 0; row < matrix.length; row++){
16 // starting from ... 阅读全帖
i**********e
发帖数: 1145
9
来自主题: JobHunting版 - 一道G家题
我写了一个,测试了几个edge cases,全都 pass 了,大家参考下:
int notInArray(int A[], int n) {
int L = 0, H = n;
while (H-L > 1) {
int M = L + (H-L)/2;
assert(M >= 0 && M <= n-1);
if (M-L >= A[M]-A[L])
L = M;
else
H = M;
}
assert(L >= 0 && L <= n-1);
return A[L]+1;
}
s*****y
发帖数: 897
10
来自主题: JobHunting版 - 问个1337网页上面的经典题
Construct Binary Tree From Inorder and Preorder/Postorder Traversal
const int MAX = 256;
// a fast lookup for inorder's element -> index
// binary tree's element must be in the range of [0, MAX-1]
int mapIndex[MAX];
void mapToIndices(int inorder[], int n) {
for (int i = 0; i < n; i++) {
assert(0 <= inorder[i] && inorder[i] <= MAX-1);
mapIndex[inorder[i]] = i;
}
}
// precondition: mapToIndices must be called before entry
Node *buildInorderPreorder(int in[], int pre[], int n, int offse... 阅读全帖
i**********e
发帖数: 1145
11
来自主题: JobHunting版 - G onsite面经
关于第一题,突然想到一个方法:
既然区间里只有7天,那为何不用一个 byte (8 bits)来表示一个区间呢?
这样判断两个区间是否相交,只要两个区间做一个 and operation,0 就代表不相交,
否则相交。
typedef unsigned char u8;
u8 setBits(int i, int j) {
if (i > j) return 0;
assert(j >= 0 && j <= 6);
assert(i >= 0 && i <= j);
u8 x = (1U << (j+1)) - 1;
u8 y = (1U << i ) - 1;
return x ^ y;
}
u8 createInterval(int start, int end) {
if (start <= end)
return setBits(start, end);
else
return ~setBits(end+1, start-1);
}
bool isIntersect(int a, int b, int c, int d) {... 阅读全帖
i**********e
发帖数: 1145
12
来自主题: JobHunting版 - 两道F电面题
我刚找到你代码有个 bug,请试一下这个 case:
isMatch("a", "ab*") -> 这个应该返回的是 true.
我尝试写这个递归程序的时候也犯了跟你同样的错误了。
我的代码如下,感觉跟你思路挺像,但是我的多了个 while loop. (那个 do-while
loop 不用管它,只是跳掉多余的 '*'s).
bool isMatch(const char *s, const char *p) {
assert(s && p);
if (*p == '\0') return *s == '\0';
// next char is not '*': must match current character
if (*(p+1) != '*') {
assert(*p != '*');
return ((*p == *s) || (*p == '.' && *s != '\0')) && isMatch(s+1, p+1);
}
// next char is '*'
char rep = *p; // repeated ch... 阅读全帖
i**********e
发帖数: 1145
13
来自主题: JobHunting版 - 两道F电面题
我刚找到你代码有个 bug,请试一下这个 case:
isMatch("a", "ab*") -> 这个应该返回的是 true.
我尝试写这个递归程序的时候也犯了跟你同样的错误了。
我的代码如下,感觉跟你思路挺像,但是我的多了个 while loop. (那个 do-while
loop 不用管它,只是跳掉多余的 '*'s).
bool isMatch(const char *s, const char *p) {
assert(s && p);
if (*p == '\0') return *s == '\0';
// next char is not '*': must match current character
if (*(p+1) != '*') {
assert(*p != '*');
return ((*p == *s) || (*p == '.' && *s != '\0')) && isMatch(s+1, p+1);
}
// next char is '*'
char rep = *p; // repeated ch... 阅读全帖
y******n
发帖数: 47
14
来自主题: JobHunting版 - G家onsite面经
感觉功力还是不行, 看着不难, 写起来还是花了好一阵功夫. lastNode用来处理i大于
树节点总数的情况, 也就是返回right most node.
int findith(Node *r, int i) {
assert(r != NULL);
assert(i > 0);
int k = i; Node *lastNode;
Node * ret = helper(r, k, lastNode);
return (ret != NULL) ? ret->value : lastNode->value;
}
Node *helper(Node *n, int& k, Node *& lastNode) {
if (n == NULL)
return NULL;
Node *ret = helper(n->left, k, lastNode);
if (ret != NULL)
return ret;
k--;
if (k == 0)
return n;
lastNode = n;
return helpe... 阅读全帖
h********r
发帖数: 51
15
来自主题: JobHunting版 - Palantir面经
下面这个方法行不行? O(n), 模拟一次买卖的DP解法。大牛们看看对不对?
#include
#include
#include
void dump(int *x, int l) {

int i;
for (i = 0; i < l; ++ i) {

printf("%d ", x[i]);
}
printf("\n");
}
int main() {
int array[] = {3, 6, 8, 4, 5, 7, 9, 13, 15, 10, 6, 2, 7, 11, 8, 6};
int len = sizeof(array)/sizeof(int);
assert (len >= 4);
int *a = (int*)malloc(sizeof(int)*len);
int *b = (int*)malloc(sizeof(int)*len);
int *c = (int*)malloc(sizeof(... 阅读全帖
s******n
发帖数: 3946
16
来自主题: JobHunting版 - 感恩发面经-Amazon第一轮电面
看不懂if (mid==0 || ...)什么意思,这样是不是更清楚?
int find_max(int a[], int n)
{
ASSERT(n>0);
int i = 0, j = n-1;

while (i < j-1) {
int mid = (i + j)/2;
ASSERT(mid>i && mid if (a[mid-1] < a[mid] && a[mid] i = mid;
} else if (a[mid-1] > a[mid] && a[mid]>a[mid+1]) {
j = mid;
} else if (a[mid-1] < a[mid] && a[mid]>a[mid+1]) {
return mid;
} else {
//如果a[mid]==a[mid-1], ... 阅读全帖
w****x
发帖数: 2483
17
来自主题: JobHunting版 - 大家看看我写的这个itoa有没有bug
const char* myitoa(int num, char str[])
{
assert(a);
char* pIterBeg = str;
if (num < 0)
{
num = -num;
*pIterBeg++ = '-';
}
//use long not short to avoid INT_MIN overflow
unsigned int numshort = num;
char* pIter = pIterBeg;
while (true)
{
*pIter++ = numshort%10 + '0';
numshort = numshort/10;
if (numshort == 0) break;
}
*pIter = 0;
int nLen = strlen(pIterBeg);
assert(nLen > 0);
char* pIterEnd = ... 阅读全帖
S**I
发帖数: 15689
18
来自主题: JobHunting版 - [合集] G家onsite面经
☆─────────────────────────────────────☆
sharc (sharc) 于 (Mon Aug 22 15:15:14 2011, 美东) 提到:
刚从G家onsite归来。新鲜面经奉上。
总共5轮,4轮technical interview, 一个thesis discussion。在technical里,有编
程题,有open design。我记得的问题有:
1. 编程题:一堆字符串。找longest common prefix。
我的方法就是找最短的字符串,对它的每个字符,逐个与其他字符串对应位置比较。(
求更好方法)
2. open question: 一堆文件,size差别极大( from KB to many GB). 找出所有内
容相同的文件。
3. 编程题: 有一个observer 类,监视另一个类foo 的成员变量的值,每当那个值被
修改,就要调用 该observer.updated() 方法。需要实现 foo.registerObserver(ob)
, foo.unregisterObserver( ob )... 阅读全帖
l*****a
发帖数: 559
19
来自主题: JobHunting版 - 攒人品:google电面面经
I tried. Your code is not working.
#include
#include
#include
#include
#include
#include
using namespace std;
double GetMedian (int* a, int n, int* b, int m, int k)
{
assert(a && b);
if (n <= 0) return b[k-1];
if (m <= 0) return a[k-1];
if (k <= 1) return min(a[0], b[0]);
if (b[m/2] >= a[n/2])
{
if ((n/2 + 1 + m/2) >= k)
return GetMedian(a, n, b, m/2, k);
else
return GetMe... 阅读全帖
i**********e
发帖数: 1145
20
来自主题: JobHunting版 - leetcode OJ 不能使用exception?
不支持。
用 assert() 就可以。
如果 assert() 里的 expression 是false的话,就会 runtime error.
s******n
发帖数: 3946
21
来自主题: JobHunting版 - 说好得FG面经,回馈板上GGJJ
递归非DP做法
bool isScramble(char* str1, char* str2, int length) {
if (length==1) return *str1 == *str2;
for (int i=1; i if (isScramble(str1, str2+(length-i), i)
&& isScramble(str1+i, str2, length-i))
return true;
if (isScramble(str1, str2, i)
&& isScramble(str1+i, str2+i, length-i))
return true;
}
return false;
}
递归DP做法
class Solution {
char* str1;
char* str2;
int m;
int m2;
int m3;
int* DP;
#define dp(i,j,k) DP[m2 * (i) + m * (j) + (k) ]
public:
Solution(cha... 阅读全帖
w****x
发帖数: 2483
22
来自主题: JobHunting版 - 做了一下scramble string
/*
scramble string,judge if one string can be scrambled to another one
tiger
/ \
ti ger
/ \ / \
t i g er
/ \
e r
rotation is allowded
itreg
/ \
it reg
/ \ / \
t i g re
/ \
e r
then tiger can be changed to itreg
*/
bool _inner_can_scramble(const char* szStr1, const char* szStr2, int n);
bool CanScramble(const char* szStr1, const char* szStr2)
{
assert(szStr1 && szStr2);
int nLen1 = strlen(szStr1);
int nLen2 = strlen(szStr2);
if (nLen1 !... 阅读全帖
w****x
发帖数: 2483
23
来自主题: JobHunting版 - google scramble string O(n) 解法
贴一个递归和DP的:
/*
scramble string,judge if one string can be scrambled to another one
tiger
/ \
ti ger
/ \ / \
t i g er
/ \
e r
rotation is allowded
itreg
/ \
it reg
/ \ / \
t i g re
/ \
e r
then tiger can be changed to itreg
*/
bool _inner_can_scramble(const char* szStr1, const char* szStr2, int n);
bool CanScramble(const char* szStr1, const char* szStr2)
{
assert(szStr1 && szStr2);
int nLen1 = strlen(szStr1);
int nLen2 = strlen(szStr2);
... 阅读全帖
w****x
发帖数: 2483
24
来自主题: JobHunting版 - LeetCode Scramble String 疑问
/*
scramble string,judge if one string can be scrambled to another one
tiger
/ \
ti ger
/ \ / \
t i g er
/ \
e r
rotation is allowded
itreg
/ \
it reg
/ \ / \
t i g re
/ \
e r
then tiger can be changed to itreg
*/
bool _inner_can_scramble(const char* szStr1, const char* szStr2, int n);
bool CanScramble(const char* szStr1, const char* szStr2)
{
assert(szStr1 && szStr2);
int nLen1 = strlen(szStr1);
int nLen2 = strlen(szStr2);
if (nLen1 !... 阅读全帖
g****y
发帖数: 240
25
来自主题: JobHunting版 - 贡献几个题
第二题,只能想到linear的算法。
def find_duplicates(array):
assert array

for i in range(len(array)):
assert 0 <= array[i] < len(array)
while i != array[i]:
if array[array[i]] == array[i]:
return array[i]
else:
array[array[i]], array[i] = array[i], array[array[i]]

return None
g****y
发帖数: 240
26
来自主题: JobHunting版 - 贡献几个题
第二题,只能想到linear的算法。
def find_duplicates(array):
assert array

for i in range(len(array)):
assert 0 <= array[i] < len(array)
while i != array[i]:
if array[array[i]] == array[i]:
return array[i]
else:
array[array[i]], array[i] = array[i], array[array[i]]

return None
g****y
发帖数: 240
27
来自主题: JobHunting版 - 请问G一题
dynamic programming solution.
input: array, with all elements greater than or equal to zero.
p[i,j,k]: Ture, if for array[0..i], there exists a subset of length k, whose
sum equals to j; False otherwise
optimal substructure: p[i,j,k] = True if p[i-1,j,k] is True or p[i-1, j-
array[i], k -1] is True
After calculating p, the original problem becomes: for i = len(array)- 1, k
= len(array) / 2, find j such that p[i,j,k]=True and abs(sum - 2 * j) is
minimized.
def balanced_partition_even(array):
... 阅读全帖
g****y
发帖数: 240
28
来自主题: JobHunting版 - 请问G一题
dynamic programming solution.
input: array, with all elements greater than or equal to zero.
p[i,j,k]: Ture, if for array[0..i], there exists a subset of length k, whose
sum equals to j; False otherwise
optimal substructure: p[i,j,k] = True if p[i-1,j,k] is True or p[i-1, j-
array[i], k -1] is True
After calculating p, the original problem becomes: for i = len(array)- 1, k
= len(array) / 2, find j such that p[i,j,k]=True and abs(sum - 2 * j) is
minimized.
def balanced_partition_even(array):
... 阅读全帖
h**********9
发帖数: 3252
29
来自主题: JobHunting版 - 弱问一道G题
public class Divisible {
public static void main(String args[]) {
int a1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int a2[] = { 1, 2, 3, 4, 7, 10 };
System.out.println(isDivisible(a1, 5));
System.out.println(isDivisible(a2, 3));
}

public static boolean isDivisible(int a[], int k) {
assert (a != null && a.length % 2 == 0);
assert (k != 0);

int count[] = new int[k];
for(int i = 0; i < a.length; i++) {
... 阅读全帖
c********s
发帖数: 817
30
This is my implementation for these two functions, and a driver to run them.
cat string_reverse.c
#include
#include
#include
void swap(char* c1, char* c2);
// -----------------
void string_reverse1(char* string) {
if (string == NULL)
return;
char* head = string;
char* tail = head;
// find the tail
while (*tail) ++tail;
--tail;
// while loop to do the swap
while (head < tail)
swap(head++, tail--);
}
// -----------------
// the caller of this function is res... 阅读全帖
r*c
发帖数: 167
31
来自主题: JobHunting版 - 谷歌面经
#5
int getSevenFreeInt(int src){
assert(src > 0);
int res = src;
int nCount = 0;
int lastDigit = src % 10;
if(lastDigit >= 7) nCount++;
src -= lastDigit;
nCount += src/10;
res += nCount;
return res;
}

int getNormalInt(int encSrc) //encoded string with 8 for 7
{
assert(encSrc > 0);
int res = encSrc;
int nCount = 0;
int lastDigit = encSrc % 11;
if(lastDigit >= 8) nCount++;
... 阅读全帖
w****x
发帖数: 2483
32
来自主题: JobHunting版 - 求分析这题的时间复杂度
careercup上的一道题:
Imagine you have a square matrix, where each cell is filled with either
black or white. Design an algorithm to find the maximum subsquare such that
all four borders are filled with black pixels.
Assumption: Square is of size NxN.
This algorithm does the following:
1. Iterate through every (full) column from left to right.
2. At each (full) column (call this currentColumn), look at the subcolumns (
from biggest to smallest).
3. At each subcolumn, see if you can form a square with ... 阅读全帖
w****x
发帖数: 2483
33
发现这道题属于没见过肯定想不出来,知道怎么解也很难写对。昨天重新写了一下,觉
得学到蛮多的,分享一下。
以前写过一个很挫的DP版本:
int GetEditDist(const char* str1, const char* str2)
{
assert(str1 && str2);
int nLen1 = strlen(str1);
int nLen2 = strlen(str2);
int* rec = new int[nLen2];
bool bFound = false;
for (int i = 0; i < nLen2; i++)
{
if (str2[i] == str1[0])
bFound = true;
rec[i] = bFound ? i : i+1;
}
bFound = (str2[0] == str1[0]); //(str2[0] == str1[0]) not false
for (int i = 1; i < nLe... 阅读全帖
w****x
发帖数: 2483
34
来自主题: JobHunting版 - 拓扑排序的题怎么做?

/*
Given an array list of lots of strings, they are arranged by unknown
alphabetical
order, print all possible alphabetical orders (Sort)
*/
void buildMap(const char* str[], int n, int nBeg, bool bMap[26][26])
{
if (str == NULL || n <= 1) // <= 1 not 0
return;
int nPrev = 0;
while (str[nPrev][nBeg] == 0 && nPrev < n) // missing nPrev < n
nPrev++;
int nIter = nPrev + 1;
while (nIter < n)
{
if (str[nPrev][nBeg] != str[nIter][nBeg])
{
... 阅读全帖
w****x
发帖数: 2483
35
来自主题: JobHunting版 - 问一个题目,面试时我没有搞出来
//Given an array of positive and negative integers,
//re-arrange it so that you have postives on one end
//and negatives on the other, BUT retain the original order of appearance.
do it in-place
//e.g. 1, 7, -5, 9, -12, 15 => -5, -12, 1, 7, 9, 15
//When considering optimization from O(n^2) to O(nlogn),use divided &
conquer
void swapRange(int a[], int nBeg, int nEnd)
{
assert(a && nBeg <= nEnd);
while (nBeg < nEnd)
swap(a[nBeg++], a[nEnd--]);
}
//solution:
//e.g. in any stage: ---... 阅读全帖
s**********1
发帖数: 58
36
来自主题: JobHunting版 - 一道A家店面题求解
我也贴一个,没有上面lucky的简单,不过可以改成binary search,lucky的那个应该
不好改
主要就是写了个iterator
enum FindType {
Lesser,
LesserEqual,
Equal,
GreaterEqual,
Greater
};
class Iterator {
public:
Iterator(const vector& array)
:array(array)
{
assert(array.size());
ascending = array.front() <= array.back();
offset = ascending ? 0 : array.size() - 1;
}

bool can_step(bool forward) {
int new_offset = offset + ascending == forward ? 1 : -1;
retu... 阅读全帖
t*********n
发帖数: 89
37
来自主题: JobHunting版 - BB面试一题
void sleep(int n)
{
assert(n>0);
assert(n>5 || n%2==0);
int five = n/5;
int rest = n%5;
int two = 0;
switch(rest){
case '0':
break;
case '1':
{
five--;
two =3;
break;
}
case '2':
case '4':
{
two = rest/2;
break;
}
case '3':
{
five --;
two = 4;
}
default:
break;
}
... 阅读全帖
w****a
发帖数: 710
38
那也好办,创建实例的时候assert一下,是否是null。其实这个是最简单的。
类似于这样:
Singleton( void )
{
Assert( !s_pSingleton );
s_pSingleton = static_cast( this );
}

modifier
d****n
发帖数: 1241
39
来自主题: JobHunting版 - F面经
可以使用编译器在lexing和parsing的时候用的某种技术:recursive descent parsing
http://en.wikipedia.org/wiki/Recursive_descent_parser
下边的代码假设单个的数字也是合法的,比如:
1 合法
(1)合法
(1) + (2) 合法, 等等
#include
#include
#include
#include
using namespace std;
enum TokenKind {
TOK_Unknown = -1,
TOK_End,
TOK_Op,
TOK_LParen,
TOK_RParen,
TOK_Num,
};
static TokenKind getToken(const string &Input, int &Pos)
{
assert(Pos <= Input.length());
char TheChar = Input[Pos];
while (isspace(TheC... 阅读全帖
d****n
发帖数: 1241
40
来自主题: JobHunting版 - F面经
可以使用编译器在lexing和parsing的时候用的某种技术:recursive descent parsing
http://en.wikipedia.org/wiki/Recursive_descent_parser
下边的代码假设单个的数字也是合法的,比如:
1 合法
(1)合法
(1) + (2) 合法, 等等
#include
#include
#include
#include
using namespace std;
enum TokenKind {
TOK_Unknown = -1,
TOK_End,
TOK_Op,
TOK_LParen,
TOK_RParen,
TOK_Num,
};
static TokenKind getToken(const string &Input, int &Pos)
{
assert(Pos <= Input.length());
char TheChar = Input[Pos];
while (isspace(TheC... 阅读全帖
l*****s
发帖数: 279
41
来自主题: JobHunting版 - 发个刚面完的rocket fuel的面经吧
2nd question
public static int maxValue(int[] l, int[] w, int[] p, int L, int W) {
assert(l.length == w.length && l.length == p.length);
if (l.length == 0) return Integer.MIN_VALUE;
return maxValueRecur(l, w, p, L, W, 0);
}
public static int maxValueRecur(int[] l, int[] w, int[] p, int L, int W,
int value) {
int len = l.length;
assert(l.length == w.length && l.length == p.length);
if (len == 1) {
if (l[0] > L || w[0]... 阅读全帖
l*****s
发帖数: 279
42
来自主题: JobHunting版 - 发个刚面完的rocket fuel的面经吧
2nd question
public static int maxValue(int[] l, int[] w, int[] p, int L, int W) {
assert(l.length == w.length && l.length == p.length);
if (l.length == 0) return Integer.MIN_VALUE;
return maxValueRecur(l, w, p, L, W, 0);
}
public static int maxValueRecur(int[] l, int[] w, int[] p, int L, int W,
int value) {
int len = l.length;
assert(l.length == w.length && l.length == p.length);
if (len == 1) {
if (l[0] > L || w[0]... 阅读全帖
h**o
发帖数: 548
43
来自主题: JobHunting版 - cc150 - design OOP parking lot problem
int test_parking_lots(){
ParkingLots p(3,40,50);//floor, row, column
Car car1 = Car();
Motor motor1 = Motor();
Bus bus1 = Bus();
int car1_id, motor1_id, bus1_id;
bool res;
res = p.park(car1, car1_id);
res = p.park(motor1, motor1_id);
res = p.park(bus1, bus1_id);
res = p.unpark(car1, car1_id);
return 0;
}
typedef enum VehicleType_e{
MOTOR_VEHICLE_TYPE = 0,
CAR_VEHICLE_TYPE,
BUS_VEHICLE_TYPE,
MAX_VEHICLE_TYPE_SIZE
} VehicleType;
typedef e... 阅读全帖
y***n
发帖数: 1594
44
来自主题: JobHunting版 - hasPathSum
LeetCode says that I am failing the test case
Input: {1,2}, 0
Output: true
Expected: false
However, I can pass it with a console assert.
bool getPathSum(TreeNode *root, int pathSum, int sum){
pathSum +=root->val;
if(NULL==root->left && NULL==root->right){//get to the leaf
return pathSum==sum;
}

bool left, right;
if(root->left){
left=getPathSum(root->left, pathSum, sum);
}

if(root->right){
... 阅读全帖
l*n
发帖数: 529
45
来自主题: JobHunting版 - G电面的一个题
每个时间段还有找最大的要求,应该会比O(nolgn)要稍大。
class Record {
int start;
int end;
int vol;
public Record(int s, int e, int vol) {
assert (s < e);
this.start = s;
this.end = e;
this.vol = vol;
}
}
class Point {
int time;
boolean isStart;
Record rec;
public Point(int time, boolean isStart, Record rec) {
this.time = time;
this.isStart = isStart;
this.rec = rec;
}
}
ArrayList loudestVol(Record[] varr) {
assert (v... 阅读全帖
l*n
发帖数: 529
46
来自主题: JobHunting版 - G电面的一个题
class Record {
int start;
int end;
int vol;
public Record(int s, int e, int vol) {
assert (s < e);
this.start = s;
this.end = e;
this.vol = vol;
}
}
class Point {
int time;
boolean isStart;
Record rec;
public Point(int time, boolean isStart, Record rec) {
this.time = time;
this.isStart = isStart;
this.rec = rec;
}
}
ArrayList loudestVol(Record[] varr) {
assert (varr != null);
ArrayList阅读全帖
s******d
发帖数: 424
47
来自主题: JobHunting版 - 一道onsite面试题
5个里面选三个
#include
#include
#include
#include
#include
int main()
{
int values[] = { 1, 2, 3, 4, 5};
int elements[] = { 1, 1, 1, 0, 0};
const size_t N = sizeof(elements)/sizeof(elements[0]);
assert(N == sizeof(values)/sizeof(values[0]));
std::vector selectors(elements, elements + N);
int count = 0;
do
{
std::cout << ++count << ": ";
for (size_t i = 0; i < selectors.size(); ++i)
{
if (selectors[i])
{
... 阅读全帖
b******p
发帖数: 49
48
来自主题: JobHunting版 - leetcode上的Sort List那道题
我来贴个CPP的(注意:以下有乱七八糟的code……)
合并排序确实比较好用,我还在写第一遍leetcode,代码风格也比较乱,带了很多
debug code,还带了测试用例,试了9次才通过…
有一些多边界条件需要判断的。我的方法是加很多debug,或加很多assertion(我做别
的题里面经常用assertion……不知道是不是好习惯)
============================
#include
#include
using namespace std;
/*
Merge sort ?
*/
// Start: 22:40
// End: 23:45 用了一个小时
//Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
#define TOMMYDBG
class Solution {
public:
... 阅读全帖
f***s
发帖数: 112
49
来自主题: JobHunting版 - airBnb电面面经
这题45分钟弄不出来
import java.util.HashMap;
import java.util.Map;
public class KDistance {
static class Node{
char c;
boolean isLeaf;
Node p;
Map children;
String word;
int no;
int depth;
static int counter = 0;
static Map nodemap = new HashMap();
public Node(Node parent, char c){
children = new HashMap();
p = parent;
if(... 阅读全帖
f***s
发帖数: 112
50
来自主题: JobHunting版 - airBnb电面面经
这题45分钟弄不出来
import java.util.HashMap;
import java.util.Map;
public class KDistance {
static class Node{
char c;
boolean isLeaf;
Node p;
Map children;
String word;
int no;
int depth;
static int counter = 0;
static Map nodemap = new HashMap();
public Node(Node parent, char c){
children = new HashMap();
p = parent;
if(... 阅读全帖
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)