package javaLeetCode.primary;

import java.util.Scanner;

public class ImplementStrStr_28 {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.println("Please input a string(hayStack):");
String hayStack = input.next();
System.out.println("Please input a string(needle):");
String needle = input.next();
System.out.println(strStr_1(hayStack, needle));
}// end main() /**
* 1. Find the same character in "haystack" as the first "needle" character.
* 2. Start with this character and move back in the "haystack" one by one against the characters in the "needle".
* */
/*
* Input: haystack = "hello", needle = "ll"
* Output: 2
* Input: haystack = "helollo", needle = "ll"
* Output: 4
* Input: haystack = "aaaaa", needle = "bba"
* Output: -1
*Input: haystack = "a", needle = ""
* Output: 0
* Input: haystack = "abcd", needle = "b"
* Output: 0
* */
public static int strStr_1(String hayStack, String needle) {
int i = 0;
boolean isInclude = false;
// Judge "needle" is valid.
if (needle.length()==0 || needle == null) {
return 0;
} // end if
if (needle.length() > hayStack.length()) {
return -1;
} else {
for (i = 0; i < hayStack.length(); i++) {
if (hayStack.charAt(i) == needle.charAt(0)&&(hayStack.length()-i>=needle.length())) {
int k = i;
isInclude = true;
for (int j = 1; j < needle.length(); j++) {
if (needle.charAt(j) == hayStack.charAt(++k)) {
isInclude = true;
} else {
isInclude = false;
break;
} // end if
} // end for
} else {
continue;
}//end if
if (isInclude == true) {
break;
} // end if
} // end for
} // end if
return i < hayStack.length() ? i : -1;
}// end strStr() /**
* Use the method of string class.
* */
public static int strStr_2(String hayStack, String needle) {
return hayStack.indexOf(needle);
}// end strStr()
}//end ImplementStrStr_28

Java实现LeetCode_0028_ImplementStrStr的更多相关文章

  1. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  2. 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题

    背景起因: 记起以前的另一次也是关于内存的调优分享下   有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...

  3. Elasticsearch之java的基本操作一

    摘要   接触ElasticSearch已经有一段了.在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题.看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得 ...

  4. 论:开发者信仰之“天下IT是一家“(Java .NET篇)

    比尔盖茨公认的IT界领军人物,打造了辉煌一时的PC时代. 2008年,史蒂夫鲍尔默接替了盖茨的工作,成为微软公司的总裁. 2013年他与微软做了最后的道别. 2013年以后,我才真正看到了微软的变化. ...

  5. 故障重现, JAVA进程内存不够时突然挂掉模拟

    背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...

  6. 死磕内存篇 --- JAVA进程和linux内存间的大小关系

    运行个JAVA 用sleep去hold住 package org.hjb.test; public class TestOnly { public static void main(String[] ...

  7. 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用

    有一种场景, 手机内存卡空间被用光了,但又不知道哪个文件占用了太大,一个个文件夹去找又太麻烦,所以我开发了个小程序把手机所有文件(包括路径下所有层次子文件夹下的文件)进行一个排序,这样你就可以找出哪个 ...

  8. Java多线程基础学习(二)

    9. 线程安全/共享变量——同步 当多个线程用到同一个变量时,在修改值时存在同时修改的可能性,而此时该变量只能被赋值一次.这就会导致出现“线程安全”问题,这个被多个线程共用的变量称之为“共享变量”. ...

  9. Java多线程基础学习(一)

    1. 创建线程    1.1 通过构造函数:public Thread(Runnable target, String name){}  或:public Thread(Runnable target ...

随机推荐

  1. spring boot构建restful服务

    使用spring boot快速构建出restful服务 JPA实现REST 创建spring boot项目,在项目文件pom.xml中添加以下依赖: <dependency> <gr ...

  2. shrine

    0x01 import flask import os app = flask.Flask(__name__) app.config['FLAG'] = os.environ.pop('FLAG') ...

  3. Springboot Mybatis 打包jar扫描bean与mapper问题研究与解决

    SpringBootLean 是对springboot学习与研究项目,是根据实际项目的形式对进行配置与处理,欢迎star与fork. [oschina 地址] http://git.oschina.n ...

  4. Jmeter参数化四种方式

    JMeter的三种参数化方式包括: 1.用户参数 2.函数助手 3.CSV Data Set Config/CSV数据配置文件 4.用户自定义变量 一.用户参数 位置:添加-前置处理器-用户参数 操作 ...

  5. Linux之cat的使用介绍

                                                                                                cat选项分析 ...

  6. csdn code git下载运行的问题

    问题是,上传上去的脚本编码会有问题,老报 syntax error: unexpected end of file 搜索之,找到了 http://hi.baidu.com/homappy/item/f ...

  7. JSR303后端校验详细笔记

    目录 JSR303 使用步骤 关于不为空 分组校验 自定义校验 完整代码 JSR303 使用步骤 1.依赖 <!--数据校验--> <dependency> <group ...

  8. mac OS和win7笔记本实现文件共享

    记录下macbook通过共享读取win7笔记本中文件的过程,条件是两台电脑处于同一无线网中 win7操作 点击网络图标--属性--更改高级共享设置 选择公用后选择以下选项:启动网络发现--启动文件和打 ...

  9. .Net基础之4——流程控制

    (1)异常捕获 我们在程序中经常会出现各种各样的异常,你如果想要你的程序变得坚强一点. 在你的代码中应该经常性的使用try-catch来进行异常捕获. 语法: try { 可能会出现异常的代码: } ...

  10. jsonp跨域封装

    一.什么是同源政策? 同源策略是指在Web浏览器中,允许某个网页脚本访问另一个网页的数据,但前提是这两个网页必须有相同的URI.主机名和端口号,一旦两个网站满足上述条件,这两个网站就被认定为具有相同来 ...