当前位置:网站首页>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
边栏推荐
- Introduction to Cortex-M3 register set, assembly language and C language interface
- Common string processing functions in C language
- Matlab minimalist configuration of vscode configuration
- [AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022
- [pytoch foundation] torch Split() usage
- Gut liver axis: host microbiota interaction affects hepatocarcinogenesis
- Coinbase: basic knowledge, facts and statistics about cross chain bridge
- Basic operation of sequence table
- Single chip microcomputer serial port data processing (1) -- serial port interrupt sending data
- 【Echart】echart 入門
猜你喜欢

Gut liver axis: host microbiota interaction affects hepatocarcinogenesis

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

Chlamydia infection -- causes, symptoms, treatment and Prevention
![[AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022](/img/40/72fdf9c89ed7d063cc368e6e052d0f.png)
[AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022
![[paper reading] [3D target detection] point transformer](/img/c5/b1fe5f206b5fe6e4dcd88dce11592d.png)
[paper reading] [3D target detection] point transformer

Alibaba cloud IOT transfer to PostgreSQL database scheme

/etc/bash_completion.d目录作用(用户登录立刻执行该目录下脚本)

递归调用--排列的穷举

MYSQL去重方法汇总

针对NFT的网络钓鱼
随机推荐
Chlamydia infection -- causes, symptoms, treatment and Prevention
第四章 --- 了解标准设备文件、过滤器和管道
Bacterial infection and antibiotic use
How to regulate intestinal flora? Introduction to common natural substances, probiotics and prebiotics
在AWS控制台创建VPC(无图版)
[AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022
A heavy sword without a blade is a great skill
Effects of antibiotics on microbiome and human health
[pytoch foundation] torch Split() usage
eksctl 部署AWS EKS
Iron and intestinal flora
Nature medicine reveals individual risk factors of coronary artery disease
TreeSet after class exercises
无线键盘全国产化电子元件推荐方案
电钻、电锤、电镐的区别
[BIM introduction practice] Revit building wall: detailed picture and text explanation of structure, envelope and lamination
Go reflection - go language Bible learning notes
IDE Idea 自动编译 与 On Upate Action 、 On Frame Deactivation 的配置
Basic operation of sequence table
【时序】基于 TCN 的用于序列建模的通用卷积和循环网络的经验评估