58. 最后一个单词的长度 发表于 2021-07-26 更新于 2026-08-08 分类于 algorithm 本文字数: 48 阅读时长 ≈ 1 分钟 123456789101112131415161718int 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
28. 实现 strStr() 发表于 2021-07-26 更新于 2026-08-08 分类于 algorithm 本文字数: 80 阅读时长 ≈ 1 分钟 1234567891011121314151617181920212223242526272829int 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;} 菜鸡是我
1137.第 N 个泰波那契数 发表于 2021-07-26 更新于 2026-08-08 分类于 algorithm 本文字数: 105 阅读时长 ≈ 1 分钟 分析通项有: 两式相减,有: 所以 12345678910111213int 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];}
509. 斐波那契数 发表于 2021-07-25 更新于 2026-08-08 分类于 algorithm 本文字数: 39 阅读时长 ≈ 1 分钟 同70.爬楼梯 123456789101112int 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];}
70.爬楼梯 发表于 2021-07-25 更新于 2026-08-08 分类于 algorithm 本文字数: 68 阅读时长 ≈ 1 分钟 1234567891011int 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];} 忘了动态规划怎么写🙄,只好用从兰神那学的递推模型变成数列求解,,,, 或许通项公式算?逃