今日LeetCode--28,38
来源:互联网 发布:淘宝退货率最低的产品 编辑:程序博客网 时间:2024/05/20 07:33
28. Implement strStr()
代码如下:
class Solution {
public:
int strStr(string haystack, string needle) {
int offset;
int h = haystack.length();
int n = needle.length();
int i;
if(n==0)
return 0;
if(h==0)
return -1;
for(i = 0; i< h-n+1; i++){
for(offset = 0; offset < n; ){
if(haystack[i+offset]==needle[offset])
offset++;
else
break;
}
if(offset == n)
return i;
}
if(offset == n)
return i;
else
return -1;
}
};
还可以用flag;
还可以用KMP, 以后有时间再尝试;
//for(offset = 0; offset < n; ){
曾出错;
38. count and say
第一次尝试递归,有时间尝试一下非递归。
代码如下:
string countAndSay(int n) {
if(n == 1)
return "1";
if(n == 2)
return "11";
string res = "";
//int i;
string res_tmp = countAndSay(n-1);
int count = 1;
int length = res_tmp.length();
for(int i = 1; i < length ; i++){
if(res_tmp[i] == res_tmp[i-1]){
count++;
}
else{
res.push_back('0'+count);
res.push_back(res_tmp[i-1]);
count = 1;
}
if(i == length-1){
res.push_back('0'+count);
res.push_back(res_tmp[i]);
}
}
return res;
}
- 今日LeetCode--28,38
- 今日LeetCode--13 14
- 今日LeetCode--21
- 今日视点 2010.5.28
- 今日
- 今日
- 今日
- 今日
- 今日
- 今日
- 今日
- 今日兩點4/28
- [今日所学] 第5天 LeetCode Power of Three
- 今日e语-6月28日
- ACM今日学习总结(2015.07.28)
- 今日事,今日毕
- 今日事今日毕
- 今日事今日毕
- React中state与props介绍与比较
- Mac下删除mysql
- MyBatis 模糊查询-拼接SQL语句
- 读 zepto 源码之工具函数
- 软件测试的分类——按测试模式来分类
- 今日LeetCode--28,38
- Java排序8大基本算法
- C++笔记:函数重载、覆盖、隐藏
- 一些技巧TD
- hibernate 7种映射关系
- 【版本升级】MyEclipse 2017 CI 5发布(附下载)
- 用vue实现模态框组件
- 好记性不如烂笔头------使用Gradle构建不同的应用名和图标
- Ubuntu 16.04 安装Vmware Workstation12