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

最新文章

  1. ASP.NET MVC Model元数据(一)
  2. mysql general log日志
  3. Microsoft SQL Server Management Studio ------------------------------ 附加数据库 对于 服务器
  4. 24单行插入与批量插入-insert(必学)-天轰穿sqlserver视频教程
  5. Spring配置JNDI的解决方案
  6. js object(对象)
  7. 正确编写Designated Initializer的几个原则
  8. cloudstack 修改显示名称
  9. 如何高性能的给UIImageView加个圆角?(不准说layer.cornerRadius!)
  10. Java Evaluate Reverse Polish Notation(逆波兰式)
  11. kali linux 忘记root密码重置办法
  12. JVM常用启动参数
  13. 使用EasyWechat快速开发微信支付
  14. Lintcode393 Best Time to Buy and Sell Stock IV solution 题解
  15. Wish-递推DP记数
  16. dos2章
  17. Windows下git设置代理服务器
  18. 【学亮IT手记】jQuery text()/html()回调函数实例
  19. Centos yum 修改为阿里源以及常用的命令
  20. C++中的istringstream

热门文章

  1. HDU 2017 (水)
  2. python --函数学习之全局变量和局部变量
  3. Wfuzz使用学习
  4. node响应头缓存设置
  5. 都0202年了,你还不知道javascript有几种继承方式?
  6. 全网最详细最好懂 PyTorch CNN案例分析 识别手写数字
  7. OGG应用进程abend报错无法insert虚拟列
  8. Hyperledger Fabric Node SDK和应用开发
  9. CF #459 D. MADMAX
  10. elasticsearch7.X x-pack破解