当前位置:网站首页>[LeetCode]28. Implement strstr()
[LeetCode]28. Implement strstr()
2022-06-13 00:12:00 【PowerDon】
Realization strStr() function .
Given a haystack String and a needle character string , stay haystack Find in string needle The first place the string appears ( from 0 Start ). If it doesn't exist , Then return to -1.
Example 1:
Input : haystack = “hello”, needle = “ll”
Output : 2
Example 2:
Input : haystack = “aaaaa”, needle = “bba”
Output : -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 .
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/implement-strstr
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
public class Solution {
public int StrStr(string haystack, string needle) {
// Super lazy writing
// return haystack.IndexOf(needle);
// Fancy writing
// if(needle.Length == 0){
// return 0;
// }
// string[] temp = haystack.Split(needle);
// if(temp.Length > 1){
// return temp[0].Length;
// }
// return -1;
// There is no technical content
if(needle.Length == 0){
return 0;
};
for(int i = 0; i < haystack.Length; i++){
if(haystack.Length - i < needle.Length){
return -1;
}
int count = 0;
for(int j = 0; j < needle.Length; j++){
if(haystack[i+j] != needle[j]){
break;
}
++count;
//Console.WriteLine($"{haystack[i+j]}:{needle[j]}:Count:{i-count}");
if(count == needle.Length){
return (i+j+1)-count;
}
}
}
return -1;
// Water in the head
// if(needle.Length == 0){
// return 0;
// }
// int hayCount = 0;
// int neeCount = 0;
// int count = 0;
// int target = 0;
// while(hayCount < haystack.Length){
// Console.WriteLine(hayCount+":"+neeCount);
// if(haystack[hayCount] == needle[neeCount]){
// hayCount++;
// neeCount++;
// count++;
// }else{
// hayCount++;
// count = 0;
// neeCount = 0;
// target++;
// hayCount = target;
// }
// if(count == needle.Length){
// return hayCount-count;
// }
// }
// return -1;
}
}
边栏推荐
- ik分词器的安装
- 2022施工員-設備方向-通用基礎(施工員)操作證考試題及模擬考試
- 2022 operation of simulated examination platform for hoisting machinery command certificate
- PMP training organization
- leaflet如何优雅的展示重叠点位的气泡窗口
- Online examination questions for September examination of financial management
- Real time preview of PHP in browser by vscade
- 3、 Storage system
- How to publish OSM maps locally and customize the mapping
- The PMP examination time in March 2022 is set -- "March 27"
猜你喜欢
随机推荐
【HCIE论述】STP-A
How about opening a securities account in flush? Is it safe or not
KConfig
进程间通信-共享内存shmat
【Matlab】矩阵变换与矩阵求值
2022-06-13日报: 图灵奖得主:想要在学术生涯中获得成功,需要注意哪些问题?
Building crud applications in golang
Test platform series (97) perfect the case part
最全预告!华为云精彩议程火速收藏
vs studio_ How to use scanf in 2022
A detailed explanation of synchronized
[matlab] 3D curve and 3D surface
3、 Storage system
How to pass the PMP review?
Start of u-boot S analysis (III)
2022施工員-設備方向-通用基礎(施工員)操作證考試題及模擬考試
USTC of China University of science and technology: Minrui Wang | distribution network voltage stabilization based on transformer Multi-Agent Reinforcement Learning
Learn to divide subnets in an article
The e-commerce employee changed the product price to 10% off after leaving the company, and has been detained
APISpace 空号检测API接口 免费好用