Skip to content

Commit 6cbaece

Browse files
author
Jie Feng
committed
haha
1 parent 3f5f7d0 commit 6cbaece

File tree

14 files changed

+207
-81
lines changed

14 files changed

+207
-81
lines changed

codeforces/437B

-99.4 KB
Binary file not shown.

codeforces/437B.o

-183 KB
Binary file not shown.

codeforces/438C

-151 KB
Binary file not shown.

codeforces/438C.o

-202 KB
Binary file not shown.

codeforces/temp.o

-63.1 KB
Binary file not shown.

interview/convert-BST-to-doublelinkedlist.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/* Facebook
2+
* http://www.mitbbs.com/article_t/JobHunting/32705761.html
3+
*/
14
#include<iostream>
25

36
using namespace std;

interview/count-inversions.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* http://www.mitbbs.com/article_t/JobHunting/32708791.html
3+
*/
4+
#include<iostream>
5+
#include<cstring>
6+
#include<vector>
7+
using namespace std;
8+
9+
10+
11+
int merge(int A[], int L, int M, int R){
12+
int *B = new int[R-L+1];
13+
int L1 = L, L2 = M+1, i = 0;
14+
int cnt = 0;
15+
while(L1 <= M && L2 <= R){
16+
if (A[L1] <= A[L2]){
17+
B[i++] = A[L1++];
18+
cnt += L2-(M+1);
19+
}else
20+
B[i++] = A[L2++];
21+
}
22+
23+
if (L1 <= M){
24+
cnt += (M+1-L1) * (R-M);
25+
while(L1 <= M){
26+
B[i++] = A[L1++];
27+
}
28+
}else{
29+
while(L2 <= R){
30+
B[i++] = A[L2++];
31+
}
32+
}
33+
34+
memcpy(A+L, B, sizeof(int)*(R-L+1));
35+
return cnt;
36+
}
37+
38+
int count(int A[], int L, int R){
39+
if (L == R) return 0;
40+
int M = (L + R) / 2;
41+
int cnt = count(A, L, M) + count(A, M+1, R);
42+
// cout << L << ' ' << R << ' ' << cnt << endl;
43+
cnt += merge(A, L, M, R);
44+
return cnt;
45+
}
46+
47+
int main(){
48+
int A[] = {5,4,3,2,1};
49+
int n = sizeof(A)/4;
50+
cout << count(A, 0, n-1) << endl;
51+
}

interview/expression.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* given 一个字符串,这个字符串是一个算式,包含加减乘除,没有括号,符号和数
3+
字之间以一个空格格开,比如:“1 + 2 * 4 / 5”,return算式的结果
4+
*/
5+
#include<iostream>
6+
#include<string>
7+
#include<stack>
8+
#include<sstream>
9+
using namespace std;
10+
11+
int expression(string exp){
12+
stringstream ss(exp);
13+
char c, op;
14+
15+
stack<int> nums;
16+
stack<char> ops;
17+
18+
int num, num1;
19+
20+
while(true){
21+
// cout << "New round ....." << endl;
22+
while(!ss.eof() && ss.peek() == ' '){
23+
// cout << ss.peek() << " ";
24+
ss.get(c);
25+
// cout << (int)c << endl;
26+
}
27+
28+
if (ss.eof()) break;
29+
c = ss.peek();
30+
31+
if ('0' <= c && c <= '9'){
32+
ss >> num;
33+
if (!ops.empty()){
34+
op = ops.top();
35+
if (op == '*' || op == '/'){
36+
ops.pop();
37+
num1 = nums.top(); nums.pop();
38+
if (op == '*'){
39+
num = num1 * num;
40+
}else{
41+
num = num1 / num;
42+
}
43+
}
44+
}
45+
nums.push(num);
46+
}else{
47+
ss >> op;
48+
ops.push(c);
49+
}
50+
}
51+
while(!ops.empty()){
52+
num = nums.top(); nums.pop();
53+
num1 = nums.top(); nums.pop();
54+
op = ops.top(); ops.pop();
55+
if (op == '+'){
56+
nums.push(num1+num);
57+
}else{
58+
nums.push(num1-num);
59+
}
60+
}
61+
return nums.top();
62+
63+
64+
}
65+
66+
67+
int main(){
68+
cout << expression("1 + 2 * 4 * 5") << endl;
69+
}
70+

interview/kmp.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include<cstring>
2+
#include<vector>
3+
using namespace std;
4+
class Solution {
5+
public:
6+
char *strStr(char *haystack, char *needle) {
7+
if(*needle == 0){
8+
return haystack;
9+
}
10+
int m = 0;
11+
while(needle[m] != 0) m++;
12+
vector<int> preLen(m+1);
13+
preLen[0] = -1;
14+
15+
for (int L = 0; L < m; L++){
16+
int l = L;
17+
while(preLen[l] != -1 && needle[preLen[l]] != needle[L]) l = preLen[l];
18+
preLen[L+1] = preLen[l]+1;
19+
}
20+
21+
int matchLen = 0;
22+
for (int i = 0; *(haystack+i) != 0; i++){
23+
while(matchLen != -1 && haystack[i] != needle[matchLen]){
24+
matchLen = preLen[matchLen];
25+
}
26+
matchLen++;
27+
if (matchLen == m) return haystack+i-matchLen+1;
28+
}
29+
return NULL;
30+
}
31+
};
32+
33+
34+

interview/makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
target= convert-BST-to-doublelinkedlist
1+
target= expression
22

33
all:
44
#g++ -o $(target) -g $(target).cpp -w

0 commit comments

Comments
 (0)