LeetCode 28 — Implement strStr()(C++ Java Python)
来源:互联网 发布:知乎 常凯申 真实身份 编辑:程序博客网 时间:2024/06/15 00:59
题目:http://oj.leetcode.com/problems/implement-strstr/
采用暴力方法,注意下标不要越界。(KMP待补充)
Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
题目翻译:
实现strStr()。
返回指向needle在haystack中第一次出现位置的指针,如果needle不是haystack的一部分,则返回null。
采用暴力方法,注意下标不要越界。(KMP待补充)
C++实现1:
class Solution {public: char *strStr(char *haystack, char *needle) {if(*needle == NULL) {return haystack;}char *p1;char *p2 = needle + 1; char *p1_advance = haystack;while(*p2){++p1_advance;++p2; }while(*p1_advance) {p1 = haystack;p2 = needle;while(*p2 && *p1 == *p2){++p1;++p2;}if(*p2 == NULL) {return haystack;}++haystack;++p1_advance;}return NULL; }};
C++实现2:
class Solution {public: char *strStr(char *haystack, char *needle) { std::string str(haystack); std::string target(needle); if(target.empty()) { return haystack; } int len1 = str.length(); int len2 = target.length(); if(len1 < len2) { return NULL; } for(int i = 0; i <= len1 - len2; ++i) { int j = 0; int k = i; while(j < len2 && target[j] == str[k]) { k++; j++; } if(j == len2) { return (char*)str.substr(i).c_str(); } } return NULL; }};Java实现:
public class Solution { public String strStr(String haystack, String needle) {if (needle != null && needle.isEmpty()) {return haystack;}int len1 = haystack.length();int len2 = needle.length();if (len1 < len2) {return null;}for (int i = 0; i <= len1 - len2; ++i) {int j = 0;int k = i;while (j < len2 && needle.charAt(j) == haystack.charAt(k)) {j++;k++;}if (j == len2) {return haystack.substring(i);}}return null; }}Python实现:
class Solution: # @param haystack, a string # @param needle, a string # @return a string or None def strStr(self, haystack, needle): if not needle: return haystack len1 = len(haystack) len2 = len(needle) if len1 < len2: return None for i in range(len1 - len2 + 1): j = 0 k = i while j < len2 and needle[j] == haystack[k]: j += 1 k += 1 if j == len2: return haystack[i:] return None感谢阅读,欢迎评论!
0 0
- LeetCode 28 — Implement strStr()(C++ Java Python)
- LeetCode 28 Implement strStr() (C,C++,Java,Python)
- 【LeetCode 28】Implement strStr() (Python)
- 【leetcode c++】28 Implement strStr()
- [C++]LeetCode 28: Implement strStr() (实现strStr()函数)
- LeetCode 28 : Implement strStr() (Java)
- 【leetcode】28. Implement strStr()(Python & C++)(KMP算法)
- [LeetCode] 028. Implement strStr() (Easy) (C++/Python)
- Leetcode-Implement strStr()-Python
- 【Leetcode】【python】Implement strStr()
- LeetCode--Implement strStr() Python
- Implement strStr() (C,C++,Java,Python)
- LeetCode(28)Implement strStr()
- LeetCode (28)Implement strStr()
- [Leetcode] Implement strStr() (Java)
- [LeetCode][Java] Implement strStr()
- LeetCode : Implement strStr() [java]
- [leetcode-28]implement strstr()(C语言)
- 白话机器学习算法(十八)决策树
- Cloudstack 与 Openstack , 自由软件和开源软件
- 通过ApplicationContextAware获取bean
- 动态规划小结
- gcc 中指定头文件路径及动态链接库路径
- LeetCode 28 — Implement strStr()(C++ Java Python)
- socket阻塞与非阻塞,同步与异步、I/O模型
- android 面试题(1)
- phonegap捕获视频上传
- 中国孩子的土豪梦
- eclipse使用svn上传新的项目
- fprintf中使用stderr
- 平衡二叉树
- Openstack之Cinder服务初探