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 ...
随机推荐
- sfliter__except_handler4
sfliter源码在vs08中编译 出现 错误error LNK2019: unresolved external symbol __except_handler4 referenced in fun ...
- (转)客户端触发Asp.net中服务端控件事件
第一章. Asp.net中服务端控件事件是如何触发的 Asp.net 中在客户端触发服务端事件分为两种情况: 一. WebControls中的Button 和HtmlControls中的Type为su ...
- pandas 数据检索
使用pandas的一些方法: 条件检索 按index取值
- web开发学习之Http协议
web入门 请求: request 浏览器向服务器发信息 响应:response 服务器回复浏览器 一个请求发出,一定有且仅有一个响应 http协议:对浏览器客户端余服务器的数据传输规范 ht ...
- 关于粒子发射(CAEmitterLayer)
技术是条长而远的路,只有不断学习丰富自己的技能才能让自己行走在路上! CAEmitterCell CAEmitterCell: CAEmitterCell是粒子发射系统里的粒子,用CAEmitterC ...
- iOS 判断View 是否是第一次显示
在实现某些需求的时候会有这样的情况,页面第一次加载显示的时候需要某些操作,而以后就不需要重复执行了, 一般这种处理都放在- (void)viewDidLoad或- (id)init因为一般这两个函数除 ...
- Java 单列模式(Singleton)
单例模式(Singleton) 定义:保证一个类仅有一个实例,并提供一个访问它的全局访问点. 目的:使得类的一个对象成为系统中的唯一实例. 如何保证一个类只有一个实例并且这个实例易于被访问呢? 我们都 ...
- InnoDB 存储引擎的锁机制
测试环境隔离级别:REPEATABLE-READ 行级别的 - Share and Exclusive Locks 共享锁 S:允许持有S锁的事务对行进行读操作 排他锁 X: 允许持有X锁的事务对行进 ...
- sql case when then else end
我们都知道SQL中适用case when then来转化数据库中的信息 比如 select (case sex when 0 then '男' else '女' end) AS sex from st ...
- Request.ServerVariables 获取服务器或者客户端信息
本机ip[客户端]:request.servervariables("remote_addr") 从哪个页面转到当前页面的:Request.ServerVariables(&quo ...