当前位置:网站首页>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
边栏推荐
- io. Platform. packageRoot; // ignore: deprecated_ Member_ use
- Recursive call -- Enumeration of permutations
- Single chip microcomputer serial port data processing (2) -- ucosiii + cyclic queue receiving data
- shell wc (统计字符数量)的基本使用
- Installation and use of Apache bench (AB pressure test tool)
- [AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022
- thymeleaf th:value 为null时报错问题
- Go 语言中的 logger 和 zap 日志库
- Common string processing functions in C language
- How to regulate intestinal flora? Introduction to common natural substances, probiotics and prebiotics
猜你喜欢

The 14th issue of HMS core discovery reviews the long article | enjoy the silky clip and release the creativity of the video
![[echart] démarrer avec echart](/img/40/e057f4ac07754fe6f3500f3dc72293.jpg)
[echart] démarrer avec echart

Single chip microcomputer serial port data processing (2) -- ucosiii + cyclic queue receiving data
![[AI vision · quick review of today's sound acoustic papers, issue 2] Fri, 15 APR 2022](/img/c2/fd9531e48118e384bc5d490ee8e506.png)
[AI vision · quick review of today's sound acoustic papers, issue 2] Fri, 15 APR 2022

zynq平臺交叉編譯器的安裝

Nature medicine reveals individual risk factors of coronary artery disease
![[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

Youqilin 22.04 lts version officially released | ukui 3.1 opens a new experience

Bacterial infection and antibiotic use

Record your own dataset with d435i, run orbslam2 and build a dense point cloud
随机推荐
兼容NSR20F30NXT5G的小体积肖特基二极管
STM32 MCU ADC rule group multi-channel conversion DMA mode
【论文阅读】【3d目标检测】point transformer
Go 语言中的 logger 和 zap 日志库
优麒麟 22.04 LTS 版本正式发布 | UKUI 3.1开启全新体验
Go reflection rule
补:注解(Annotation)
重剑无锋,大巧不工
leetcode004--罗马数字转整数
AWS eks add cluster user or Iam role
Stm32f4 MCU ADC sampling and FFT of ARM-DSP Library
QML进阶(五)-通过粒子模拟系统实现各种炫酷的特效
Detailed explanation of life cycle component of jetpack
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
IEEE Transactions on systems, man, and Cybernetics: Notes for systems (TSMC)
Youqilin 22.04 lts version officially released | ukui 3.1 opens a new experience
win10, mysql-8.0.26-winx64. Zip installation
Installation and use of Apache bench (AB pressure test tool)
test
RC低通滤波器的逆系统