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 ...
随机推荐
- 最强密码 (百度之星复赛 T5)
题目大意: 给出一个字符串A,要求最短的字符串B,B不是A的子序列. 求最短长度 和 最短的字符串个数 |A|<=105. 题解: 1.比赛的时候没有想出来,时隔一个多月又看到了这道题,虽 ...
- Android中ListView的用法
使用方法1 显示简单的文本 在layout文件中像加入普通控件一样在layout文件中引入ListView <ListView android:id="@+id/list_view&q ...
- 《JavaScript高级程序设计》读书笔记--(1)JavaScript简介
概述 JavaScript是一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在HT ...
- 转载《遭受arp攻击怎么办》
ARP(Address Resolution Protocol,地址解析协议)协议的基本功能就是通过目标设备的IP地址,查询目标设备的MAC地址,以保证通信的进行.ARP攻击仅能在以太网(局 域网如: ...
- jQuery focus、blur事件 添加、删除类名
jQuery.focusblur = function(ele,className){ var focusblurid = $(ele); focusblurid.focus(function(){ ...
- 进程和线程及Linux下的编程
一.概述 进程和线程网络上有一堆解释,我不喜欢抄袭,也不喜欢套用太教科书的说法.就以我自己的理解来说说进程和线程吧,当然自己的理解肯定不是很严谨,但是理解起来应该会比教科书快一点.进程和线程都可以认为 ...
- 006 复杂的数据类型&函数(方法)
2016-01-16 1.变量类型int double string char bool decimal变量的使用规则:先声明再赋值最后使用int number;number=10; number=2 ...
- 使用MJRefresh遇到的一个问题,上拉刷新后tableview瞬间滑到最底部
最近用MJRefresh上拉刷新时遇到一个问题,就是上拉刷新后,tableview会瞬间滑到最底部,用户还要往回翻才能看到新刷出来的数据,体验十分不好.查了很久没找到原因,最后发现在refreshvi ...
- Xcode 7如何 免费 真机调试iOS应用
运行Xcode后,点击菜单中的Preferences…进入Accounts标签,这里选择添加Apple ID: 在弹出的对话框中登入你的Apple ID,没有的话去注册一个就是了,登录成功后会看到下面 ...
- centos7安装
1.准备工具 VMware,我用的是 VMware11 2.打开VMware,创建新的虚拟机 3.选择典型-->下一步 4.稍后安装操作系统-->下一步 5.选择linux操作系统,lin ...