由买买提看人间百态

topics

全部话题 - 话题: namespaces
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
b*******y
发帖数: 32
1
来自主题: JobHunting版 - 脸书电话面试第一轮代码题面筋
第一个google了一下,可以用Babynonian, O(n^2).
==================
#include
#include
using namespace std;
double Babylonian(double value) {
double root = value/2.0;
int steps = 100;
for (int i = 0; i < steps; i++){
root = 0.5*(root + value / root);
}
return root;
}
void compSqrt(double value) {
cout<<"Input: "< < }
int main()
{
compSqrt(5);
compSqrt(
D***h
发帖数: 183
2
来自主题: JobHunting版 - Amazon onsite面试的惨痛经历
how about this. I test it seems ok.
#include
#include
using namespace std;
void num2alpha(int n){
vector vec;
vector::reverse_iterator it;
int i=n;
if(n==0)
vec.push_back('a');
else{
while(i>0){
vec.push_back('a'+n%26);
i=n/26;
n = n/26 -1;

}
}
for(it=vec.rbegin();it!=vec.rend();it++)
cout<<*it;
cout<
}
b*******y
发帖数: 32
3
来自主题: JobHunting版 - Amazon onsite面试的惨痛经历
第三题当场很难作对啊,amazon这种不提前做真题看来拿不到Offer了。
我贴一个,大家给看看。
====================
#include
#include
using namespace std;
void num2str(int num) {
vector str;
int value;
bool first = true;
while(num != -1) {
value = num % 26;
str.push_back('a'+value);
num = num / 26 -1;
}
vector::reverse_iterator rit;
for ( rit=str.rbegin() ; rit < str.rend(); ++rit )
cout << *rit;
cout< }
int main() {
cout<<"d "; num2
P***t
发帖数: 1006
4
来自主题: JobHunting版 - Amazon onsite面试的惨痛经历
The 3rd one isn't any traditional number.
If you still use 10 to rewrite those number, you will find that:
0,
1,
...
9,
00,
01,
...
99,
000,
....
999
for digits=1, there will be 10 numbers. For digits=2, there will additional
100.
Therefore you first need to find how many digits you need to use to rewrite
the given number. it will be in some range of:
26^N < X < 26 ^ (N+1)
The you do X-26^N, and fill the rest digits.
Try this below:
#include
#include
using namespace std;
cons
b*******y
发帖数: 32
5
来自主题: JobHunting版 - Amazon onsite面试的惨痛经历
你的结果和我的一样,
============================
#include
#include
using namespace std;
void num2str(int num) {
vector str;
vector::reverse_iterator rit;
const int step = 3;
int value;
bool first = true;
while(num != -1) {
value = num % step;
str.push_back('a'+value);
num = num / step -1;
}
for (rit = str.rbegin(); rit < str.rend(); ++rit)
cout << *rit;
cout< }
int NumString() {
for (int i = 0;
I*****y
发帖数: 602
6
来自主题: JobHunting版 - Amazon onsite面试的惨痛经历
这道题确实有些trick在里面。昨晚想了很长时间,下面的代码应该没有问题:
#include
#include
using namespace std;
deque n2s(int n)
{
std::deque s;
int j = n % 26 ;
n /=26;
s.push_front('A' + j);
while(n)
{
j = n % 26 ;
s.push_front('A' + (j + 25) % 26); // 这里j=1->a,...,0->z
n = n / 26;
if(j==0) // 这里j=0时需要调整,不然输出序列为YZ,AZA,AZB,...,AZZ,
AAA...
n -= 1;
}
return s;
}
int main()
{
int a = numeric_limits::max();
for(i
w***h
发帖数: 415
7
来自主题: JobHunting版 - Amazon onsite面试的惨痛经历
好象第三题没有什么trick啊. 就是全组合的代数,
26^1 + 26^2 + 26^3 ...
各项对应string长度,所以算法很直接:第一步确定string长度N,第二步在给定N的集
合内把数字转成string,就是在26x26..x26的"矩阵"里计算每维的整数位置坐标(可以
取0, ..., 25)
///
/// num2str.cpp
///
#include
#include
const static char a = 'a';
const static int b = 26;
namespace MyNum2Str
{

long int pow(int a, int b)
{
long int a2b = 1;
for ( int i = 0; i < b; i++ )
a2b *= a;
return a2b;
}
int num2strlen ( int M )
{
// 第一步确定string长度
int N = 1, sum = 0;
d**e
发帖数: 6098
8
来自主题: JobHunting版 - 这道题好像有点难
careercup 150 上面好像有道类似的,但就是十进制加法。
我下面的解法结果的最高位有可能为0,比如101 + 1结果会出现0110
#include
#include
using namespace std;
char badd(int x, int y, int & carry)
{
int z;
int v = x + y + carry;
if(v == 0)
{
z = 0;
carry = 0;
}
else if(v == 1)
{
z = 1;
carry = 0;
}
else if(v == 2)
{
z = 0;
carry = 1;
}
else
{
z = 1;
carry = 1;
}
return '0' + z;
}
char * bstradd(char * a, char * b)
{
char * c = 0;
c**********e
发帖数: 2007
9
来自主题: JobHunting版 - C++ Q59: pointer & c-string (Bloomberg)
#include
using namespace std;
int main()
{
char s[256];
char* p = s;
p = "some new string";
cout << s << endl;
return 1;
}
What is the output?
a) s
b) space
c) Nothing due to error
d) It depends on implementation
c**********e
发帖数: 2007
10
#include
using namespace std;
class A {
public:
A() { cout<<"A_constructor "; f(); }
virtual void f() { cout<<"A_f "; }
};
class B: public A {
public:
B() { cout<<"B_constructor "; f(); }
virtual void f() { cout<<"B_f"; }
};
int main()
{ B b; }
What is the output?
a) A_constructor A_f B_constructor A_f
b) A_constructor A_f B_constructor B_f
c) Cannot call f() in A's constructor
d) None of above.
g*****7
发帖数: 111
11
来自主题: JobHunting版 - 一个实际的排序问题
我不知道怎么在你说的前提下一次完成给vector排序和记录index。可以先记录index再
根据index排序吗?
using namespace std;
class MyCompare
{
public:
MyCompare(const double* array) : d_array(array) {} // doesn't own array
bool operator() (int i, int j)
{
return d_array[i] < d_array[j];
}
private:
const double* d_array;
};
int main()
{
double array[] = {1., 5., 1.5, 3., 0.24, 11., 7.};
const size_t n = sizeof(array)/sizeof(array[0]);
int* indices = new int[n];
for (size_t i = 0; i < n; ++i)
indices[i] = i;
MyCom
c**********e
发帖数: 2007
12
来自主题: JobHunting版 - C++ Q66: reverse a string -- is it efficient
我写的下面的程序是不是很不efficient?
#include
#include
#include
using namespace std;
int main(){
string input = string("reverse_this_string");
int n=input.size();
const char* c_str=new char[n+1];
c_str=input.c_str();
char* c_str2=new char[n+1];
for(int i=0;i cout << string(c_str2) << endl;
return 0;
}
m*****n
发帖数: 2152
13
来自主题: JobHunting版 - C++ Q66: reverse a string -- is it efficient
#include
#include
using namespace std;
int main(){
string input("reverse_this_string");
string::iterator it_first = input.begin();
string::iterator it_last = input.end()-1;
while(it_first < it_last)
swap(*it_first++, *it_last--);
return 0;
}
l*******y
发帖数: 1498
14
来自主题: JobHunting版 - 问个c++题
你看看是什么错误再修改一下不就可以了
#include
#include
using namespace std;
class A
{
private:
string name;
public:
A(string n):name(n){}

friend ostream& operator<< (ostream &out, const A &a);
};
ostream& operator<< (ostream &out, const A &a)
{
out << a.name;
return out;
}
int main()
{
A aa("test");

cout<
return 0;
}
l********n
发帖数: 54
15
来自主题: JobHunting版 - C++问题
用template只能做到这样,不过#type想不出如何打印出来
#include
using namespace std;
template
inline void PrintSize()
{
cout<<"sizeof "<<" is " << sizeof(T)< }
int main(void)
{
PrintSize();
return 1;
}
l********n
发帖数: 54
16
来自主题: JobHunting版 - C++问题
great! Then, following code should work.
#include
#include
using namespace std;
template
inline void PrintSize()
{
cout<<"size of "<< typeid(T).name()<<" is " << sizeof(T)< }
struct A
{
int a;
double b;
};
int main(void)
{
PrintSize();
PrintSize();
PrintSize();
return 1;
}
w****g
发帖数: 206
17
/thinking in C++/第三章最后一个程序,硬是看不懂, 请高手指点一下:

//: C03:FunctionTable.cpp
// Using an array of pointers to functions
#include
using namespace std;
// A macro to define dummy functions:
#define DF(N) void N() { \
cout << "function " #N " called..." << endl; }
DF(a); DF(b); DF(c); DF(d); DF(e); DF(f); DF(g);
//以上是和前一句, 宏定义一起的么? 怎么解释?
void (*func_table[])() = { a, b, c, d, e, f, g };
//以上function定义怎么理解,a,b,...g,是arguments还是statments?
int main() {
... 阅读全帖
c*b
发帖数: 3126
18
来自主题: JobHunting版 - 弱问一个c++编程题
do u know what is a namespace?
a****n
发帖数: 1887
19
来自主题: JobHunting版 - 弱问一个c++编程题
queue在std namespace里面
t*****j
发帖数: 1105
20
来自主题: JobHunting版 - 弱问一个c++编程题
a famous poor c++ knowledge id...
我的蠢问题很多的,希望大家以后不要大惊小怪,很正常,^-^
std里到底包括哪些东西啊,除了IO,感觉是不是一般常用的都在这个namespace里...

, there is
h**6
发帖数: 4160
21
来自主题: JobHunting版 - 弱问一个c++编程题
大家不要冷嘲热讽没,我再说一点:
如果不加namespace那句话,可以这样定义队列:
std::queue
有不少人都是使用的这种编程风格。
x***y
发帖数: 633
22
来自主题: JobHunting版 - 问个bb的面试题
1. exceptions thrown by the variables or operations at globe, file or
namespace scope.
2. exceptions thrown in the process of stack unwinding in the try block,
which is, some exceptions are thrown in the destructor of a variable.
s*****n
发帖数: 5488
23
来自主题: JobHunting版 - 一道二叉树的老题
#include
use namespace std;
void k-bst(Node * root, int k)
{
static int i = 0;
if ( root )
{
K-BST(root->left, k);
if (++i == k)
cout << root->data;
k-bst(root->right,k);
}
}
B***n
发帖数: 84
24
来自主题: JobHunting版 - c++ 程序一问
下面程序的输出结果是什么?为什么不是期望的结果 10个A?
#include
#include
using namespace std;
void test( char *p )
{
p = new char[10];
memset(p, 'A', 10);
}
int main()
{
char *k;
test(k);
cout << k << endl;
return 0;
}
i******s
发帖数: 301
25
来自主题: JobHunting版 - 问两道google题
你的解法应该是对的,基于这个写了如下C++代码
/*
* Author: Shengzhe Yao
* Date: 08 Nov 2010
*/
#include
#include
#include
#include
using namespace std;
// T(n) = \sum_{i=1}^{n-1} (T(i) * H(i+1))
int findAllStr(const set &dict, char *str,
int end, int *lookup_table) {
if (lookup_table[end] > 0)
return lookup_table[end];
int total = 0;
for (int i=1; i < end; ++i) {
char *substr = (str+i+1);

if (dict.find(substr) != dict.end()) {
char tmp =... 阅读全帖
V*****i
发帖数: 92
26
来自主题: JobHunting版 - 问两道google题
There is no need to pass Dictionary and lookup table, use global variable or
use class. My solution as follows:
#include
#include
#include
#include
using namespace std;
// T(n) = \sum_{i=1}^{n-1} (T(i) * H(i+1))
const int MAXSIZE = 100;
class find_str {
private:
set dict;
int lookup_table[MAXSIZE];
public:
find_str(set &d) {
dict = d;
for (int i=0; i ... 阅读全帖
A*H
发帖数: 127
27
来自主题: JobHunting版 - 问两道google题
#q2
#include
#include
using namespace std;
int partition_count(map dict, string words) {
int count = 0;
if (dict[words])
count++;

for (int i=1, n=words.size(); i if (dict[words.substr(0, i)])
count += partition_count(dict, words.substr(i, n-i));
}
return count;
}
int main() {
map dict;
dict["aaa"] = true;
dict["bb"] = true;
dict["cc"] = true;
dict["aaabb"] = true;
dict["bbcc"] = true;
string words = ... 阅读全帖
l**********3
发帖数: 161
28
第一题可不可以这么做,O(N)的方法,请大虾指教。
#include
using namespace std;
typedef struct best_days {
int buy;
int sell;
} best_days_t;
best_days_t get_best_days(double* prices, int days){
best_days_t best;
best.buy = best.sell = -1;

int minday = 0;
double dmax = 0.0f;
double minprice = prices[0];

for (int i=1; i if (prices[i] - minprice > dmax){
dmax = prices[i] - minprice;
best.sell = i;
best.buy = minda... 阅读全帖
L*******e
发帖数: 114
29
来自主题: JobHunting版 - Two old questions
This is a small program I wrote for DP solution.
/*
* DP: B[j] = product[0,j-1] * product[j+1, n-1]
* product[1,j] = product[1,j-1] * A[j]
* product[j,n-1] = product[j+1,n-1] * A[j]
*/
#include
using namespace std;
void convertArray( const vector& in, vector& out )
{
vector::size_type sz = in.size();
vector< vector > product(sz, vector(sz));
product[0][0] = in[0];
product[sz-1][sz-1] = in[sz-1];
// calcuate product[0,j] = product[0... 阅读全帖
y*****a
发帖数: 221
30
来自主题: JobHunting版 - c++需要用hashtable怎么办?
自己回一下这个,给平时用不到hash_set的人,在GCC
#include
using namespace __gnu_cxx;
c*****e
发帖数: 74
31
/*
How can we generate all possibilities on braces ?
N value has given to us and we have to generate all possibilities.
**Examples:**
1) if N == 1, then only one possibility () .
2) if N==2, then possibilities are (()), ()()
3) if N==3, then possibilities are ((())), (())(),()()(), ()(()) ...
Note: left and right braces should match. I mean )( is INVALID for the N==1
How can we solve this problem by using recurrence approach ?
*/
#include
using namespace std;
void PrintStack(deque&... 阅读全帖
b*****e
发帖数: 474
32
很不错了。细节可以再推敲一下,算法本身(基于DFS的递归)没问题,C++
的掌握也不错。当个 developer 够了哈。
我的版本,给你参考一下吧:
#include
#include // i like arrays
#include // for atoi()
using namespace std;
void print_parens(int n) {
static vector s; // i just use static vars to save parameter
static numLeft = 0; // passing
static numRight = 0;
if (n<0) return;
if (n==0) {
for( int i=0; i // save the extra recursion when there are n '('s already -
// so ... 阅读全帖
b*****e
发帖数: 474
33
来自主题: JobHunting版 - 讨论一道面试题
前提和 flydog MM 的一样, 用C++:
#include
#include
#include
using namespace std;
int has_conflict(vector >& s1, int i1,
vector >& s2, int i2) {
if ( s1[i1].first <= s2[i2].first && s2[i2].first < s1[i1].second
|| s2[i2].first <= s1[i1].first && s1[i1].first < s2[i2].second )
return 1;
return 0;
}
void print_conflict(vector >& s1, int i1,
vector >& s2, int i2) {
cout << "confli... 阅读全帖
i**********e
发帖数: 1145
34
来自主题: JobHunting版 - fb 面经
For number 1, the problem of proving the statement sounds MORE interesting..
. hmm..
My solution using character pointer manipulation.
#include
using namespace std;
void printCharCounts(const char *in, char out[]) {
int count = 1;
char *pOut = out;
const char *pIn = in;
while (*pIn) {
if (*pIn == *(pIn+1)) {
count++;
} else {
*pOut++ = '0' + count;
*pOut++ = *pIn;
count = 1;
}
pIn++;
}
*pOut = '\0';
}
int main() {
char str[100] = "... 阅读全帖
i**********e
发帖数: 1145
35
来自主题: JobHunting版 - 问道很难的面世题
其实我之前的帖子有一些错误.
例如,我的假设是integer是错的.
liveInBoston 也犯了这个错误,题目说 N, D, C, F is real numbers.
另外,我也漏了一些细节部分.
例如,忘了处理当中间没油了的情况.
但是基本思路还是对的.
我已经把我解这道题的详细思路 记录在这里:
http://www.ihas1337code.com/2011/01/nuts-in-oasis-interview-que
Below is my output for the sample test cases:
N: 1 D: 1 C: 1 F: 1 maxNuts: 0
N: 1 D: 2 C: 1 F: 1 maxNuts: 0
N: 1 D: 0.25 C: 1 F: 1 maxNuts: 0.75
N: 2 D: 1 C: 1 F: 1 maxNuts: 0.333333
N: ... 阅读全帖
i**********e
发帖数: 1145
36
来自主题: JobHunting版 - Facebook Interview Questions
你这方法我想了想,应该是不行的。
试想想,假设有重复的数字怎么处理呢?
[3, 5, 2], 5
hash[5] = pointer that point to node with value 5.
sliding window move to the right 1 step.
3, [5, 2, 5]
Now, you have two 5's. If you choose hash[5] to point to the first 5, which
will be deleted next, so now hash[5] points to nothing!
这问题最优解好像是 O(N),大家再努力想想吧。
(以下的解法是利用 hash table 来数每一个数字出现的次数,不是最优解,复杂度为
O(N lg W),W = sliding window 的长度
#include
#include
#include
#include
#include
using namespace s... 阅读全帖
i**********e
发帖数: 1145
37
来自主题: JobHunting版 - 问道 facebook 面试题
不是大侠,贴一贴我的代码,代码如果不好看 请多多包涵
恐怕只能 handle 以上的 sample test case。
基本思路就是递归,如果有更复杂的情况可以再慢慢改进。
#include
#include
#include
using namespace std;
const int TAB_SPACE = 4;
void outputJSon(istream &in, int indentLevel) {
bool firstChar = true;
bool inBracket = false;
while (in) {
char c = in.get();
if (firstChar) {
cout << endl << setw(indentLevel) << c;
firstChar = false;
}
else {
cout << c;
}
if (c == '{') {
outputJSon(in... 阅读全帖
l****y
发帖数: 44
38
来自主题: JobHunting版 - bloomberg 电面面经
刚放下电话就来贡献面经了。几乎全是概念题,前人的面经里都出现过的,没碰上什么
新题。
online assessment 选的C,3天后就收到电面通知了。
电面约的11点,他10:30就打过来了。我说你咋提前了?他说那我半小时后再打也行。
我说那就现在吧,给我几秒钟
setup。面试的应该是个白人哥哥,比较nice。先问了简历上的project,你是什么role
,哪个最interesting,有给什么公
司做的没。
然后开始 technical 问题。最熟悉什么语言,我说 C and C++. 问C 和 C++有啥
不同。OO有什么key components。解
释 Inheritance, Polymorphism, Singleton, 什么时候用。C++ 和 Java 有啥不同。
Multiple Inheritance 怎么实现。
Pass by value, pass by reference. Error Handling, 怎么用。Namespace 什么用途
. 怎么比较俩个object相同。Heap
和 Stack (memory). Binary tr... 阅读全帖
s*******f
发帖数: 1114
39
using System;
using System.Collections.Generic;
using System.Text;
namespace MSC
{
class Program
{
//“bool F(string s, char ch1, char
ch2, int n)”
//The function determines whether
or not there exists an occurrence of
//ch1 and ch2 separated by a
distance of no more than n in the given
//string s, where s is ascii and
user entered (your function needs
//to work on any input.) The
function must be efficient and
//**optimized** for both space... 阅读全帖
g*********s
发帖数: 1782
40
来自主题: JobHunting版 - 一道M$算法题。

?
using namespace std;
vector find_duplicate_idx(const vector& A)
{
unsorted_map X;
vector idx;
for ( int i = 0; i < A.size(); ++ i ) {
unsorted_map::iterator it = X.find(A[i]);
if ( it != X.end() ) {
idx.push_back((*it).second);
break;
}
else X[A[i]] = i;
}
return idx;
}
S**I
发帖数: 15689
41
来自主题: JobHunting版 - 一道M$算法题。
make some changes on your code, a little more efficient:
using namespace std;
list find_duplicate_idx(const vector& A)
{
hash_map X;
list idx;
for ( int i = 0; i < A.size(); ++ i ) {
hash_map::iterator it = X.find(A[i]);
if ( it != X.end() ) {
idx.push_back(it->second);
idx.push_back(i);
for ( int j = i + 1; j < A.size(); ++j )
if ( A[j] == A[i] )
idx.push_back(j);
return idx;
}
X[A[i]] = i;
}
return idx;
}
c******t
发帖数: 391
42
来自主题: JobHunting版 - 一道M$算法题。
How about this one? Is this O(NlogN) solution?
#include
#include
using namespace std;
void finddup(int arr[], int length){
hash_map hm;
cout< for(int i=0; i hm[arr[i]]++;
}
hash_map::iterator it;
int dup=0;
for(int i=0; i it=hm.find(arr[i]);
if(it->second>1){
dup=it->first;
cout< }
}
cout< }
void main(){... 阅读全帖
s*********l
发帖数: 103
43
来自主题: JobHunting版 - [算法] unsorted array

of
#include
using namespace std;
template
pair unsorted_subarray(const vector &a)
{
T max_record, min_record;
int n = a.size();
assert(n>0);
vector max_record_ind, min_record_ind;
max_record = a[0]; max_record_ind.push_back(0);
min_record = a[n-1]; min_record_ind.push_back(n-1);
for (int i=1;i if (a[i]>=max_record){
max_record = a[i]; max_record_ind.push_back(i);
}
}
for(int i=n-2;i>=0;... 阅读全帖
s**********v
发帖数: 1379
44
来自主题: JobHunting版 - 亚麻题目
I don't know the correct answer... Replied global ( but I said global cannot
have access control) , he said close and
then cut me to next question...555555. I guess add namespace? Need c++guru
to help
i**********e
发帖数: 1145
45
来自主题: 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 ... 阅读全帖
l*****a
发帖数: 559
46
来自主题: JobHunting版 - 问一道无聊的bloomberg电面题
try this one
#include
#include
#include
using namespace std;
int main(){
const volatile int i = 10;

volatile const int* tmp = &i;
int* j = const_cast (&i);
*j = 2;
cout< system("PAUSE");
return 0;
}
the compiler optimization cached i and hid the i's value after the change.
to tell the truth, i do hate bloomberg asking this kind of silly question.
g*****1
发帖数: 998
47
来自主题: JobHunting版 - 请教c++的string vector问题,谢谢!
下面这段代码,在vc express2010里编译通不过,但是我试了g++就可以。
另外,我把string vector改成注解掉的那段里的int vector, vc就可以通过。
请问我这个应该怎么改一下呢?我希望在vc里用。
#include
#include
#include
#include
using namespace std;
int main () {

char *m1[] = {"a","e","c","m"};
vector v1(m1,m1+4);
char *m2[] ={"n"};
vector v2(m2,m2+1);

vector u;
/* int m1[] = {1,2,3,4};
vector v1(m1,m1+4);
int m2[] ={100};
vector v2(m2,m2+1);

vector... 阅读全帖
S**I
发帖数: 15689
48
来自主题: JobHunting版 - 请教c++的string vector问题,谢谢!
error message说的很清楚:string类的operator<没有定义,改成这样子就行了:
#include
#include
#include
#include
#include
using namespace std;
bool comp(const string& s1, const string& s2){
return strcmp(s1.c_str(), s2.c_str()) < 0;
}
int main () {
char *m1[] = {"a","e","c","m"};
vector v1(m1,m1+4);
char *m2[] ={"n"};
vector v2(m2,m2+1);
vector u;
/* int m1[] = {1,2,3,4};
vector v1(m1,m1+4);
int m2[] ={... 阅读全帖
j***y
发帖数: 2074
49
来自主题: JobHunting版 - 大家windows下面用什么写C程序的?

我用你的coding panel调试remove duplicate的程序:
---
#include
//#include
#include
using namespace std;
const int REMOVE = -1000;
int remove_duplicate(int a[],int n){
int i = 0, j = 0;
//unordered_map hash;
map hash;
for(i = 0; i < n; ++i){
if(hash.find(a[i]) != hash.end()) {
a[i] = REMOVE;
} else {
hash[a[i]] = 1;
}
}
j = 0;
i = 0;
while (i < n){
while (a[i] == REMOVE){
i++;
}
a[j] = a[i];
i... 阅读全帖
j***y
发帖数: 2074
50
来自主题: JobHunting版 - 请教几个电面题

谢谢啊。这两天我也在学习hashtable的方法,刚好看到C++里面有个map可以用。
调试了一下你的程序,稍作调整后的code如下:
---
#include
//#include
#include
using namespace std;
const int REMOVE = -1000;
int remove_duplicate(int a[],int n){
int i = 0, j = 0;
//unordered_map hash;
map hash;
for(i = 0; i < n; ++i){
if(hash.find(a[i]) != hash.end()) {
a[i] = REMOVE;
} else {
hash[a[i]] = 1;
}
}
j = 0;
i = 0;
while (i < n){
while (a[i] == REMOVE){
i++;... 阅读全帖
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)