【Leetcode】:28. Implement strStr()问题 in JAVA
2016-06-22 20:32
666 查看
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Subscribe to see which companies asked this question
题目要求求字符串needle第一次在haystack中出现的位置
------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Solution {
public int strStr(String haystack, String needle) {
if (needle.length() == 0) {
return 0;
}
if (haystack.length() == 0) {
return -1;
}
int result = -1;
point:
for (int index = 0; index <= haystack.length() - needle.length(); index++){
for(int i = 0; i < needle.length(); i++) {
if (haystack.charAt(index + i) != needle.charAt(i)){
break;
}
if (i == needle.length() - 1) {
result = index;
break point;
}
}
}
return result;
}
}
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Subscribe to see which companies asked this question
题目要求求字符串needle第一次在haystack中出现的位置
------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Solution {
public int strStr(String haystack, String needle) {
if (needle.length() == 0) {
return 0;
}
if (haystack.length() == 0) {
return -1;
}
int result = -1;
point:
for (int index = 0; index <= haystack.length() - needle.length(); index++){
for(int i = 0; i < needle.length(); i++) {
if (haystack.charAt(index + i) != needle.charAt(i)){
break;
}
if (i == needle.length() - 1) {
result = index;
break point;
}
}
}
return result;
}
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android之获取手机上的图片和视频缩略图thumbnails
- android string.xml文件中的整型和string型代替
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序