LeetCode : Implement strStr() [java]
2016-03-09 00:35
483 查看
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
思路:简单遍历查找即可。
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
思路:简单遍历查找即可。
public class Solution { public int strStr(String haystack, String needle) { if (needle == null || haystack == null || needle.length() == 0) { return 0; } if (haystack.length() < needle.length()) { return -1; } for (int i = 0; i < haystack.length(); i++) { if (i + needle.length() > haystack.length()) return -1; int m = i; for (int j = 0; j < needle.length(); j++) { if (needle.charAt(j) == haystack.charAt(m)) { if (j == needle.length() - 1) return i; m++; } else { break; } } } return -1; } }
相关文章推荐
- Java菜鸟总结
- java io类 和servlet类 的UML图表示
- 自学 java 笔记 day11(多线程)
- java中的正则表达式
- Java的值传递
- Java 书籍
- java生成日期列表datelist
- Java动态代理的实现
- Spring学习笔记十三---动态代理
- CloudStack API 请求生成步骤
- JAVA编程经验
- Java并发编程:线程池的使用
- 原始方式:spring四种依赖注入方式 |注解方式:Spring零配置通过注解实现Bean依赖注入
- Spring依赖注入:注解注入总结
- struts常用4种遍历
- Java虚拟机 运行时数据区
- Java 的clone
- Java多线程常见的问题总结
- hibernate4整合spring4
- LeetCode : Remove Element [java]