LeetCode Online Judge 题目C# 练习 - Implement strStr()
2012-09-07 05:47
549 查看
Implement strStr().
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
needle in the haystack 大海捞针的意思
其实就是找出 “短字符串” 在 “长字符串” 中的位置。
代码分析:
第一时间想到的应该是这个两重循环的方法,O(n2)。
代码分析:
为了提高效率,我们希望直接跳到needle长度后一个字符来比较。比如说。
“ABDABDABC”中找“ABC"。如果对比完第一个"ABD"发现不相等,我们希望跳到下一个"A"开始,而不是从字符串中第二个字符"B"开始。
但是如果needle字符串中带有重复的。比如 "ABABACAB" 中找 “ABAC" 比较完 "ABAB" != "ABAC"以后,不能跳到第3个"A"开始比较,因为needle中有重复的,这样会错过了从第二个”A"开始的“ABAC".
所以,以上代码使用了一个array来 看needle字符串中重复位置。
”ABAC" 会生成ARRAY [ -1,0,0,1], 当比较到"B" != "C", h_index = h_index + n_index - table[n_index], 从第2个“A”开始再比较。
Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.
needle in the haystack 大海捞针的意思
其实就是找出 “短字符串” 在 “长字符串” 中的位置。
public static string strStr(string haystack, string needle) { if (haystack.Length == 0) return string.Empty; if (needle.Length == 0) return string.Empty; if (needle.Length > haystack.Length) return string.Empty; for (int i = 0; i < haystack.Length; i++) { bool found = true; for (int j = 0; j < needle.Length; j++) { if (haystack[i + j] != needle[j]) { found = false; break; } } if(found) return haystack.Substring(i); } return string.Empty; }
代码分析:
第一时间想到的应该是这个两重循环的方法,O(n2)。
public static string strStrOpt(string haystack, string needle) { if (haystack.Length == 0) return string.Empty; if (needle.Length == 0) return string.Empty; if (needle.Length > haystack.Length) return string.Empty; //Create a partial match table int[] table = new int[needle.Length]; partial_table(needle, table); // Search int h_index = 0; int n_index = 0; while (h_index + n_index < haystack.Length) { if (needle[n_index] == haystack[h_index + n_index]) { if (n_index == needle.Length - 1) return haystack.Substring(h_index); else ++n_index; } else { h_index = h_index + n_index - table[n_index]; if (table[n_index] > -1) n_index = table[n_index]; else n_index = 0; } } return string.Empty; } public static void partial_table(string word, int[] table) { if (0 == table.Length) return; if (1 == table.Length) { table[0] = -1; return; } table[0] = -1; table[1] = 0; int pos = 2; int cnd = 0; while (pos < table.Length) { if (word[pos - 1] == word[cnd]) table[pos++] = ++cnd; else if (cnd > 0) cnd = table[cnd]; else table[pos++] = 0; } }
代码分析:
为了提高效率,我们希望直接跳到needle长度后一个字符来比较。比如说。
“ABDABDABC”中找“ABC"。如果对比完第一个"ABD"发现不相等,我们希望跳到下一个"A"开始,而不是从字符串中第二个字符"B"开始。
但是如果needle字符串中带有重复的。比如 "ABABACAB" 中找 “ABAC" 比较完 "ABAB" != "ABAC"以后,不能跳到第3个"A"开始比较,因为needle中有重复的,这样会错过了从第二个”A"开始的“ABAC".
所以,以上代码使用了一个array来 看needle字符串中重复位置。
”ABAC" 会生成ARRAY [ -1,0,0,1], 当比较到"B" != "C", h_index = h_index + n_index - table[n_index], 从第2个“A”开始再比较。
相关文章推荐
- LeetCode Online Judge 题目C# 练习 - Sprial Matrix
- LeetCode Online Judge 题目C# 练习 - Unique Paths
- LeetCode Online Judge 题目C# 练习 - Length of Last Word
- LeetCode Online Judge 题目C# 练习 - Valid Number
- LeetCode Online Judge 题目C# 练习 - Edit Distance
- LeetCode Online Judge 题目C# 练习 - Combination
- LeetCode Online Judge 题目C# 练习 - Reverse Nodes in k-Group
- LeetCode Online Judge 题目C# 练习 - 3SUM Closest
- LeetCode Online Judge 题目C# 练习 - Subsets
- LeetCode Online Judge 题目C# 练习 - Valid Sudoku
- LeetCode Online Judge 题目C# 练习 - Climbing Stairs
- LeetCode Online Judge 题目C# 练习 - N-Queens
- LeetCode Online Judge 题目C# 练习 - Subsets II
- LeetCode Online Judge 题目C# 练习 - Anagrams
- LeetCode Online Judge 题目C# 练习 - Word Search
- LeetCode Online Judge 题目C# 练习 - Minimum Window Substring
- LeetCode Online Judge 题目C# 练习 - ZigZag Conversion
- LeetCode Online Judge 题目C# 练习 - Gray Code
- LeetCode Online Judge 题目C# 练习 - Remove Element
- LeetCode Online Judge 题目C# 练习 - Count and Say