問題:
方法:
很簡(jiǎn)單的題目,遍歷遇到相符起始字符逐個(gè)匹配,最后輸出結(jié)果即可
package com.eric.leetcode
class ImplementStrStr {
fun strStr(haystack: String, needle: String): Int {
if (needle.isEmpty()) {
return 0
}
loop@ for (el in haystack.withIndex()) {
if (el.value == needle[0]) {
for (index in needle.indices) {
if (el.index + index > haystack.lastIndex && needle[index] != haystack[el.index + index]) {
continue@loop
}
}
return el.index
}
}
return -1
}
}
有問題隨時(shí)溝通
