您的位置:首页
LeetCode -- 求字符串数组中的最长公共前缀
2017-05-28 17:56
591 查看
题目描写叙述:
Write a function to find the longest common prefix string amongst an array of strings.
就是给定1个字符串数组,找出公共最长前缀。
思路非常直接。使用1个索引来存最长公共前缀的长度就能够了。
注意, 假设使用1个字符串变量来存前缀的话,是不能AC的,由于题目不同意使用额外的空间。
Write a function to find the longest common prefix string amongst an array of strings.
就是给定1个字符串数组,找出公共最长前缀。
思路非常直接。使用1个索引来存最长公共前缀的长度就能够了。
注意, 假设使用1个字符串变量来存前缀的话,是不能AC的,由于题目不同意使用额外的空间。
public string LongestCommonPrefix(string[] strs) { if(strs == null || strs.Length == 0){ return string.Empty; } if(strs.Length == 1){ return strs[0]; } var index = 0; for(var j = 0;j < strs[0].Length; j++){ for(var i = 1;i < strs.Length; i++){ if(j >= strs[i].Length || strs[0][j] != strs[i][j]){ return strs[0].Substring(0,index); } } index++; } return strs[0].Substring(0 ,index); }
相关文章推荐
- LeetCode 找到一个字符串数组的最长公共前缀
- LeetCode -- 求字符串数组中的最长公共前缀
- LeetCode 14. Longest Common Prefix--字符串数组元素的最长公共前缀
- leetcode-14. Longest Common Prefix(寻找字符串数组最长公共前缀)
- leetcode-14-求字符串数组最长公共前缀
- leetcode_14. Longest Common Prefix 求字符串数组中所有字符串的最长前缀
- 每日一练之Longest Common Prefix【LeetCode No.14】——求字符串的最长公共前缀
- Leetcode练习<十九> 字符串数组的公共前缀
- 算法#26--查找字符串数组中最长的公共前缀
- 字符串数组的最长公共前缀
- PHP实现一个函数查找字符串数组中最长的公共前缀字符串
- Lettcode_14_Longest Common Prefix——查找字符串数组中最长公共前缀字符串
- leetcode:java.T014_LongestCommonPrefix--写一个函数找出一个字串所数组中的最长的公共前缀
- leetcode 求字符串数组的最大公共前缀
- Longest Common Prefix 字符串数组中查找最长相同前缀字符串@LeetCode
- 字符串数组最长公共前缀
- [转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀
- 查找字符串数组中最长的公共前缀字符串。
- 找出一个字符串数组中最长公共前缀字符串
- LeetCode 14. Longest Common Prefix 找字符串数组最长相同前缀