由买买提看人间百态

topics

全部话题 - 话题: couted
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
c**y
发帖数: 172
1
Something is wrong with "string::iterator p ..."
string accessAString(const string& s) {
string a;
cout << s.c_str() << endl;
for (string::iterator p = s.begin(); p != s.end(); p++) {
cout << *p ;
}
cout << endl;
return a;
}
i**********e
发帖数: 1145
2
来自主题: JobHunting版 - FaceBook面经--第三(最后)部分
我以前贴过,但不知道为什么找不回那个帖子了。
#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, indentLevel+TAB_SPACE);
c = in.get();
assert(c == '... 阅读全帖
c**********e
发帖数: 2007
3
来自主题: JobHunting版 - C++: what is the output? How to interpret it?
#include
using namespace std;
void main()
{
char *c1="xyz";
cout << c1 << endl;
char *c2=0;
cout << c2 << endl;
char *c3="abc";
cout << c3 << endl;
}
b*******y
发帖数: 32
4
来自主题: 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
c**********e
发帖数: 2007
5
来自主题: JobHunting版 - C++ Q56: map & typedef (C29)
typedef std::map MapType;
MapType theMap;
Referring to the code sample above, which one of the following blocks of
code displays the string contents of the entire map theMap?
a) MapType::iterator it = theMap.first();
while(it!= theMap.end()) std::cout << it;
b) MapType::iterator it = theMap.begin();
while(it!= theMap.end()) std::cout << theMap[it];
c) MapType::iterator it = theMap.begin();
while(it!= theMap.end()) std::cout << (*it++).second;
d) MapType::iterator it = theMap
L*******e
发帖数: 114
6
来自主题: JobHunting版 - 砸了面试,发面题
骑驴找驴,砸了面试,潜心修行,明春再战。
//1. what is the output of the following program?
class A{
public:
struct HEADER{
int a_;
double b_;
unsigned int c_;
unsigned char d_;
static int SIZE = 100;
}header;
private:
double k;
};
int main()
{
A a;
cout << "sizeof A: " << sizeof(a) << endl;
cout << "sizeof structure: " << sizeof(a.HEADER) << endl;
}
//2. What is the output of the following program?
void print(int a, int b)
{
cout<<"value a: " << a
h**k
发帖数: 3368
7
来自主题: JobHunting版 - 问个c++题
没有办法作为成员函数。因为对于二元操作符,如果定义为成员函数,缺省的左参数必
须是对象本身。
比如对于operator +,如果我们在一个class A里用成员函数overload它,那么我们只
能支持A+B,不能支持B+A。
在这个例子里,我们希望实现 cout << A,而不是A << cout。那么要不在cout所属类
中对A定义 <<,要不定义<<作为non-member operator。
t*******0
发帖数: 16
8
来自主题: JobHunting版 - 谷歌 电面
1. Find the given node, add the track to stack
2. Find next right node
void findnextnode(BinaryTree *root, int node_val)
{
stack s;
int error = 0;
BinaryTree *cur = root;
BinaryTree *cur = NULL;
//Find match node
while(cur!=NULL)
{
s.push(cur);
if(cur->value==node_val)
break;
else if((cur->valueright!=NULL))
{
... 阅读全帖
b*****e
发帖数: 474
9
很不错了。细节可以再推敲一下,算法本身(基于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 ... 阅读全帖
h**6
发帖数: 4160
10
来自主题: JobHunting版 - 一道位运算题
本题有三种方法可以解答:
1.直接筛选法:
for(int m=0; m<(1< {
for(int x=0; x<=m; x++)
{
if((x^(m-x)) == m)
cout< }
}
对于每一个固定的m,循环次数为 m+1 次,总循环次数为 2^(2n-1)+2^(n-1),复杂度
为 O(2^(2n)) 或 O(4^n)。
2.按位枚举法:
int* one = new int[n];
for(int m=0; m<(1< {
int mask = 1, k = 0;
for(int i=0; i {
if(m & mask)
one[k++] = mask;
mask <<= 1;
}
for(int i=0; i<(1< {
int mask2 = 1, x = 0;
for(int ... 阅读全帖
k***s
发帖数: 277
11
受到wiki上的启发, http://en.wikipedia.org/wiki/Integer_partition
wiki上是用smallest addedn is k, 我改成with m's largest addend k
// k is the maximum number of integer partition;
// m is the num of k of integer partition.
void ip_helper(int n, int k, int m, std::deque sequence) {
if (n < k*m)
return;
if (k < 1)
return;
int i, j, r = n-k*m;
for (i=0; i sequence.push_back(k);
if (r == 0) {
for (i=0; i<(int)sequence.size(); ++i)
std::c... 阅读全帖
i**********e
发帖数: 1145
12
来自主题: 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
13
来自主题: 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... 阅读全帖
c******t
发帖数: 391
14
来自主题: 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(){... 阅读全帖
i**9
发帖数: 351
15
来自主题: JobHunting版 - 新鲜Amazon面经
第三题的code:
int main(){
int a[]={2,4,5,12,6,7,5,8,9,10};
int b[4]={0};
int k=3;
int sum=0;
for(int i=0;i<10;i++){
if(i<=k-1){
b[i]=a[i];
sum+=a[i];
if(i==(k-1)){
cout<< double(sum)/double(k)<<" ";
}
}else{
int t=i%(k+1);
int t2=(t+1)%(k+1);
b[t]=a[i];
sum+=a[i];
sum-=b[t2];
cout<< double(sum)/double(k)<<" ";
}
}
cout<< endl;
}
j***y
发帖数: 2074
16
来自主题: 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
17
来自主题: 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++;... 阅读全帖
l*********y
发帖数: 142
18
来自主题: JobHunting版 - 生物男的Google面经节略版
#include
#include
using namespace std;
int square[1000000];
int best = 4;
int result[4];
void RecurSearch(int count, int target, int start)
{
count ++;
if (count > best) return;
for (int i = start; i >= 1; i --) {
int value = target - square[i];
if (value < 0) continue;
if (square[i] < (target / (best - count))) continue;
result[count - 1] = square[i];
if (value == 0) {
if (count <= best) {
best =... 阅读全帖
h*****g
发帖数: 944
19
来自主题: JobHunting版 - 为什么我在array里用IsOdd 总出错?
g++总是说我的14行有错,为啥?
1 #include
2 #include
3 #include
4 #include
5
6 using namespace std;
7 int main(){
8 int numbers [] ={20, -30, 10, 40, 0};
9 vector five (numbers, numbers+5);
10 int cx = count_if(numbers, numbers+5, bind1st(greater_equal(),
15));
11 cout<<"There are "< 12 int dx = count_if(numbers, numbers+5, bind2nd(greater_equal(),
15));
13 c... 阅读全帖
k*n
发帖数: 150
20
来自主题: JobHunting版 - 求顺时针打印矩阵code

肯定是nxn吗?有可能是nxm吗?后者似乎要麻烦一些
比如这样的
1 2 3 4 5 6
14 15 16 17 18 7
13 12 11 10 9 8
input是什么?如果是一维数组表示法的话,还得转换一下坐标,虽然也不算什么麻烦事
void printMatrix(const int* matrix, int colNum, int rowNum) {
int direction = 0; // 0:>; 1:v; 2:<; 3:^;
int hLen = colNum - 1;
int vLen = rowNum - 1;
int x = 0;
int y = 0;
while (hLen > 0 && vLen > 0) {
printVector(matrix, colNum, x, y, direction, (direction % 2 == 0) ? hLen
: vLen);
direction = (direction + 1) % 4;
if (direction == 0) {
hL... 阅读全帖
m********l
发帖数: 4394
21
来自主题: JobHunting版 - 谁能解释下这道c++的面试题
你试下这个:
class A
{
int x;
public:
A(int x_):x(x_){}
int& getX(){return x;}
};
A a1(11);
A a2(10);
int& y = a1.getX();
cout << y;
y = a2.getX();
cout << y;
y = 20;
cout << y;
看看结果是啥.
y = a2.getX() 是10吗?
is this the case why reference variable can't be reassigned to a new
reference?
d*******l
发帖数: 338
22
来自主题: JobHunting版 - 问个面试题
#include
using namespace std;
int f[110];
int p[50];
int cur = 1;
int ans;
void solve(int n, int m, int s)
{
if(m == 0) {
/* for(int i = 0; i < cur; i++)
cout << p[i]+1 << " ";
cout << endl;
*/ ans += n-1-p[cur-1];
return ;
}
for(int i = s; i < n; i++) {
if(!f[i]) {
p[cur++] = i;
for(int j = cur-1; j >= 0; j--)
if(2*i-p[j] < n)
f[2*i-p[j]]++;
solve(n, m... 阅读全帖
c**********e
发帖数: 2007
23
void reversePrint(Node* head) {
if(head=NULL) {
cout << "Empty List. No node to print." << endl;
return;
}
if(head->next=NULL) {
cout << head->data << endl;
return;
} else {
Node* temp=head;
head=temp->next;
reversePrint(head);
cout << temp->data << endl;
return;
}
}
c**********e
发帖数: 2007
24
发现不定义新节点也没问题。
void reversePrint(Node* head) {
if(head==NULL) {
cout << "Empty List. No node to print." << endl;
return;
}
if(head->next==NULL) {
cout << head->data << endl;
return;
} else {
reversePrint(head->next);
cout << head->data << endl;
return;
}
}
s*****y
发帖数: 897
25
Done's code is better.
void reversePrint(Node * head){
if(!head){
cout << "the end ..." << endl; // print some message here if needed..
.
} else {
reversePrint(head->next);
cout << head->data << endl;
}
}
Suppose you write this function and this function is very very long. Also, m
aybe there are a lot of #ifdef, #endif inside this function to support multi
ple platform. People look at this kind of code often get loss.
Then later on, another engineer jump in and m... 阅读全帖
F**r
发帖数: 84
26
来自主题: JobHunting版 - two questions
int *p1, *p2;
initialize p1, p2;
for(i=0; i std::cout<<1<<'\t';
for(j=1; j<=i; j++) {
std::cout<<(p2[j]=p1[j]+p1[j-1])<<"\t";
}
swap(p1, p2);
cout< }
it tell you the idea, not an solution for a particular problem.

DP
2
i**********e
发帖数: 1145
27
来自主题: JobHunting版 - 新鲜onsite面经
我写的 boggle 游戏算法,DFS + trie.
一秒以内给出所有 5x5 的答案。
#include
#include
#include
#include
#include
#include
#include
using namespace std;
struct Trie {
bool end;
Trie *children[26];
Trie() {
end = false;
memset(children, NULL, sizeof(children));
}
void insert(const char *word) {
const char *s = word;
Trie *p = this;
while (*s) {
int j = *s-'A';
assert(0 <= j && j < 26);
if (!p->childre... 阅读全帖
z**********n
发帖数: 3
28
来自主题: JobHunting版 - facebook的一道题
#include
#include
using namespace std;
class Matrix
{
public:
Matrix(int r, int c)
{
pData = new int[r * c];
_rowNum = r;
_colNum = c;
}
// TODO pData is a pointer, so deep copy struct is needed
// TODO write copy structor and assign operateo
//Matrix(Matrix& m)
//{
//}
//Matrix& operator = (Matrix& m)
//{
//}
void SetNum(int r, int c, int num)
{
pData[r * _colNum + c] = num;
}
const i... 阅读全帖
t*******i
发帖数: 4960
29
我上机试了一下,
std::cout << f(100) << endl;
std::cout << f(10) << endl;
std::cout << f(22010) << endl;
637534208
1342177280
1604976640
for example v = 10. its binary form
becomes 010100000....(28 0s)
c**********e
发帖数: 2007
30
来自主题: JobHunting版 - amazon 2nd phone interview
Quick Selection:
选target个最小的。最终目的是,使a[0], …, a[target-1]为最小的。因为是递归调用子函数,子函数对数组的一部分a[low], …, a[high]操作。
#include
using namespace std;
#define swap(x,y) { int z=x; x=y; y=z; }
int low_n(int a[], int low, int high, int target) {
int pivot; int mid=(low+high)/2;
if(a[low]<=a[mid])
{
if(a[high]<=a[low]) pivot=a[low];
else if(a[high]<=a[mid]) pivot=a[high];
else pivot=a[mid];
} else
{
if(a[high]<=a[mid]) pivot=a[mid];
else if(a[high]<=a[low]) pivot=a[high];
... 阅读全帖
e*****w
发帖数: 144
31
我也发个加法:
#include
using namespace std;
int Add(int a, int b) {
return (b ? Add(a ^ b, (a & b) << 1) : a);
}
int main() {
cout << "Add(3, 4) = " << Add(3, 4) << endl;
cout << "Add(3, -8) = " << Add(3, -8) << endl;
cout << "Add(-8, 40) = " << Add(-8, 40) << endl;
}
e*****w
发帖数: 144
32
我也发个加法:
#include
using namespace std;
int Add(int a, int b) {
return (b ? Add(a ^ b, (a & b) << 1) : a);
}
int main() {
cout << "Add(3, 4) = " << Add(3, 4) << endl;
cout << "Add(3, -8) = " << Add(3, -8) << endl;
cout << "Add(-8, 40) = " << Add(-8, 40) << endl;
}
f*******t
发帖数: 7549
33
来自主题: JobHunting版 - Facebook电面题目
第一题的code:
void printByLevel(Node *root) {
if(root == NULL)
return;

list l;
l.push_back(root);
int length = 1;
int level = 0;
while(length) {
cout << "Level " << level << ": ";
list::iterator it = l.begin();
for(int i = 0; i < length; i++) {
// Print current node
cout << (*it)->val << " ";
if((*it)->left)
l.push_back((*it)->left);
if((*it)->right)
... 阅读全帖
N**********d
发帖数: 9292
34
来自主题: JobHunting版 - 问个缺少逗号的数组赋值问题
【 以下文字转载自 Programming 讨论区 】
发信人: NeedForSpeed (working~~~~~), 信区: Programming
标 题: 问个缺少逗号的数组赋值问题
发信站: BBS 未名空间站 (Sun Jan 15 17:05:58 2012, 美东)
源程序是:
#include
#include
using namespace std;
int main(int argc, char * argv[])
{
std::string m_ColumnName [] =
{
"str1",
"str2"
"last_one"
};
cout << m_ColumnName[0].substr(0,4) << endl;
cout << m_ColumnName[1].substr(0,4) << endl;
cout << ... 阅读全帖
w****x
发帖数: 2483
35
来自主题: JobHunting版 - binary tree的in-order iterator怎么写?
这题作的不下5遍了, 说实话, 第一次做还挺不容易的:
============带parent===========================
void InOrderPrint(NODE* pRoot)
{
assert(pRoot);
NODE* pCur = pRoot;
while (pCur != NULL)
{
while (pCur->pLft != NULL)
{
cout<nVal< pCur = pCur->pLft;
}
cout<nVal< //The ending condition is tricky but simple
while (pCur != NULL)//use "pCur != NULL" rather than "pCur->pParent
!= NULL"
... 阅读全帖
d****o
发帖数: 1055
36
来自主题: JobHunting版 - 问一道c++面试题
#include
using std::cout;
using std::endl;
class thing
{
public:
thing(){cout << "hello, world" << endl;};
};
thing myclass;
int main(void)
{
cout << "Started" << endl;
return 0;
}
s***5
发帖数: 2136
37
来自主题: JobHunting版 - 问题:从电话号码打出所有单词
参加下面一个challenge,就是给定一系列电话号码,把每个号码对应的所有单词都按
字母顺序打印出来,并用,分隔。具体在这:
http://www.codeeval.com/open_challenges/59/
我下面的code提交就说不能通过所有的test cases,或者有warnings。谁大牛指出问题
所在!包子谢。
#include
#include
#include
#include
using namespace std;
void printWord(vector, string, string, bool&);
int main(int argc, char* argv[])
{
string pad1[10] = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs",
"tuv", "wxyz"};
vector pad;
for(int i = 0; i < 10; i+... 阅读全帖
b*********n
发帖数: 1258
38
来自主题: JobHunting版 - Palantir新鲜面经
#include
#include
using namespace std;
void getQuery(int number, int length)
{
int original_len = length;
while(number)
{
int mod = number%10;
if( mod != 0 ) {
int base = number - number%10;
for (int i=0; i cout<< "A";
for (int j=0; j0?(int)log10((
float)base):0; ++j) {
... 阅读全帖
y********a
发帖数: 18
39
来自主题: JobHunting版 - 求教:这个程序为什么不能编译?
#include
#include
#include
#include
using namespace std;
template
void printvec( vector vec ) {
cout << "begin--------------------" << endl;
for ( vector::const_iterator it = vec.begin(); it != vec.end();
it++ ) {
cout << setw(10) << *it << endl;
}
cout << "----------------------end" << endl;
}
int main() {
vector vec1;
for ( int ii = 0; ii < 10; ii++ ) {
vec1.push_back( ii );
... 阅读全帖
l*******b
发帖数: 2586
40
想输出那个序列得要有一个数组记录每个数值在数列中之前的那个值,所以要多开一个
数组记录这个信息吧.
每次用binary search push进去一个之后那个数之前的一个数就是这个. 写得太烂了,
有空重写一下.
class Solution {
private:
vector s;
vector pre;
int push(int k) {
if(s.empty()){
s.push_back(k);
return -1;
}
if(k >= s.back()){
s.push_back(k);
return s[s.size()-2];
}
int l = 0, r = s.size()-1, mid;
while(l < r)
... 阅读全帖
f*********d
发帖数: 140
41
//我上个代码吧, 没有测试过, 有错误或者bug请指出。。。
//这里假设所有节点的值都不一样, 如果存在一样的节点值, 需要一点修改。。。
struct BSTNode {
BSTNode* left;
BSTNode* right;
int val;
}
//print open interval (min_val, max_val) in r
void PrintBST(BSTNode* r, int min_val, int max_val)
{
if(r == NULL) return;
if(r->val <= min_val) PrintBST(r->right, min_val, max_val);
else if(r->val >= max_val) PrintBST(r->left, min_val, max_val);
else {
PrintBST(r->left, min_val, r->val);
cout << r->vale;
PrintBST(r-... 阅读全帖
h********g
发帖数: 496
42
如下。本来想算算尾递归和递归的性能区别。结果发现iterative的算法都不行。
每步的结果都%777,就是为了防止结果溢出。
#include
using namespace std;
int factor(int n){
if(n==0) return 1;
int acc=1;
for(int i=n; i>0; i--)
acc=acc*i%777;
return acc;
}
int factor_recursive(int n){
if(n==1 || n==0) return 1;
return factor_recursive(n-1)*n%777;
}
//g++ doesn't optimize for tail-recursion. The following function stops at
Factor(37).
int factor_tailrecursive(int n, int acc){
if(n==1 || n==0) return acc;
re... 阅读全帖
p****e
发帖数: 3548
43
来自主题: JobHunting版 - flag新题
不知对不,欢迎测试
// Type your C++ code and click the "Run Code" button!
// Your code output will be shown on the left.
// Click on the "Show input" button to enter input data to be read
#include
using namespace std;
int findnotsubarray(vector &rlt, vector &temp, int &X, int a[],
int &length, int start)
{
if(start>=length) return 0;
for(int t=0; t<=X; ++t)
{
int i = start;
for(; i {
if(t==a[i])
{
... 阅读全帖
w*r
发帖数: 72
44
来自主题: JobHunting版 - LinkedIn 面经
two semaphores 就够了吧
#include
#include
#include
class sem {
boost::mutex guard;
int c ;
public:
sem() : c(0) {}
void V ( int n = 1 ) {
boost::mutex::scoped_lock lock(guard);
c = c + n;
}
void P ( int n = 1) {
while ( c < n ) ;
boost::mutex::scoped_lock lock(guard);
c = c - n;
}

};
sem semh;
sem semo;
class H{
public:
void operator()() {
std::cout << "create H thread ... 阅读全帖
w*r
发帖数: 72
45
来自主题: JobHunting版 - LinkedIn 面经
two semaphores 就够了吧
#include
#include
#include
class sem {
boost::mutex guard;
int c ;
public:
sem() : c(0) {}
void V ( int n = 1 ) {
boost::mutex::scoped_lock lock(guard);
c = c + n;
}
void P ( int n = 1) {
while ( c < n ) ;
boost::mutex::scoped_lock lock(guard);
c = c - n;
}

};
sem semh;
sem semo;
class H{
public:
void operator()() {
std::cout << "create H thread ... 阅读全帖
t****t
发帖数: 387
46
这段程序我可以运行没问题
如果再pop一次就crash
template struct node{
node *next;
T data;
node(node* n, T d):next(n),data(d){};
};
template class stack{
public:
node *head;
int ind;
stack(){}
~stack(){}
void push(T d);
T pop();
bool isEmpty();
};
template T stack::pop(){
T d=head->data;
node *s=head;
head=head->next;
delete s;
ind--;
return d;
}
template void stack::push(T d){
if(i... 阅读全帖
j*****y
发帖数: 1071
47
来自主题: JobHunting版 - F面经
写了一个,欢迎测试
#include
#include
#include
#include
#include
using namespace std;
bool isNumber(string s)
{
if(s[0] == '+' || s[0] == '-' || s[0] == '(' || s[0] == ')')
{
return false;
}
return true;
}
int helper(int a, int b, char c)
{
if(c == '+')
{
return a + b;
}
else
{
return a - b;
}
}
bool valid(vector &s)
{
stack v;
stack c;
for(int i = 0; i < s.size(); ++i)
... 阅读全帖
j*****y
发帖数: 1071
48
来自主题: JobHunting版 - F面经
写了一个,欢迎测试
#include
#include
#include
#include
#include
using namespace std;
bool isNumber(string s)
{
if(s[0] == '+' || s[0] == '-' || s[0] == '(' || s[0] == ')')
{
return false;
}
return true;
}
int helper(int a, int b, char c)
{
if(c == '+')
{
return a + b;
}
else
{
return a - b;
}
}
bool valid(vector &s)
{
stack v;
stack c;
for(int i = 0; i < s.size(); ++i)
... 阅读全帖
f****s
发帖数: 74
49
leetcode上给出的思想很好。就是keep一个pre和一个cur,
如果cur是pre的孩子,说明我们正从上到下,如果反过来,说明我们从下往上,就该打
印节点了。
顺便练一个,
void postorder(Node* r)
{
if(!r) return;
stack s;
Node* pre=0;
Node* cur=r;
s.push(r);
while(!s.empty()){
cur=s.top();
if(pre==0||cur==pre->left||cur==pre->right){
if(cur->left) s.push(cur->left);
else if(cur->right) s.push(cur->right);
else{
cout<val;
s.pop();
}
}else{
if(pre=cu... 阅读全帖
r***e
发帖数: 29
50
来自主题: JobHunting版 - BBC 编程风格面试题,求指教
最后个人标准答案
#ifndef _ROMON_HEADER_
#define _ROMON_HEADER_
#define _DEBUG_
#include
#include
//ROMON digits
const std::string ROMON_DIGITS[] = {"I","IV","V","IX", "X","XL","L","XC","C"
,"CD","D","CM","M" };
//ROMON scale
const int ROMON_SCALE[] = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900,
1000};
const int ROMON_MAX = 3999;
const int ROMON_MIN = 1;
class RomanNumeralGenerator
{
public:
virtual std::string generator(int num) = 0;
};
class CRoman : public RomanNumeralGene... 阅读全帖
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)