由买买提看人间百态

topics

全部话题 - 话题: find
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
w**z
发帖数: 8232
1
来自主题: JobHunting版 - Find number of subtrees with the same value
我给个题吧。最近面试碰到的
Find number of subtrees in a tree which all nodes have the same value.
public int FindNumberOfSubTreesWithSameValue(Node root)
Note:
The leaf node itself is a subtree.
5
1 2
1 3
1
answer is 4
C***U
发帖数: 2406
2
#include
#include
#include
#define MAX 10000
int main(int argc, char *argv[]){
std::vector numbers1;
std::vector numbers2;
int k = atoi(argv[3]);
if(argc < 4)
std::cout << "we need 2 files and a number." << std::endl;
std::cout << "we need find " << k << "th number" << std::endl;
std::cout << "reading first array of numbers ..." << std::endl;
std::ifstream f1(argv[1]);
if(!f1){
std::cout << "cannot open... 阅读全帖
m****r
发帖数: 141
3
thanks,
How to find the oldest element in the heaps ?
c**********e
发帖数: 2007
4
My solution: Suppose the window width is L.
Use two heaps, one min-heap and one max-heap, and an array Node* arr[L],
which are pointers to the nodes.
Each node of the heap is a structure. It not only includes the data, a left
pointer, and a right pointer, but also an integer. The integer is the
location of the node in the array.
When we delete an value from the window, we know its location i (0 <= i < L)
. By arr[i], we find the node in either heap, and we delete it. When we
delete the node, s... 阅读全帖
b**y
发帖数: 13
5
求教各位大拿,这道面试题怎么解
Find Longest Word Made of Other Words: Write a program that reads a file
containing a sorted list of words (one word per line, no spaces, all lower
case), then identifies the longest word in the file that can be constructed
by concatenating copies of shorter words also found in the file.
给你一个词典,找出长度最长的复合单词,怎么解呢,先谢谢了
b**y
发帖数: 13
6
求教各位大拿,这道面试题怎么解
Find Longest Word Made of Other Words: Write a program that reads a file
containing a sorted list of words (one word per line, no spaces, all lower
case), then identifies the longest word in the file that can be constructed
by concatenating copies of shorter words also found in the file.
给你一个词典,找出长度最长的复合单词,怎么解呢,先谢谢了
m******s
发帖数: 204
7
原题解法CC150
1. Sort the array by size, putting the longest word at the front
2. For each word, split it in all possible ways. That is, for “test”,
split it into {“t”, “est”}, {“te”, “st”} and {“tes”, “t”}.
3. Then, for each pairing, check if the first half and the second both exist
elsewhere in the array.
4. “Short circuit” by returning the first string we find that fits
condition #3.
原题解法似乎不能涵盖所有情况:
aa aabbcc bb bbc cc
1. Sort: (aabbcc, bbc, aa, bb, cc)
2. 把aabbcc拆分成两部分, 下面是所有的情况:
(a abbcc) (aa b... 阅读全帖
c**********e
发帖数: 2007
8
Design an algorithm to find the kth number such that the only prime factors
are 3, 5 and 7.
w****x
发帖数: 2483
9
迭代法:
//Find the nth number which is composed by factor 2, 3 and 5
void PrintSerials(int n)
{
assert(n > 0);
vector vec;
vec.push_back(1);
int nI2 = 0;
int nI3 = 0;
int nI5 = 0;
int nCount = 1;
while(nCount < n)
{
int nTmp2 = vec[nI2]*2;
int nTmp3 = vec[nI3]*3;
int nTmp5 = vec[nI5]*5;
int nMin = min(min(nTmp2, nTmp3), nTmp5);
if (vec.back() != nMin)
{
vec.push_back(nMin);
nCount++;
... 阅读全帖
c**********e
发帖数: 2007
10
来自主题: JobHunting版 - How to find the size of an array? Thanks.
It seems there is no way to find the size of array on heap.
c**********e
发帖数: 2007
11
来自主题: JobHunting版 - How to find out the memory layout of a class
How to find out the memory layout of a class without initiation of the class
?
d****o
发帖数: 1055
12
来自主题: JobHunting版 - find the first missing positive integer.
这道题谁能解出来?(注意,这道题题意请理解清楚)
Given an unsorted integer array, find the first missing positive integer.
For example,
Given [1,2,0] return 3,
and [3,4,-1,1] return 2.
and [-3,-4,1,2,6,7] return 3
Your algorithm should run in O(n) time and uses constant space.
y***d
发帖数: 2330
13
来自主题: JobHunting版 - find the first missing positive integer.
find the max, min, sum and the ought-to-be sum if there were nothing missing
pretty dummy solution though
d****o
发帖数: 1055
14
来自主题: JobHunting版 - find the first missing positive integer.
这道题谁能解出来?(注意,这道题题意请理解清楚)
Given an unsorted integer array, find the first missing positive integer.
For example,
Given [1,2,0] return 3,
and [3,4,-1,1] return 2.
and [-3,-4,1,2,6,7] return 3
Your algorithm should run in O(n) time and uses constant space.
y***d
发帖数: 2330
15
来自主题: JobHunting版 - find the first missing positive integer.
find the max, min, sum and the ought-to-be sum if there were nothing missing
pretty dummy solution though
A***o
发帖数: 358
16
来自主题: JobHunting版 - find the first missing positive integer.
O(n) time, O(n) space
int find(vector & input){
vector check;
check.resize(input.size(),0);
for(int i=0;i if(input[i]>0 && input[i]<=input.size())
check[ input[i]-1 ]=input[i];
}
for(int i=0;i if(check[i]==0)
return i+1;
}
return check.size()+1;
}
b*******e
发帖数: 217
17
来自主题: JobHunting版 - find the first missing positive integer.
build a min-heap in o(n).
query the min element.
query the two child ot he min element.
if the three elements not consecutive. find the missing interger already.
Otherwiese, go to the smaller child until the missing integer is found. o(
logn)
complexity = o(n) + o(logn)
t*********r
发帖数: 845
18
来自主题: JobHunting版 - find the first missing positive integer.
新手,欢迎大家拍砖
假设数组有n个大于0的而且不重复的整数,
(1) sum all elements, if sum=n(n+1)/2, then we know its n+1
(2) if not, compare with n/2, we will have two new arrays, one contains
elements <= n/2, one >n/2. Subtract the second one with n/2. find the sum of
first array, if sum=n'(n'+1)/2, the missing element must be in the second
array
(3) split the second one into two by comparing with n/4, and repeat again..
A***o
发帖数: 358
19
来自主题: JobHunting版 - find the first missing positive integer.
O(n) time, O(n) space
int find(vector & input){
vector check;
check.resize(input.size(),0);
for(int i=0;i if(input[i]>0 && input[i]<=input.size())
check[ input[i]-1 ]=input[i];
}
for(int i=0;i if(check[i]==0)
return i+1;
}
return check.size()+1;
}
b*******e
发帖数: 217
20
来自主题: JobHunting版 - find the first missing positive integer.
build a min-heap in o(n).
query the min element.
query the two child ot he min element.
if the three elements not consecutive. find the missing interger already.
Otherwiese, go to the smaller child until the missing integer is found. o(
logn)
complexity = o(n) + o(logn)
t*********r
发帖数: 845
21
来自主题: JobHunting版 - find the first missing positive integer.
新手,欢迎大家拍砖
假设数组有n个大于0的而且不重复的整数,
(1) sum all elements, if sum=n(n+1)/2, then we know its n+1
(2) if not, compare with n/2, we will have two new arrays, one contains
elements <= n/2, one >n/2. Subtract the second one with n/2. find the sum of
first array, if sum=n'(n'+1)/2, the missing element must be in the second
array
(3) split the second one into two by comparing with n/4, and repeat again..
d*******u
发帖数: 186
22
Given a list of points in 2D and a single reference point, find k nearest
neighbors in amazon.com:
http://www.glassdoor.com/Interview/Given-a-list-of-points-in-2D
还有类似的问题,比如以家为中心的方圆5 miles的business names?
Thanks.
t*****e
发帖数: 53
23
来自主题: JobHunting版 - find sum of three number in array
Can we design an algorithm that can find the triplet in an unsorted array (
including both positive and negative numbers) that sum up to a defined
number? Requirement: the runtime should be o(nlogn).
I can think about alot of O(n^2) way. Do you think this problem has a
solution with O(nlogn).
thanks in advance.
a*******y
发帖数: 1040
24
来自主题: JobHunting版 - find index of an element in sorted array
sorted array with repeated elements
for given element, find out its range.
e.g. A A B B B B B C C D D E F G, given B, the output should be [2, 6]
binary search的变种
我感觉这个不是O(logn)啊
code是发现repeat次数的
int binarySearch(char* charset, int start, int end, char target)
{
if (charset == NULL) return 0;
if (start == end)
{
if (charset[start] == target) return 1;
else return 0;
}
int mid = start + (end-start)/2;

int left = 0, right = 0;
left = binarySearch(cha... 阅读全帖
f*******n
发帖数: 12623
25
来自主题: JobHunting版 - find index of an element in sorted array
Functions to find the left and right bounds, both log(n):
// left bound, inclusive
int binarySearch_left(char* arr, int len, char value) {
int low = 0, high = len-1, mid;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= value)
high = mid - 1;
else
low = mid + 1;
}
return low;
}
// right bound, inclusive
int binarySearch_right(char* arr, int len, char value) {
int low = 0, high = len-1, mid;
while (low <= high) {
... 阅读全帖
l****c
发帖数: 782
26
来自主题: JobHunting版 - find index of an element in sorted array

you lost one more step in each function. Think about "ABBC". Your approach
will find 0 as the left edge.
Try to add this before return in:
if (array[low] else return low;
a*******y
发帖数: 1040
27
来自主题: JobHunting版 - Find consecutive repeated string
ok, I got it
use a suffix array, then sort it( remember index when you sort), now
repeated substring must occur in neigbor, compare two by two to find two
they have common prefix, and if the common prefix length equal to their
index difference +1 , that would be the answer
g****y
发帖数: 240
28
贴个python版的
def findk(alist, blist, k):
"""Given two sorted array, find kth minimum element"""
m = len(alist)
n = len(blist)
assert (k <= m + n)

if m == 0:
return blist[k - 1]
elif n == 0:
return alist[k - 1]

if k == 1:
return min(alist[0], blist[0])

# divide k into two even parts as much as possible
ai = min(k // 2, m)
bi = k - ai
if bi > n:
bi = n
ai = k - bi

if alist[ai - 1] == blist[... 阅读全帖
l***i
发帖数: 1309
29
Given an array of distinct integers, a[0] to a[n-1]
a[i] is local maxima if a[i] > a[i-1] and a[i] > a[i+1] provided the
neighbor exists.
Find any local maxima in sublinear time.
l***i
发帖数: 1309
30
The solution will find a[i] in log(n) time in your case, and in general.
l********r
发帖数: 140
31
来自主题: JobHunting版 - Find top K most frequent numbers?
I think I saw this before, but can't recall it.
How to find the top K most frequent numbers from a large number set? (or
from a stream of numbers)
Thanks!
z********i
发帖数: 568
32
150有一题是finding all paths which sum up to a given value.
给出来的解法是O(nlogn)。可是感觉解法中的path没有考虑说有的path,只考虑了从上
到下的path.
For example,
5
3 6
4 9 7 8
3+5+6=14这样的path没有考虑进去。
l********r
发帖数: 140
33
来自主题: JobHunting版 - CS question: find pythagorean triples
Looks like an old question, but can anyone do it better than n*n?
The question is:
given an array of numbers, find all values in them that a*a + b*b = c*c
Standard way is to convert it to 3SUM, which is n*n. But I heard people can
do it better than that.
Thanks.
n****r
发帖数: 120
34
An operation "swap" means removing an element from the array and appending
it at the back of the same array. Find the minimum number of "swaps" needed
to sort that array.
Eg :- 3124
Output: 2 (3124->1243->1234)
z****p
发帖数: 18
35
I guess this problem is not about algorithm. It asks for the number of min
swaps. It's more like a math problem. As long as we give N-K, our job is
done, and we don't have to implement any algorithm.
Think this problem in another way:
-There is a large graph, where each node is a permutation of the given list
of data; there is an edge points from node i to node j if we can move from
permutation-i to permutation-j by using one "swap".
-Now the question is I pick two nodes i and k in the graph, wh... 阅读全帖
m****1
发帖数: 41
36
来自主题: JobHunting版 - HackerRank find string..
这题真心很难。。不知怎么下手~
https://www.hackerrank.com/challenges/find-strings
求教
n*****a
发帖数: 55
37
来自主题: JobHunting版 - find first nonduplicate unicode questions
面一个cloud start up, 问了find first non-duplicated char 的问题。 我先用
vector写了code, interviewer 又问如果不是ASCII , 是Unicode, 该怎么办?改用map
但是他说算法不是O(n). 还有什么别的方法吗?
r*******e
发帖数: 7583
38
来自主题: JobHunting版 - How to find median of a stream of integers ?
理论上是用一个最大堆和一个最小堆,保持元素个数最多相差一
http://stackoverflow.com/questions/10657503/find-running-median
不过这个要求全部data都能放进内存,不实用
实际上应该是根据counting sort原理,分多个bin来统计
p*****2
发帖数: 21240
39
来自主题: JobHunting版 - How to find median of a stream of integers ?
看到这个了
If the variance of the input is statistically distributed (e.g. normal , log
-normal ... etc) then reservoir sampling is a reasonable way of estimating
percentiles/medians from an arbitrarily long stream of numbers.
int n = 0; // Running count of elements observed so far
#define SIZE 10000
int reservoir[SIZE];
while(streamHasData())
{
int x = readNumberFromStream();
if (n < SIZE)
{
reservoir[n++] = x;
}
else
{
int p = random(++n); // Choose a random numb... 阅读全帖
l*****s
发帖数: 774
40
来自主题: JobHunting版 - 请教:find Kth prime number
今天看了这个帖子
http://www.mitbbs.com/article_t/JobHunting/32475925.html
里面有一题就是 find Kth prime number,目前我能想到的方法就是wiki上面的那个方
法,从2开始循环,依次除去2的倍数,3的倍数,5的倍数,等等。然后找到第Kth,这
个时候要一直循环到N,N远大于K。
另外一种方法,就是依次循环找prime,然后再设置一个count计数,这个方法还不如第
一个那。
请问有没有更好的方法?谢谢
n****n
发帖数: 568
41
来自主题: JobHunting版 - 请教:find Kth prime number
but how do we find N to make sure the k-th prime is less than N?
s*****n
发帖数: 839
42
来自主题: JobHunting版 - How to find Nonprofit jobs?
If a person with H4 visa wants to work, does she has to apply to nonprofit
organizations?
(assuming CPT, OPT is no longer an option)
the chance of getting H1b from normal com is very low rihgt?
anyone knows how to find a H1b sponsored job from non profit organization
when holding H4/F2 visa?
Thank you very much in advance.
f******p
发帖数: 173
43
我怎么觉得是O(n^3)呢
http://stackoverflow.com/questions/12278528/find-subset-with-el
里面ElKamina的答案是对的,不过有一处没说清楚,X[i,j] 应该是 the optimum
result of selecting j elements from the first i elements provided that A[j-1
] is included.
贴个代码
public int cal(int[] a, int k)
{
if (k <2 || a == null ||a.length < 2)
return -1;

Arrays.sort(a);

if (k == 2)
return a[a.length-1] -a[0];

int[][] res = new int[a.length + 1][a.length +1];... 阅读全帖
s********u
发帖数: 1109
44
来自主题: JobHunting版 - 算法题:Find the latest version
http://www.careercup.com/question?id=5673258271637504
Find the latest version of released software. For e.g1. 2 and 2.2.. latest
is 2.2. eg2: 3.1 and 3.1.3... latest version is 3.1.3... version is passed
as string in above format.
一道面经,感觉还是蛮有意思的。正好复习了下怎么用stringstream把string转换成
int/double/
下面的回复,一种解法是把3.1.3转换成一个数组{3,1,3},然后对所有数组补0,再依
次从右往左对每一位排序(有点像radix sort)但是这样感觉还是比较繁琐,相当于对
每一位都要做一次快排,也就是O(knlogn)
另一位说用radixsort,就是从右往左,每次做一个bucket sort。这样的好处是,每一
位只要O(n),总共O(kn)。但坏处是,比如其中一位是很大的数,那么开的空间就要很
大,其实t... 阅读全帖
y*********0
发帖数: 406
45
/**
Given a binary tree, find the maximum path sum.
The path may start and end at any node in the tree.
For example:
Given the below binary tree,
1
/ \
2 3
Return 6.
**/
OJ上面的题目。
给的例子,为啥是结果6,而不是4? 不是说path吗?
h****p
发帖数: 87
46
Find the node with given value in binary tree in in-order way,and return it;
PS: the binary tree may include two nodes with the same value.
感觉老写不对
哪位大牛分享下solution?
谢谢
N******t
发帖数: 43
47
来自主题: JobHunting版 - How to find smiley faces in an image
微软的电话面试:how to find smiley faces in an image?
可能有多个smiley faces在一张给定的图上,请问如何找出来?
C*******n
发帖数: 24
48
来自主题: JobHunting版 - Find the Kth smallest element in 2 sorted
Find the Kth smallest element in 2 sorted
 array.  so if you have 2 arrays&#
160;[1, 5, 9, 15, 20, 34] and [2, 6,
 8, 10, 11, 19] and k = 5, the&
#160;answer is 8.  Basically whats the 
5th smallest number in the 2 arrays 
combined.
Any ideas?
w**7
发帖数: 22
49
来自主题: JobHunting版 - Find the Kth smallest element in 2 sorted
find median of two sorted array
k*******r
发帖数: 355
50
Find all words from a dictionary that are Y edit distance away.
这个怎么做,最原始的比较all word pairs 是O(kn^2)复杂度,肯定有更快的吧
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)