力扣00028.找出字符串中第一个匹配项的下标


题目描述

给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标(下标从 0 开始)。如果 needle 不是 haystack 的一部分,则返回 -1 。

示例 1:

输入:haystack = “sadbutsad”, needle = “sad”
输出:0
解释:”sad” 在下标 0 和 6 处匹配。
第一个匹配项的下标是 0 ,所以返回 0 。

示例 2:

输入:haystack = “leetcode”, needle = “leeto”
输出:-1
解释:”leeto” 没有在 “leetcode” 中出现,所以返回 -1 。

提示:

  • $1 <= haystack.length, needle.length <= 10^4$
  • haystack 和 needle 仅由小写英文字符组成

解决方法

C++

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int strStr(std::string haystack, std::string needle) {
size_t pos = haystack.find(needle);
if (pos != std::string::npos) {
return static_cast<int>(pos);
} else {
return -1;
}
}
};

结果

执行用时 : 1 ms, 击败 32.03% 使用 C++ 的用户

内存消耗 : 7.45 MB, 击败 5.03% 使用 C++ 的用户


Java

1
2
3
4
5
6
public class Solution {
public int strStr(String haystack, String needle) {
int index = haystack.indexOf(needle);
return index;
}
}

结果

执行用时 : 0 ms, 击败 100.00% 使用 Java 的用户

内存消耗 : 40.36 MB, 击败 20.59% 使用 Java 的用户


Python

1
2
3
4
5
6
7
8
9
class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
index = haystack.find(needle)
return index

结果

执行用时 : 15 ms, 击败 76.06% 使用 Python 的用户

内存消耗 : 11.54 MB, 击败 92.21% 使用 Python 的用户


Python3

1
2
3
4
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
index = haystack.find(needle)
return index

结果

执行用时 : 38 ms, 击败 73.98% 使用 Python3 的用户

内存消耗 : 16.38 MB, 击败 33.01% 使用 Python3 的用户


C

1
2
3
4
5
6
7
8
int strStr(char* haystack, char* needle) {
char* result = strstr(haystack, needle);
if (result != NULL) {
return result - haystack;
} else {
return -1;
}
}

结果

执行用时 : 0 ms, 击败 100.00% 使用 C 的用户

内存消耗 : 5.25 MB, 击败 99.47% 使用 C 的用户


C#

1
2
3
4
5
6
public class Solution {
public int StrStr(string haystack, string needle) {
int index = haystack.IndexOf(needle);
return index;
}
}

结果

执行用时 : 50 ms, 击败 77.71% 使用 C# 的用户

内存消耗 : 39.58 MB, 击败 5.22% 使用 C# 的用户


JavaScript

1
2
3
4
5
6
7
8
9
/**
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
var strStr = function(haystack, needle) {
var index = haystack.indexOf(needle);
return index;
};

结果

执行用时 : 58 ms, 击败 69.67% 使用 JavaScript 的用户

内存消耗 : 48.68 MB, 击败 10.90% 使用 JavaScript 的用户


TypeScript

1
2
3
4
function strStr(haystack: string, needle: string): number {
const index: number = haystack.indexOf(needle);
return index;
}

结果

执行用时 : 68 ms, 击败 43.15% 使用 TypeScript 的用户

内存消耗 : 50.23 MB, 击败 13.29% 使用 TypeScript 的用户


PHP

1
2
3
4
5
6
7
8
9
10
11
class Solution {
/**
* @param String $haystack
* @param String $needle
* @return Integer
*/
function strStr($haystack, $needle) {
$index = strpos($haystack, $needle);
return $index !== false ? $index : -1;
}
}

结果

执行用时 : 3 ms, 击败 92.96% 使用 PHP 的用户

内存消耗 : 19.80 MB, 击败 5.63% 使用 PHP 的用户


Swift

1
2
3
4
5
6
7
8
9
class Solution {
func strStr(_ haystack: String, _ needle: String) -> Int {
if let range = haystack.range(of: needle) {
return haystack.distance(from: haystack.startIndex, to: range.lowerBound)
} else {
return -1
}
}
}

结果

执行用时 : 1 ms, 击败 75.25% 使用 Swift 的用户

内存消耗 : 16.25 MB, 击败 5.94% 使用 Swift 的用户


Kotlin

1
2
3
4
5
6
class Solution {
fun strStr(haystack: String, needle: String): Int {
val index = haystack.indexOf(needle)
return if (index != -1) index else -1
}
}

结果

执行用时 : 157 ms, 击败 24.11% 使用 Kotlin 的用户

内存消耗 : 34.41 MB, 击败 16.96% 使用 Kotlin 的用户


Dart

1
2
3
4
5
6
class Solution {
int strStr(String haystack, String needle) {
int index = haystack.indexOf(needle);
return index;
}
}

结果

执行用时 : 292 ms, 击败 23.08% 使用 Dart 的用户

内存消耗 : 143.05 MB, 击败 92.31% 使用 Dart 的用户


Go

1
2
3
4
func strStr(haystack string, needle string) int {
index := strings.Index(haystack, needle)
return index
}

结果

执行用时 : 0 ms, 击败 100.00% 使用 Go 的用户

内存消耗 : 1.87 MB, 击败 47.87% 使用 Go 的用户


Ruby

1
2
3
4
5
6
7
# @param {String} haystack
# @param {String} needle
# @return {Integer}
def str_str(haystack, needle)
index = haystack.index(needle)
return index.nil? ? -1 : index
end

结果

执行用时 : 59 ms, 击败 85.71% 使用 Ruby 的用户

内存消耗 : 206.23 MB, 击败 71.43% 使用 Ruby 的用户


Scala

1
2
3
4
5
6
object Solution {
def strStr(haystack: String, needle: String): Int = {
val index = haystack.indexOf(needle)
return index
}
}

结果

执行用时 : 453 ms, 击败 53.85% 使用 Scala 的用户

内存消耗 : 52.46 MB, 击败 46.15% 使用 Scala 的用户


Rust

1
2
3
4
5
6
7
8
9
impl Solution {
pub fn str_str(haystack: String, needle: String) -> i32 {
if let Some(index) = haystack.find(&needle) {
return index as i32;
} else {
return -1;
}
}
}

结果

执行用时 : 0 ms, 击败 100.00% 使用 Rust 的用户

内存消耗 : 1.95 MB, 击败 92.17% 使用 Rust 的用户


Racket

暂时未解决

1

结果

执行用时 : ms, 击败 % 使用 Racket 的用户

内存消耗 : MB, 击败 % 使用 Racket 的用户


Erlang

暂时未解决

1

结果

执行用时 : ms, 击败 % 使用 Erlang 的用户

内存消耗 : MB, 击败 % 使用 Erlang 的用户


Elixir

暂时未解决

1

结果

执行用时 : ms, 击败 % 使用 Elixir 的用户

内存消耗 : MB, 击败 % 使用 Elixir 的用户