当前位置:网站首页>leetcode008--实现strStr()函数
leetcode008--实现strStr()函数
2022-04-23 04:38:00 【singularityDZF】
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class test08 {
/**
* 实现strStr()函数。
* 给你两个字符串haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串出现的第一个位置(下标从 0 开始)。如果不存在,则返回 -1 。
*
* 说明:
* 当needle是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
* 对于本题而言,当needle是空字符串时我们应当返回 0 。这与 C 语言的strstr()以及 Java 的indexOf()定义相符。
*
* 示例 1:
* 输入:haystack = "hello", needle = "ll"
* 输出:2
*
* 示例 2:
* 输入:haystack = "aaaaa", needle = "bba"
* 输出:-1
*
* 示例 3:
* 输入:haystack = "", needle = ""
* 输出:0
*
* 提示:
* 0 <= haystack.length, needle.length <= 5 * 104
* haystack 和 needle 仅由小写英文字符组成
* @param args
*/
public static void main(String[] args) throws IOException {
//创建缓冲字符输入流
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String haystack = br.readLine();
String needle = br.readLine();
System.out.println(strStr(haystack,needle));
}
/**
* 正向思路
*/
// 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;
// }
/**
* 反向思路
* @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://blog.csdn.net/dangzefei/article/details/124357048
边栏推荐
- Supplement: Annotation
- Effects of antibiotics on microbiome and human health
- eksctl 部署AWS EKS
- C language character constant
- 第四章 --- 了解标准设备文件、过滤器和管道
- KVM error: Failed to connect socket to ‘/var/run/libvirt/libvirt-sock‘
- Iron and intestinal flora
- 用D435i录制自己的数据集运行ORBslam2并构建稠密点云
- Recursive call -- Enumeration of permutations
- Summary of Android development posts I interviewed in those years (attached test questions + answer analysis)
猜你喜欢
Use recyclerview to realize left-right side-by-side classification selection
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
兼容NSR20F30NXT5G的小体积肖特基二极管
Recursive call -- Enumeration of permutations
Go reflection rule
Phishing for NFT
Microbial neuroimmune axis -- the hope of prevention and treatment of cardiovascular diseases
IDE Idea 自动编译 与 On Upate Action 、 On Frame Deactivation 的配置
Matlab minimalist configuration of vscode configuration
test
随机推荐
229. 求众数 II
520. Detect capital letters
Fusobacterium -- symbiotic bacteria, opportunistic bacteria, oncobacterium
2020 is coming to an end, special and unforgettable.
STM32 upper μ C / shell transplantation and Application
[AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022
【论文阅读】【3d目标检测】Voxel Transformer for 3D Object Detection
MYSQL去重方法汇总
上海航芯技术分享 | ACM32 MCU安全特性概述
Go反射法则
Common string processing functions in C language
zynq平臺交叉編譯器的安裝
Supplement: Annotation
STM32单片机ADC规则组多通道转换-DMA模式
Go reflection rule
Migrate from MySQL database to AWS dynamodb
Inverse system of RC low pass filter
Gut liver axis: host microbiota interaction affects hepatocarcinogenesis
The perfect combination of collaborative process and multi process
VHDL语言实现32位二进制数转BCD码