当前位置:网站首页>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
边栏推荐
猜你喜欢
MYSQL查询至少连续n天登录的用户
Understand the gut organ axis, good gut and good health
Gut liver axis: host microbiota interaction affects hepatocarcinogenesis
阿里十年技术专家联合打造“最新”Jetpack Compose项目实战演练(附Demo)
[paper reading] [3D object detection] voxel transformer for 3D object detection
Mysql50 basic exercises
win10, mysql-8.0.26-winx64.zip 安装
[timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN
Unipolar NRZ code, bipolar NRZ code, 2ASK, 2FSK, 2PSK, 2DPSK and MATLAB simulation
兼容NSR20F30NXT5G的小体积肖特基二极管
随机推荐
IEEE Transactions on systems, man, and Cybernetics: Notes for systems (TSMC)
Leetcode->1 两数之和
Record your own dataset with d435i, run orbslam2 and build a dense point cloud
Installation and use of Apache bench (AB pressure test tool)
229. Find mode II
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
【Echart】echart 入門
383. Ransom letter
阿里十年技术专家联合打造“最新”Jetpack Compose项目实战演练(附Demo)
IEEE Transactions on Systems, Man, and Cybernetics: Systems(TSMC)投稿须知
QML进阶(五)-通过粒子模拟系统实现各种炫酷的特效
AWS eks add cluster user or Iam role
记录一下盲注脚本
Supplement: Annotation
What is the thirty-six plan
Fusobacterium -- symbiotic bacteria, opportunistic bacteria, oncobacterium
AWS EKS 部署要点以及控制台与eksctl创建的差异
520.检测大写字母
leetcode008--实现strStr()函数
【Pytorch基础】torch.split()用法