当前位置:网站首页>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
边栏推荐
- Recommended scheme for national production of electronic components for wireless charging
- Supplement 14: cmake practice project notes (to be continued 4 / 22)
- Go reflection - go language Bible learning notes
- [pytoch foundation] torch Split() usage
- [AI vision · quick review of robot papers today, issue 31] Fri, 15 APR 2022
- [paper reading] [3D target detection] point transformer
- Bridge between ischemic stroke and intestinal flora: short chain fatty acids
- Detailed explanation of life cycle component of jetpack
- QML进阶(五)-通过粒子模拟系统实现各种炫酷的特效
- Effects of antibiotics on microbiome and human health
猜你喜欢
【Echart】echart 入門
那些年我面试过的Android开发岗总结(附面试题+答案解析)
AWS eks add cluster user or Iam role
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
AWS EKS 部署要点以及控制台与eksctl创建的差异
【时序】基于 TCN 的用于序列建模的通用卷积和循环网络的经验评估
Interaction of diet gut microbiota on cardiovascular disease
Microbial neuroimmune axis -- the hope of prevention and treatment of cardiovascular diseases
Recommended scheme of national manufactured electronic components for intelligent electronic scales
Installation du compilateur croisé de la plateforme zynq
随机推荐
Introduction to Cortex-M3 register set, assembly language and C language interface
Go反射—Go语言圣经学习笔记
1个需求的一生,团队协作在云效钉钉小程序上可以这么玩
Go 语言中的 logger 和 zap 日志库
Basic operation of sequence table
shell wc (统计字符数量)的基本使用
leetcode006--查找字符串数组中的最长公共前缀
Unipolar NRZ code, bipolar NRZ code, 2ASK, 2FSK, 2PSK, 2DPSK and MATLAB simulation
229. 求众数 II
Stm32f4 MCU ADC sampling and FFT of ARM-DSP Library
leetcode009--用二分查找在数组中搜索目标值
IEEE Transactions on industrial information (TII)
Brushless motor drive scheme based on Infineon MCU GTM module
RC低通滤波器的逆系统
What is the thirty-six plan
Key points of AWS eks deployment and differences between console and eksctl creation
Logger and zap log Library in go language
三十六计是什么
做数据可视化应该避免的8个误区
[AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022