LintCode StrStr
1. 讨论目标字符串若为空, 则返回-1; 资源字符串若为空, 则返回-1。
2.讨论目标字符串个数为零, 则返回0; 资源字符串个数为零, 则返回-1。
3. 插入旗帜来使第二循环的结束为有条件地返回(为true才返回, 为false则break跳到上循环继续)。
class Solution {
/**
* Returns a index to the first occurrence of target in source,
* or -1 if target is not part of source.
* @param source string to be scanned.
* @param target string containing the sequence of characters to match.
*/
public int strStr(String source, String target) {
//write your code here
if(source == null || target == null) return -1;
if(target.length() == 0) return 0;
if(source.length() == 0) return -1;
for(int i = 0; i < source.length(); i++){
boolean flag = true;
for( int j = 0; j < target.length(); j++){
if(source.charAt(i + j) == target.charAt(j)){
}
else{
flag = false;
break;
}
}
if(flag) return i;
}
return -1;
}
}
LintCode StrStr的更多相关文章
- LintCode 13. Implement strStr()
LintCode 13. Implement strStr() 题目描述 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出 ...
- lintcode:strStr 字符串查找
题目: 字符串查找 字符串查找(又称查找子字符串),是字符串操作中一个很有用的函数.你的任务是实现这个函数. 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source ...
- 【Lintcode】013.strStr
题目: For a given source string and a target string, you should output the first index(from 0) of targ ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- [PHP源码阅读]strpos、strstr和stripos、stristr函数
我在github有对PHP源码更详细的注解.感兴趣的可以围观一下,给个star.PHP5.4源码注解.可以通过commit记录查看已添加的注解. strpos mixed strpos ( strin ...
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
随机推荐
- Winform 支持高清屏(High DPI) 设置
http://www.cnblogs.com/weiym/p/3555068.htmlhttp://crsouza.com/2015/04/how-to-fix-blurry-windows-form ...
- VBA唏嘘戏——简单单元格的设定(实例)
由于有很多个Word文件,所以应用宏会更加方便排版,而且版式较为统一. Sub 设置列宽() ' ' 设置列宽宏 ' ' ActiveDocument.Tables().Cell(, ).Width ...
- MicroERP开发技术分享:vsFlexGrid、scriptControl实现工资表自定义列与表间关系计算
开发大型的MIS系统,肯定是离不开第三方控件的,同时也要根据项目需要自己写几个. MicroERP共用了以下几个控件: 第三方商业控件: vsFlexGrid:大名鼎鼎的表格控件,不用多说,配合vsP ...
- SpringMVC与MyBatis整合(一)——查询人员列表
从今天开始,一点点的记录做毕设和学习的过程. 寒假才开始接触SpringMVC和MyBatis,之前对框架的概念理解并不到位,也没学过Spring.目前学习起来思路并不很清晰,有些东西我还不能理解,只 ...
- OC基础--对象做参数在方法间传递
剧情描述: 美国大兵抗把汉阳造 拿着5个弹夹(每个弹夹5发子弹) 带着弟兄们干架 子弹打完了就求救 类: 士兵: 属性: 姓名(_name) 身高(_height) 体重(_weight) 行为: 开 ...
- Add listitem with javascript 分类: Sharepoint 2015-07-16 20:23 4人阅读 评论(0) 收藏
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', createListItem);//makes sure sp.js is loaded and the ...
- ServiceStack V3 版本 免费 redis的操作类
Referencing v3 packages in New Projects If you want a new project to use ServiceStack's v3 packages ...
- 用Window Authentication的方式去连接SQLServer
用Window Authentication的方式去连接SQLServer Connection String: jdbc:sqlserver://${serverName};databaseName ...
- JS中的属性和变量的区别
在很多文章中都说变量其实就是属性,但是它们之间有一定的区别,例如: 在全局作用域下, var a = "hello"; b = "hello"; 从字面上看,它 ...
- mkstemp生成临时文件
使用该函数可以指定目录生成临时文件,函数原型为 int mkstemp(char *template); 应用举例 int main(int argc, char *argv[]) { /* char ...