当前位置:网站首页>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
边栏推荐
- 智能电子秤全国产化电子元件推荐方案
- io. Platform. packageRoot; // ignore: deprecated_ Member_ use
- /etc/bash_ completion. D directory function (the user logs in and executes the script under the directory immediately)
- 协程与多进程的完美结合
- 【时序】基于 TCN 的用于序列建模的通用卷积和循环网络的经验评估
- IEEE Transactions on Systems, Man, and Cybernetics: Systems(TSMC)投稿须知
- Record your own dataset with d435i, run orbslam2 and build a dense point cloud
- 兼容NSR20F30NXT5G的小体积肖特基二极管
- Introduction to Cortex-M3 register set, assembly language and C language interface
- Understand the gut organ axis, good gut and good health
猜你喜欢

Iron and intestinal flora

Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.

指纹Key全国产化电子元件推荐方案

Installation and use of Apache bench (AB pressure test tool)

Recursive call -- Enumeration of permutations

上海航芯技术分享 | ACM32 MCU安全特性概述

Recommended scheme of national manufactured electronic components

第四章 --- 了解标准设备文件、过滤器和管道

RC低通滤波器的逆系统

520.检测大写字母
随机推荐
Ali's ten-year technical experts jointly created the "latest" jetpack compose project combat drill (with demo)
递归调用--排列的穷举
国外LEAD,联盟经理常见问答
针对NFT的网络钓鱼
Installation and use of Apache bench (AB pressure test tool)
[paper reading] [3D object detection] voxel transformer for 3D object detection
leetcode004--罗马数字转整数
MySQL queries users logged in for at least N consecutive days
Effects of antibiotics on microbiome and human health
win10, mysql-8.0.26-winx64.zip 安装
/etc/bash_ completion. D directory function (the user logs in and executes the script under the directory immediately)
QML进阶(四)-绘制自定义控件
IEEE Transactions on Industrial Informatics(TII)投稿须知
2019 is coming to an end, the longest day.
做数据可视化应该避免的8个误区
IEEE Transactions on Systems, Man, and Cybernetics: Systems(TSMC)投稿须知
Fusobacterium -- symbiotic bacteria, opportunistic bacteria, oncobacterium
mysql ,binlog 日志查询
Single chip microcomputer serial port data processing (2) -- ucosiii + cyclic queue receiving data
兼容NSR20F30NXT5G的小体积肖特基二极管