*************
C++
topic:28. 找出字符串中第一个匹配项的下标 - 力扣(LeetCode)
*************
Have a slight check about the topic.
![]() |
I have solved this type of topics many times. But I forget the method, use double pointers? Two strings and compare them, maybe subsequence works. Something begins with shit is the best start. I have a small dream that I will buy a house in the top city center. The house has a huge floor-to-ceiling window. I sit in the sofa and watch the city breathing.
![]() |
Get the haystark and needle strings size first.
class Solution {
public:
int strStr(string haystack, string needle) {
// 获取两个串的长度
int m = haystack.size();
int n = needle.size();
}
};
If m < n, it implies the program will return -1. Both if writing style are fine but I perfer the second one. A good writer has a good habbit.
class Solution {
public:
int strStr(string haystack, string needle) {
// 获取两个串的长度
int m = haystack.size();
int n = needle.size();
// 这两种写法都OK,但是我喜欢第二种写法
if m < n return -1;
if (m < n)
{
return -1;
}
}
};
The violent compareing is the most intuitive way. If haystack[i] == needle[j], move to the next letter and so on.
![]() |
- i = 0, check j = 0 ~ 6
- i = 1, check j = 0 ~ 6
- i = 2, check j = 0 ~ 6
- i = 3, check j = 0 ~ 6
- i = 4, check j = 0 ~ 6
- i = 5, check j = 0 ~ 6
- i = 6, check j = 0 ~ 6
The work flow ensyres each of the letters in hasstack are visited.
class Solution {
public:
int strStr(string haystack, string needle) {
// 获取两个串的长度
int m = haystack.size();
int n = needle.size();
// 这两种写法都OK,但是我喜欢第二种写法
// if m < n return -1;
if (m < n)
{
return -1;
}
for (int i = 0; i <= m - n; ++i)
{
int j;
for (j = 0; j < n; ++j)
{
if (haystack[i + j] != needle[j])
{
break;
}
}
if (j == n)
{
return i;
}
}
return -1;
}
};
It is not the first day I write the code. Introduce an exciting lstandard library in c++.
size_type find(const string& str, size_type pos = 0) const; // 查找子串
size_type find(const char* s, size_type pos, size_type n) const; // 查找前n个字符组成的子串
size_type find(char c, size_type pos = 0) const; // 查找单个字符
This is a function. Calling find function is really easy.
size_t Astring.find(Bstring, position)
std::string text = "The quick brown fox jumps over the lazy dog";
size_t pos = text.find("fox"); // 返回 16
从指定位置开始查找
size_t pos = text.find("the", 10); // 从索引10开始查找,返回 31
查找字符
size_t pos = text.find('q'); // 返回 4
判断有没有找到
size_t pos = text.find("cat");
if (pos != std::string::npos) {
std::cout << "Found at position: " << pos;
} else {
std::cout << "Not found"; // 本例会输出此结果
}
std::string::npos is the result of no find. npos means no position here.
so if pos != std::string::npos, return position.
class Solution {
public:
int strStr(string haystack, string needle) {
// 直接使用std::string的find方法
size_t pos = haystack.find(needle);
if (pos != string::npos)
{
return pos;
}
else
{
return -1;
}
}
};
And there is a more elegant way to write if else:
条件表达式 ? 表达式1 : 表达式2;
int x = 10, y = 20;
int max = (x > y) ? x : y;
// x>y吗? -> 是,返回x
// -> 不是,返回y
So the code can be written as follow.
class Solution {
public:
int strStr(string haystack, string needle) {
// 直接使用std::string的find方法
size_t pos = haystack.find(needle);
// 处理未找到的情况(返回-1)
return (pos != string::npos) ? pos : -1;
}
};

In the real work, excepting the algorythm post, knowing the function and calling functions are enough. Data structure and algorythm are need some sparks to find the elegant way to solve the problem.
Smart as Lei Jun, is a very talent coder, become a boss in the end.


Code is not everything. But the first thing is being fucking professional. Then things becomes possible.



989

被折叠的 条评论
为什么被折叠?



