Carregando

Problema do LeetCode#28

Find the Index of the First Occurrence in a String

Resolvido em 1 linguagemC#

Resolver no LeetCode(abre em uma nova aba)

Solução

21 linhas
1public class Solution {
2    public int StrStr(string haystack, string needle) {
3        for (int i = 0; i <= haystack.Length - needle.Length; i++)
4        {
5            bool matches = true;
6            for (int j = 0; j < needle.Length; j++)
7            {
8                if (haystack[i + j] != needle[j])
9                {
10                    matches = false;
11                    break;
12                }
13            }
14            if (matches)
15            {
16                return i;
17            }
18        }
19        return -1;
20    }
21}

Quer uma explicação desta solução em C#?

Receba uma explicação passo a passo da ideia por trás do código.

Todos os problemas

Os enunciados dos problemas pertencem ao LeetCode e estão em leetcode.com. Não temos vínculo com o LeetCode.