Professional Documents
Culture Documents
,.
: 1, 2, 3, 4: (0)
).
3, 9, 11, 1, 2 (3)
9, 1, 4, 5(
int process(int[] a) {
}
2. single linked list, .
1 -> 3 -> 4 -> 8 -> 7 -> 5
4. string,anagram. (Anagram,
)
: good, odog -> true, abc, cab -> true, abc, abb -> false
boolean checkAnagram(String a, String b) {
}
5. char,,char.
: 'a', 'b', 'a', 'c', 'x', 'y', 'b', 'z'. 'c'. ( 'c',
char)
char getFirstChar(char[] a) {
}
6. ,L, xL-x
().O(1).
: "AbcEFGXyz", x = 3. :"EFGXyzAbc"
void reverse(String str, int x) {
}
: ()
2. 10101
3. string " i
love
microsoft" -> " i love microsoft"
4. N - 2 1 - N
Google:
1. new malloc.(c/c++,
)
2. (0, 1)
ITfocus~~1:00am
10:00am00:30NTU
8:00am5:00pm50
merge_sort
TJU 08
PHD
http://collabedit.com/
Infrastructure
blabla20-30GG
excuse me
strcmp
abcababcab
difficult to readsorry
strcmp, strncmp, strcpy,
strncpy
random_shuffle
2-3~
//need srand() before calling it
void shuffle(int * array, int length) {
int unshuffled = length;
while(unshuffled > 0){
int rnd = rand() % unshuffled;
swap(array[rnd], array[unshuffled - 1]);
}
}
swap--unshuffled
nk~
best
complexity, average complexity, worst complexitybest
O(n)O(n)
O(n^2)
pivot
3
5middle number
~~~~
~
~~
cnhawk
8am
8amcourse report5
void divide (int num, int den, int & quo, int & rem)
numden num/den = quo
rem
30
ACM3
look_and_sayn
11
211 11
321 21
41211 1211
5111221
6312211
121121
void divide (int num, int den, int & quo, int & rem) {
// try k = 0 .... num like binary search
// test if k * den > num
// find the largest k that k * den < num
// quo = k
// rem = num - den * k;
// O(log2(num))
//if(den == 0) it's error
int low, high;
if(num > 0){
low = -num;
high = num;
}
else{
low = num;
high = -num;
}
int mid;
int result = low;
while(high >= low){
mid = (high + low) >> 1;
if(mid * den > num){
high = mid - 1;
}
else {
result >?= mid;
low = mid + 1;
}
}
quo = result;
rem = num - result * den;
}
/*
* "look" "say"
* 1: 1
* 2: 11
* 3: 21
* 4: 1211
* 5: 111221
* 6: 312211
111111111111
121
*/
void look(char * prev, char * cur);
void look_and_say(int iteration) {
const int N = 1024;
char buf1[N] = "1", buf2[N];
char * cur = buf2, * prev = buf1;
if(iteration == 1){
printf("%s", buf1);
}
for( int i = 2; i <= iteration; ++i){
look(prev, cur);
swap(prev, cur);
}
printf("%s\n", prev); // the result of the iteration
}
void look(char * prev, char * cur){
int pos = 0, int cnt = 0;
char buf[32];
cur[0] = 0; // delete cur
for(int i = 0; ; ++i){
if(prev[i] == prev[pos]){
++cnt;
}
else{
sprintf(buf, "%d%c", cnt, prev[pos]);
strcat(cur, buf); //add it
pos = i;
cnt = 0;
if(prev[i] == 0){
break; // the end
}
}
}
}
1)
2) (1,1,2)2
O(N*logN)DPO(N^2)Lower_bound5
O(N^2)O(N)
a sequence of numbers:
1, 3, 2, 3, 4, 5
actually increasing
answer = 1, 2, 3, 4, 5
-----------------------------------------------
// famous DP problem
/* a :input
n :size of a
res:result
Time complexity : O(N * logN)
*/
1,
1
1,
1,
1,
1,
1,
3, 2, 4, 3, 4, 5 //
3
2
2,
2,
2,
-> 2
4 -> 3
3
3, 4, 5
#include <algorithm>
int LIS(int a[], int n, int res[]){
int ans = 0;
for(int i = 0; i < n; ++i){
struct Record{
int max_length;
result = {3, 4}
total_max = 3
total_number = 4
}
else if(rec[j].max_length + 1 == rec[i].max_length){
rec[i].number += rec[j].number;
}
}
}
if(rec[i].max_length > total_max){
total_number = rec[i].number;
total_max = rec[i].max_length;
}
else if(rec[i].max_length == total_max){
total_number += rec[i].number;
}
}
// then total_number is our result.