1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int lengthOfLastWord(char * s){
if(strlen(s) == 0)
return 0;
int x =strlen(s)-1;
while(s[x]==' '){
x-=1;
if(x<0)
return 0;
}
int j = 0;
while(s[x]!= ' '){
x-=1;
j+=1;
if(x<0)
return j;
}
return j;
}

回头学下KMP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29


int strStr(char * haystack, char * needle){
if(strlen(needle) == 0)
return 0;
if(strlen(haystack) ==0||strlen(haystack) <strlen(needle))
return -1;
int x=0,y=0,sta=0;
while(x <=strlen(haystack)-strlen(needle)){
if(haystack[x] == needle[y])
sta=1;
while(sta){
int a=x+1;
while(a-x<strlen(needle)){
y+=1;
if(haystack[a] == needle[y])
a++;
else{
sta=y=0;
break;
}
}
if(sta)
return x;
}
x++;
}
return -1;
}

菜鸡是我菜鸡

分析通项有:

两式相减,有:

所以

1
2
3
4
5
6
7
8
9
10
11
12
13
int tribonacci(int n){
unsigned int a[10000];
a[0]=0;
a[1]=1;
a[2]=1;
a[3]=2;
int j=3;
while (j < n){
a[j+1]=2*a[j]-a[j-3];
j++;
}
return a[n];
}

70.爬楼梯

1
2
3
4
5
6
7
8
9
10
11
12
int fib(int n){
n+=1;
int a[1000],j=2;
a[0]=0;
a[1]=1;
while (j<n)
{
a[j]=a[j-2]+a[j-1];
j++;
}
return a[n-1];
}

1
2
3
4
5
6
7
8
9
10
11
int climbStairs(int n){
int a[1000],j=2;
a[0]=1;
a[1]=2;
while (j<n)
{
a[j]=a[j-2]+a[j-1];
j++;
}
return a[n-1];
}

忘了动态规划怎么写🙄,只好用从兰神那学的递推模型变成数列求解,,,,

或许通项公式算?逃

0%