当前位置:网站首页>Leetcode008 -- implement strstr() function
Leetcode008 -- implement strstr() function
2022-04-23 04:39:00 【singularityDZF】
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class test08 {
/**
* Realization strStr() function .
* Here are two strings haystack and needle , Please come in haystack Find in string needle The first place the string appears ( Subscript from 0 Start ). If it doesn't exist , Then return to -1 .
*
* explain :
* When needle When it's an empty string , What value should we return ? This is a good question in an interview .
* For this question , When needle When it's an empty string, we should return 0 . This is related to C Linguistic strstr() as well as Java Of indexOf() The definition matches .
*
* Example 1:
* Input :haystack = "hello", needle = "ll"
* Output :2
*
* Example 2:
* Input :haystack = "aaaaa", needle = "bba"
* Output :-1
*
* Example 3:
* Input :haystack = "", needle = ""
* Output :0
*
* Tips :
* 0 <= haystack.length, needle.length <= 5 * 104
* haystack and needle It only consists of lowercase English characters
* @param args
*/
public static void main(String[] args) throws IOException {
// Create a buffered character input stream
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String haystack = br.readLine();
String needle = br.readLine();
System.out.println(strStr(haystack,needle));
}
/**
* Positive thinking
*/
// public static int strStr(String haystack, String needle){
// if(needle.length() == 0){
// return 0;
// }
// int flag = -1;
// for(int i=0; i<haystack.length(); i++){
// if(haystack.charAt(i) == needle.charAt(0)){
// if(needle.length() == 1){
// return i;
// }else if( (i+needle.length()-1) >= haystack.length()){
// return flag;
// }else{
// for(int j=1; j<needle.length(); j++){
// if(haystack.charAt(i+j) == needle.charAt(j)){
// if(j==needle.length()-1){
// flag = i;
// return flag;
// }else{
// flag = -1;
// continue;
// }
// }else {
// flag = -1;
// break;
// }
// }
// }
// }
// }
// return flag;
// }
/**
* Reverse thinking
* @param haystack
* @param needle
* @return
*/
public static int strStr(String haystack, String needle) {
int n = haystack.length(), m = needle.length();
for (int i = 0; i + m <= n; i++) {
boolean flag = true;
for (int j = 0; j < m; j++) {
if (haystack.charAt(i + j) != needle.charAt(j)) {
flag = false;
break;
}
}
if (flag) {
return i;
}
}
return -1;
}
}
版权声明
本文为[singularityDZF]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230438035294.html
边栏推荐
- leetcode007--判断字符串中的括号是否匹配
- IDE Idea 自动编译 与 On Upate Action 、 On Frame Deactivation 的配置
- 协程与多进程的完美结合
- [AI vision · quick review of NLP natural language processing papers today, No. 32] wed, 20 APR 2022
- [AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022
- Go reflection - go language Bible learning notes
- RC低通滤波器的逆系统
- Eksctl deploying AWS eks
- Introduction to Cortex-M3 register set, assembly language and C language interface
- Installation du compilateur croisé de la plateforme zynq
猜你喜欢

Apache Bench(ab 压力测试工具)的安装与使用

229. Find mode II
![[AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022](/img/eb/916a3fc1a89356fd8e74b943172ac3.png)
[AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022

MYSQL50道基础练习题

383. 赎金信

Summary of Android development posts I interviewed in those years (attached test questions + answer analysis)

test
![[AI vision · quick review of robot papers today, issue 31] Fri, 15 APR 2022](/img/f5/3cd3abee1480dc2cefa7f35696631b.png)
[AI vision · quick review of robot papers today, issue 31] Fri, 15 APR 2022

Why recommend you to study embedded

Recommended scheme for national production of electronic components for wireless charging
随机推荐
Youqilin 22.04 lts version officially released | ukui 3.1 opens a new experience
国外LEAD,联盟经理常见问答
IDE Idea 自动编译 与 On Upate Action 、 On Frame Deactivation 的配置
STM32 MCU ADC rule group multi-channel conversion DMA mode
Fusobacterium -- symbiotic bacteria, opportunistic bacteria, oncobacterium
MySQL queries users logged in for at least N consecutive days
兼容NSR20F30NXT5G的小体积肖特基二极管
C语言:恶搞小游戏
[AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022
Detailed explanation of life cycle component of jetpack
shell wc (统计字符数量)的基本使用
leetcode001--返回和为target的数组元素的下标
智能电子秤全国产化电子元件推荐方案
Recommended scheme for national production of electronic components for wireless charging
Leetcode - > 1 sum of two numbers
Installation and use of Apache bench (AB pressure test tool)
QML advanced (V) - realize all kinds of cool special effects through particle simulation system
[paper reading] [3D object detection] voxel transformer for 3D object detection
[pytoch foundation] torch Split() usage
The perfect combination of collaborative process and multi process