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 ...
随机推荐
- WireShark网络性能分析
最近生产上出现一个性能问题,表现为:行情延时5s左右.从log一路追查下去,发现是我们自己写的一个行情网关(部署在xx.xx.xx.132)<->第三方的中转网关(部署在xx.xx.xx. ...
- php学习
一.session使用: 1.所有内容页——最前面(<html>标签以前)添加以下代码: <?php if(!isset($_session)){ session_start(); ...
- Python3学习(二)-递归函数、高级特性、切片
##import sys ##sys.setrecursionlimit(1000) ###关键字参数(**关键字参数名) ###与可变参数不同的是,关键字参数可以在调用函数时,传入带有参数名的参数, ...
- 黑马程序员——OC基础 三种特性之封装
Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结) 三种特性之一封装 (一)set方法和get方法 1) set方法 1&g ...
- iframe 简单的一个用法 局部调用
<iframe id="main_com" name="main_com" width="100%" height="750 ...
- local认证
文件路径 用途 示例 备注 #gedit /usr/local/etc/raddb/sites-available/default #gedit /usr/local/etc/raddb/sites- ...
- C++ 之 auto_ptr and shared_ptr
1.auto_ptr 这个所谓的只能指针有点鸡肋! 没有引用计数,而且还有一个所有权转移的情况! 当所有权转移后,以前的auto_ptr将会成为null 2.shared_ptr 增加了引用计数,没 ...
- 对contentoffset的理解
今天遇到一个问题,在写瀑布流时,竖屏的时候可以正常实现,在手机变成横屏后,总是显示不全. 最终查了两个小时,查到了导致这个的原因,是自己的判断cell是否在当前显示区域的方法写错了. 根本原因是没有很 ...
- @font-face 的用法
现在很多设计用的字体都是五花八门的.我们切图又不能很好的让搜索爬虫搜索.就会使用@font-face方法: @Font-face目前浏览器的兼容性: Webkit/Safari(3.2+):TrueT ...
- qt做触摸屏演示程序
界面效果图: 参考资料: http://blog.csdn.net/orz415678659/article/details/9136575 这个最重要.. https://www.oschi ...