当前位置:网站首页>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
边栏推荐
- Gut liver axis: host microbiota interaction affects hepatocarcinogenesis
- Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
- Brushless motor drive scheme based on Infineon MCU GTM module
- Apache Bench(ab 压力测试工具)的安装与使用
- mysql ,binlog 日志查询
- Microbial neuroimmune axis -- the hope of prevention and treatment of cardiovascular diseases
- Go reflection rule
- Iron and intestinal flora
- Matlab minimalist configuration of vscode configuration
- Installation du compilateur croisé de la plateforme zynq
猜你喜欢
优麒麟 22.04 LTS 版本正式发布 | UKUI 3.1开启全新体验
【BIM+GIS】ArcGIS Pro2. 8 how to open Revit model, Bim and GIS integration?
Chapter 4 - understanding standard equipment documents, filters and pipelines
Jetpack 之 LifeCycle 组件使用详解
A new method for evaluating the quality of metagenome assembly - magista
Recursive call -- Enumeration of permutations
STM32F4单片机ADC采样及ARM-DSP库的FFT
MYSQL50道基础练习题
Set classic topics
【论文阅读】【3d目标检测】Voxel Transformer for 3D Object Detection
随机推荐
test
Redis command Encyclopedia
【Echart】echart 入门
Single chip microcomputer serial port data processing (1) -- serial port interrupt sending data
Migrate from MySQL database to AWS dynamodb
指纹Key全国产化电子元件推荐方案
【论文阅读】【3d目标检测】Voxel Transformer for 3D Object Detection
STM32 MCU ADC rule group multi-channel conversion DMA mode
[echart] démarrer avec echart
STM32单片机ADC规则组多通道转换-DMA模式
Create VPC in AWS console (no plate)
记录一下盲注脚本
单片机串口数据处理(2)——uCOSIII+循环队列接收数据
RC低通滤波器的逆系统
无线键盘全国产化电子元件推荐方案
Fusobacterium -- symbiotic bacteria, opportunistic bacteria, oncobacterium
顺序表的基本操作
QML advanced (IV) - drawing custom controls
[mapping program design] coordinate azimuth calculation artifact (version C)
2020 is coming to an end, special and unforgettable.