28. Implement strStr() [easy]
来源:互联网 发布:eclipse导入jar包源码 编辑:程序博客网 时间:2024/06/05 09:11
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Java
public class Solution { public int strStr(String haystack, String needle) { int l1 = haystack.length(), l2=needle.length(); if(l1<l2) return -1; boolean match = true; for(int i=0; i<=l1-l2; i++){ match = true; for(int j=0; j<l2; j++){ if(haystack.charAt(i+j)!=needle.charAt(j)) {match=false;break;} } if(match) return i; } return -1; }}
Python
class Solution(object): def strStr(self, haystack, needle): """ :type haystack: str :type needle: str :rtype: int """ # try: # return haystack.index(needle) # except: # return -1 for i in range(len(haystack)-len(needle)+1): found=True for j in range(len(needle)): if haystack[i+j]!=needle[j]: found=False break if found: return i return -1
0 0
- 28. Implement strStr() [easy]
- 【Easy】28. Implement strStr()
- 28. Implement strStr() [easy] (Python)
- Easy-题目57:28. Implement strStr()
- Leetcode 28. Implement strStr() (Easy) (cpp)
- Leetcode[easy] - Implement strStr()
- LeetCode-Easy-Java-Implement strStr()
- Leet Code OJ 28. Implement strStr() [Difficulty: Easy]
- [LeetCode] 028. Implement strStr() (Easy) (C++/Python)
- LeetCode-28-Implement strStr()(Sting/穷举)-Easy
- Easy 9 Implement strStr()(28)
- LeetCode OJ 28 Implement strStr() [Easy]
- (算法分析Week6)Implement strStr()[Easy]
- LeetCode-Easy刷题(9) Implement strStr()
- [LeetCode]28.Implement strStr()
- LeetCode --- 28. Implement strStr()
- LeetCode 28.Implement strStr()
- [Leetcode] 28. Implement strStr()
- 10_Flink Streaming jobSubmit
- CString常用函数用法
- 可以复用的checkbox
- Linux线程同步 屏障
- IDE接口驱动程序移植
- 28. Implement strStr() [easy]
- 手机低至或是高至一定温度自动关机
- 什么是进程(一)
- 第六周项目2-带武器的游戏角色
- Mybatis实现数据CURD操作实例--续
- 智能指针shared_ptr 的简单实现
- 图解UNIX的I/O模型
- Majority Element
- Block (三) OC中的函数式编程\链式编程