You are on page 1of 13

1. (a[i] > 0, int),.

,.
: 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

3 -> 1 -> 8 -> 4 -> 5 -> 7

void reorder(Node head) {


}

3. single linked list, int,list ().


list.,,.
A: 1 -> 3 -> 4 -> 4 -> 5
B: 2 -> 3 -> 4 -> 6 -> 7 -> 13
: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 13
Node mergeList(Node head1, Node head2) {
}

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) {
}

: ()

1. double -> string (QC: ,:


int -> string , string -> int.)

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

int strcmp( char * str1, char * str2) {


do{
if(*str1 != * str2){
return *str1 < *str2 ? -1 : 1;
}
if(*str1 == 0 || *str2 == 0)break;
++str1;
++str2;
}while(1);
return 0;
}

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

write a function to find the longest increasing subsequence

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){

if(ans == 0 || res[ans-1] < a[i]){ //directly add it


res[ans++] = a[i];
}
else{ // lower_bound is a STL algorithm.
// lower_bound(iterator begin, iterator end, T value)
// [begin, end) must be sorted
// will return the first value inside the [begin, end) that
>= value
* std::lower_bound(res, res+ans, a[i]) = a[i];
}
}
return ans; // the length of the answer
}
//---------------------total number of longest increasing sequence.
1 (1, 2, 3, 4, 5).
seq: 5 4 3
3: (5, or 4, or 3).
seq: 1, 1, 2
2: (1 (1st), 2, or 1 (2nd), 2)

struct Record{
int max_length;

int number; // how many largest sequence


}rec[N];
// 1, 3, 2, 5, 4
// max = 3
1 -> {1, 1}
1, 3, -> {2, 1}
1, 3, 2 -> rec = {2, 1}
1, 3, 2, 5 -> {2, 1}// put 1 before 5
-> {3, 1} // put seq1,3 before 5
-> {3, 2} // add seq 1,2 before 5
1, 3, 2, 5, 4 -> {3, 2}

result = {3, 4}
total_max = 3
total_number = 4

//init rec to {1, 1}


int total_max = 0;
int total_number = 0;
for(int i = 0; i < n; ++i){
for(int j = 0; j < i; ++j){
if(a[i] > a[j]) { // a[j] can put before a[i]
if(rec[j].max_length + 1 > rec[i].max_length){
rec[i].max_length = rec[j].max_length + 1;
rec[i].number = rec[j].number;

}
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.

You might also like