码迷,mamicode.com
首页 > 编程语言 > 详细

14. 字符串数组的最长公共前缀 Longest Common Prefix

时间:2017-04-16 00:18:03      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:tco   for   color   else   result   cti   public   []   tle   

Write a function to find the longest common prefix string amongst an array of strings.
  1. public class Solution {
  2. public string LongestCommonPrefix(string[] strs) {
  3. StringBuilder result = new StringBuilder();
  4. if (strs != null && strs.Length > 0) {
  5. Array.Sort(strs);
  6. char[] a = strs[0].ToCharArray();
  7. char[] b = strs[strs.Length - 1].ToCharArray();
  8. for (int i = 0; i < a.Length; i++) {
  9. if (b.Length > i && b[i] == a[i]) {
  10. result.Append(b[i]);
  11. } else {
  12. return result.ToString();
  13. }
  14. }
  15. return result.ToString();
  16. } else {
  17. return "";
  18. }
  19. }
  20. }







14. 字符串数组的最长公共前缀 Longest Common Prefix

标签:tco   for   color   else   result   cti   public   []   tle   

原文地址:http://www.cnblogs.com/xiejunzhao/p/bca1d564401e3306dc16499cd8ffd10b.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!